History

avcodec/mpegvideo_dec: Factor allocating dummy frames out

This will allow to reuse it to allocate dummy frames for the second field (which can be a P-field even if the first field was an intra field).

Andreas Rheinhardt committed 2 years ago

Commit 99543c91 parent a30c95e2 files · +56 −33Browse files

Unified · Split

2 files
  1. libavcodec/mpegvideo_dec.c +52 −33
  2. libavcodec/mpegvideodec.h +4 −0
libavcodec/mpegvideo_dec.c+52−33
@@ -281,14 +281,21 @@ fail: · 280 unmodified lines
281281 return ret;
282282 }
283283
284−static int av_cold alloc_dummy_frame(MpegEncContext *s, Picture **picp)
284+static int av_cold alloc_dummy_frame(MpegEncContext *s, Picture **picp, Picture *wpic)
285285 {
286286 Picture *pic;
287− int ret = alloc_picture(s, picp, 1);
287+ int ret = alloc_picture(s, &pic, 1);
288288 if (ret < 0)
289289 return ret;
290290
291− pic = *picp;
291+ ff_mpeg_unref_picture(wpic);
292+ ret = ff_mpeg_ref_picture(wpic, pic);
293+ if (ret < 0) {
294+ ff_mpeg_unref_picture(pic);
295+ return ret;
296+ }
297+
298+ *picp = pic;
292299
293300 ff_thread_report_progress(&pic->tf, INT_MAX, 0);
294301 ff_thread_report_progress(&pic->tf, INT_MAX, 1);
@@ -314,6 +321,45 @@ static void color_frame(AVFrame *frame, int luma) · 19 unmodified lines
314321 }
315322 }
316323
324+int ff_mpv_alloc_dummy_frames(MpegEncContext *s)
325+{
326+ AVCodecContext *avctx = s->avctx;
327+ int ret;
328+
329+ if ((!s->last_picture_ptr || !s->last_picture_ptr->f->buf[0]) &&
330+ (s->pict_type != AV_PICTURE_TYPE_I)) {
331+ if (s->pict_type == AV_PICTURE_TYPE_B && s->next_picture_ptr && s->next_picture_ptr->f->buf[0])
332+ av_log(avctx, AV_LOG_DEBUG,
333+ "allocating dummy last picture for B frame\n");
334+ else if (s->codec_id != AV_CODEC_ID_H261 /* H.261 has no keyframes */ &&
335+ (s->picture_structure == PICT_FRAME || s->first_field))
336+ av_log(avctx, AV_LOG_ERROR,
337+ "warning: first frame is no keyframe\n");
338+
339+ /* Allocate a dummy frame */
340+ ret = alloc_dummy_frame(s, &s->last_picture_ptr, &s->last_picture);
341+ if (ret < 0)
342+ return ret;
343+
344+ if (!avctx->hwaccel) {
345+ int luma_val = s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263 ? 16 : 0x80;
346+ color_frame(s->last_picture_ptr->f, luma_val);
347+ }
348+ }
349+ if ((!s->next_picture_ptr || !s->next_picture_ptr->f->buf[0]) &&
350+ s->pict_type == AV_PICTURE_TYPE_B) {
351+ /* Allocate a dummy frame */
352+ ret = alloc_dummy_frame(s, &s->next_picture_ptr, &s->next_picture);
353+ if (ret < 0)
354+ return ret;
355+ }
356+
357+ av_assert0(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
358+ s->last_picture_ptr->f->buf[0]));
359+
360+ return 0;
361+}
362+
317363 /**
318364 * generic function called after decoding
319365 * the header and before a frame is decoded.
@@ -382,34 +428,6 @@ int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx) · 62 unmodified lines
382428 s->current_picture_ptr ? s->current_picture_ptr->f->data[0] : NULL,
383429 s->pict_type, s->droppable);
384430
385− if ((!s->last_picture_ptr || !s->last_picture_ptr->f->buf[0]) &&
386− (s->pict_type != AV_PICTURE_TYPE_I)) {
387− if (s->pict_type == AV_PICTURE_TYPE_B && s->next_picture_ptr && s->next_picture_ptr->f->buf[0])
388− av_log(avctx, AV_LOG_DEBUG,
389− "allocating dummy last picture for B frame\n");
390− else if (s->codec_id != AV_CODEC_ID_H261)
391− av_log(avctx, AV_LOG_ERROR,
392− "warning: first frame is no keyframe\n");
393−
394− /* Allocate a dummy frame */
395− ret = alloc_dummy_frame(s, &s->last_picture_ptr);
396− if (ret < 0)
397− return ret;
398−
399− if (!avctx->hwaccel) {
400− int luma_val = s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263 ? 16 : 0x80;
401− color_frame(s->last_picture_ptr->f, luma_val);
402− }
403−
404− }
405− if ((!s->next_picture_ptr || !s->next_picture_ptr->f->buf[0]) &&
406− s->pict_type == AV_PICTURE_TYPE_B) {
407− /* Allocate a dummy frame */
408− ret = alloc_dummy_frame(s, &s->next_picture_ptr);
409− if (ret < 0)
410− return ret;
411− }
412−
413431 if (s->last_picture_ptr) {
414432 if (s->last_picture_ptr->f->buf[0] &&
415433 (ret = ff_mpeg_ref_picture(&s->last_picture,
@@ -423,8 +441,9 @@ int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx) · 7 unmodified lines
423441 return ret;
424442 }
425443
426− av_assert0(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
427− s->last_picture_ptr->f->buf[0]));
444+ ret = ff_mpv_alloc_dummy_frames(s);
445+ if (ret < 0)
446+ return ret;
428447
429448 /* set dequantizer, we can't do it during init as
430449 * it might change for MPEG-4 and we can't do it in the header
libavcodec/mpegvideodec.h+4−0
@@ -50,6 +50,10 @@ void ff_mpv_decode_init(MpegEncContext *s, AVCodecContext *avctx); · 49 unmodified lines
5050 int ff_mpv_common_frame_size_change(MpegEncContext *s);
5151
5252 int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx);
53+/**
54+ * Ensure that the dummy frames are allocated according to pict_type if necessary.
55+ */
56+int ff_mpv_alloc_dummy_frames(MpegEncContext *s);
5357 void ff_mpv_reconstruct_mb(MpegEncContext *s, int16_t block[12][64]);
5458 void ff_mpv_report_decode_progress(MpegEncContext *s);
5559 void ff_mpv_frame_end(MpegEncContext *s);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t