• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Common mpeg video decoding code
3  * Copyright (c) 2000,2001 Fabrice Bellard
4  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include <limits.h>
24 
25 #include "libavutil/avassert.h"
26 #include "libavutil/imgutils.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/video_enc_params.h"
29 
30 #include "avcodec.h"
31 #include "internal.h"
32 #include "mpegutils.h"
33 #include "mpegvideo.h"
34 #include "mpegvideodec.h"
35 #include "threadframe.h"
36 
ff_mpv_decode_init(MpegEncContext * s,AVCodecContext * avctx)37 void ff_mpv_decode_init(MpegEncContext *s, AVCodecContext *avctx)
38 {
39     ff_mpv_common_defaults(s);
40 
41     s->avctx           = avctx;
42     s->width           = avctx->coded_width;
43     s->height          = avctx->coded_height;
44     s->codec_id        = avctx->codec->id;
45     s->workaround_bugs = avctx->workaround_bugs;
46 
47     /* convert fourcc to upper case */
48     s->codec_tag       = ff_toupper4(avctx->codec_tag);
49 }
50 
ff_mpeg_update_thread_context(AVCodecContext * dst,const AVCodecContext * src)51 int ff_mpeg_update_thread_context(AVCodecContext *dst,
52                                   const AVCodecContext *src)
53 {
54     MpegEncContext *const s1 = src->priv_data;
55     MpegEncContext *const s  = dst->priv_data;
56     int ret;
57 
58     if (dst == src)
59         return 0;
60 
61     av_assert0(s != s1);
62 
63     // FIXME can parameters change on I-frames?
64     // in that case dst may need a reinit
65     if (!s->context_initialized) {
66         void *private_ctx = s->private_ctx;
67         int err;
68         memcpy(s, s1, sizeof(*s));
69 
70         s->avctx                 = dst;
71         s->private_ctx           = private_ctx;
72         s->bitstream_buffer      = NULL;
73         s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
74 
75         if (s1->context_initialized) {
76 //             s->picture_range_start  += MAX_PICTURE_COUNT;
77 //             s->picture_range_end    += MAX_PICTURE_COUNT;
78             ff_mpv_idct_init(s);
79             if ((err = ff_mpv_common_init(s)) < 0) {
80                 memset(s, 0, sizeof(*s));
81                 s->avctx = dst;
82                 s->private_ctx = private_ctx;
83                 return err;
84             }
85         }
86     }
87 
88     if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
89         s->height = s1->height;
90         s->width  = s1->width;
91         if ((ret = ff_mpv_common_frame_size_change(s)) < 0)
92             return ret;
93     }
94 
95     s->avctx->coded_height  = s1->avctx->coded_height;
96     s->avctx->coded_width   = s1->avctx->coded_width;
97     s->avctx->width         = s1->avctx->width;
98     s->avctx->height        = s1->avctx->height;
99 
100     s->quarter_sample       = s1->quarter_sample;
101 
102     s->coded_picture_number = s1->coded_picture_number;
103     s->picture_number       = s1->picture_number;
104 
105     av_assert0(!s->picture || s->picture != s1->picture);
106     if (s->picture)
107         for (int i = 0; i < MAX_PICTURE_COUNT; i++) {
108             ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
109             if (s1->picture && s1->picture[i].f->buf[0] &&
110                 (ret = ff_mpeg_ref_picture(s->avctx, &s->picture[i], &s1->picture[i])) < 0)
111                 return ret;
112         }
113 
114 #define UPDATE_PICTURE(pic)\
115 do {\
116     ff_mpeg_unref_picture(s->avctx, &s->pic);\
117     if (s1->pic.f && s1->pic.f->buf[0])\
118         ret = ff_mpeg_ref_picture(s->avctx, &s->pic, &s1->pic);\
119     else\
120         ret = ff_update_picture_tables(&s->pic, &s1->pic);\
121     if (ret < 0)\
122         return ret;\
123 } while (0)
124 
125     UPDATE_PICTURE(current_picture);
126     UPDATE_PICTURE(last_picture);
127     UPDATE_PICTURE(next_picture);
128 
129 #define REBASE_PICTURE(pic, new_ctx, old_ctx)                                 \
130     ((pic && pic >= old_ctx->picture &&                                       \
131       pic < old_ctx->picture + MAX_PICTURE_COUNT) ?                           \
132         &new_ctx->picture[pic - old_ctx->picture] : NULL)
133 
134     s->last_picture_ptr    = REBASE_PICTURE(s1->last_picture_ptr,    s, s1);
135     s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
136     s->next_picture_ptr    = REBASE_PICTURE(s1->next_picture_ptr,    s, s1);
137 
138     // Error/bug resilience
139     s->workaround_bugs      = s1->workaround_bugs;
140     s->padding_bug_score    = s1->padding_bug_score;
141 
142     // MPEG-4 timing info
143     memcpy(&s->last_time_base, &s1->last_time_base,
144            (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) -
145            (char *) &s1->last_time_base);
146 
147     // B-frame info
148     s->max_b_frames = s1->max_b_frames;
149     s->low_delay    = s1->low_delay;
150     s->droppable    = s1->droppable;
151 
152     // DivX handling (doesn't work)
153     s->divx_packed  = s1->divx_packed;
154 
155     if (s1->bitstream_buffer) {
156         if (s1->bitstream_buffer_size +
157             AV_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size) {
158             av_fast_malloc(&s->bitstream_buffer,
159                            &s->allocated_bitstream_buffer_size,
160                            s1->allocated_bitstream_buffer_size);
161             if (!s->bitstream_buffer) {
162                 s->bitstream_buffer_size = 0;
163                 return AVERROR(ENOMEM);
164             }
165         }
166         s->bitstream_buffer_size = s1->bitstream_buffer_size;
167         memcpy(s->bitstream_buffer, s1->bitstream_buffer,
168                s1->bitstream_buffer_size);
169         memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
170                AV_INPUT_BUFFER_PADDING_SIZE);
171     }
172 
173     // linesize-dependent scratch buffer allocation
174     if (!s->sc.edge_emu_buffer)
175         if (s1->linesize) {
176             if (ff_mpeg_framesize_alloc(s->avctx, &s->me,
177                                         &s->sc, s1->linesize) < 0) {
178                 av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
179                        "scratch buffers.\n");
180                 return AVERROR(ENOMEM);
181             }
182         } else {
183             av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
184                    "be allocated due to unknown size.\n");
185         }
186 
187     // MPEG-2/interlacing info
188     memcpy(&s->progressive_sequence, &s1->progressive_sequence,
189            (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
190 
191     return 0;
192 }
193 
ff_mpv_common_frame_size_change(MpegEncContext * s)194 int ff_mpv_common_frame_size_change(MpegEncContext *s)
195 {
196     int err = 0;
197 
198     if (!s->context_initialized)
199         return AVERROR(EINVAL);
200 
201     ff_mpv_free_context_frame(s);
202 
203     if (s->picture)
204         for (int i = 0; i < MAX_PICTURE_COUNT; i++)
205             s->picture[i].needs_realloc = 1;
206 
207     s->last_picture_ptr         =
208     s->next_picture_ptr         =
209     s->current_picture_ptr      = NULL;
210 
211     // init
212     if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
213         s->mb_height = (s->height + 31) / 32 * 2;
214     else
215         s->mb_height = (s->height + 15) / 16;
216 
217     if ((s->width || s->height) &&
218         (err = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
219         goto fail;
220 
221     /* set chroma shifts */
222     err = av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
223                                            &s->chroma_x_shift,
224                                            &s->chroma_y_shift);
225     if (err < 0)
226         goto fail;
227 
228     if ((err = ff_mpv_init_context_frame(s)))
229         goto fail;
230 
231     memset(s->thread_context, 0, sizeof(s->thread_context));
232     s->thread_context[0]   = s;
233 
234     if (s->width && s->height) {
235         err = ff_mpv_init_duplicate_contexts(s);
236         if (err < 0)
237             goto fail;
238     }
239     s->context_reinit = 0;
240 
241     return 0;
242  fail:
243     ff_mpv_free_context_frame(s);
244     s->context_reinit = 1;
245     return err;
246 }
247 
alloc_picture(MpegEncContext * s,Picture * pic)248 static int alloc_picture(MpegEncContext *s, Picture *pic)
249 {
250     return ff_alloc_picture(s->avctx, pic, &s->me, &s->sc, 0, 0,
251                             s->chroma_x_shift, s->chroma_y_shift, s->out_format,
252                             s->mb_stride, s->mb_width, s->mb_height, s->b8_stride,
253                             &s->linesize, &s->uvlinesize);
254 }
255 
gray_frame(AVFrame * frame)256 static void gray_frame(AVFrame *frame)
257 {
258     int h_chroma_shift, v_chroma_shift;
259 
260     av_pix_fmt_get_chroma_sub_sample(frame->format, &h_chroma_shift, &v_chroma_shift);
261 
262     for (int i = 0; i < frame->height; i++)
263         memset(frame->data[0] + frame->linesize[0] * i, 0x80, frame->width);
264     for (int i = 0; i < AV_CEIL_RSHIFT(frame->height, v_chroma_shift); i++) {
265         memset(frame->data[1] + frame->linesize[1] * i,
266                0x80, AV_CEIL_RSHIFT(frame->width, h_chroma_shift));
267         memset(frame->data[2] + frame->linesize[2] * i,
268                0x80, AV_CEIL_RSHIFT(frame->width, h_chroma_shift));
269     }
270 }
271 
272 /**
273  * generic function called after decoding
274  * the header and before a frame is decoded.
275  */
ff_mpv_frame_start(MpegEncContext * s,AVCodecContext * avctx)276 int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx)
277 {
278     Picture *pic;
279     int idx, ret;
280 
281     s->mb_skipped = 0;
282 
283     if (!ff_thread_can_start_frame(avctx)) {
284         av_log(avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
285         return -1;
286     }
287 
288     /* mark & release old frames */
289     if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
290         s->last_picture_ptr != s->next_picture_ptr &&
291         s->last_picture_ptr->f->buf[0]) {
292         ff_mpeg_unref_picture(s->avctx, s->last_picture_ptr);
293     }
294 
295     /* release forgotten pictures */
296     /* if (MPEG-124 / H.263) */
297     for (int i = 0; i < MAX_PICTURE_COUNT; i++) {
298         if (&s->picture[i] != s->last_picture_ptr &&
299             &s->picture[i] != s->next_picture_ptr &&
300             s->picture[i].reference && !s->picture[i].needs_realloc) {
301             ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
302         }
303     }
304 
305     ff_mpeg_unref_picture(s->avctx, &s->current_picture);
306     ff_mpeg_unref_picture(s->avctx, &s->last_picture);
307     ff_mpeg_unref_picture(s->avctx, &s->next_picture);
308 
309     /* release non reference frames */
310     for (int i = 0; i < MAX_PICTURE_COUNT; i++) {
311         if (!s->picture[i].reference)
312             ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
313     }
314 
315     if (s->current_picture_ptr && !s->current_picture_ptr->f->buf[0]) {
316         // we already have an unused image
317         // (maybe it was set before reading the header)
318         pic = s->current_picture_ptr;
319     } else {
320         idx = ff_find_unused_picture(s->avctx, s->picture, 0);
321         if (idx < 0) {
322             av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
323             return idx;
324         }
325         pic = &s->picture[idx];
326     }
327 
328     pic->reference = 0;
329     if (!s->droppable) {
330         if (s->pict_type != AV_PICTURE_TYPE_B)
331             pic->reference = 3;
332     }
333 
334     pic->f->coded_picture_number = s->coded_picture_number++;
335 
336     if (alloc_picture(s, pic) < 0)
337         return -1;
338 
339     s->current_picture_ptr = pic;
340     // FIXME use only the vars from current_pic
341     s->current_picture_ptr->f->top_field_first = s->top_field_first;
342     if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
343         s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
344         if (s->picture_structure != PICT_FRAME)
345             s->current_picture_ptr->f->top_field_first =
346                 (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
347     }
348     s->current_picture_ptr->f->interlaced_frame = !s->progressive_frame &&
349                                                  !s->progressive_sequence;
350     s->current_picture_ptr->field_picture      =  s->picture_structure != PICT_FRAME;
351 
352     s->current_picture_ptr->f->pict_type = s->pict_type;
353     s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
354 
355     if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
356                                    s->current_picture_ptr)) < 0)
357         return ret;
358 
359     if (s->pict_type != AV_PICTURE_TYPE_B) {
360         s->last_picture_ptr = s->next_picture_ptr;
361         if (!s->droppable)
362             s->next_picture_ptr = s->current_picture_ptr;
363     }
364     ff_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
365             s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
366             s->last_picture_ptr    ? s->last_picture_ptr->f->data[0]    : NULL,
367             s->next_picture_ptr    ? s->next_picture_ptr->f->data[0]    : NULL,
368             s->current_picture_ptr ? s->current_picture_ptr->f->data[0] : NULL,
369             s->pict_type, s->droppable);
370 
371     if ((!s->last_picture_ptr || !s->last_picture_ptr->f->buf[0]) &&
372         (s->pict_type != AV_PICTURE_TYPE_I)) {
373         int h_chroma_shift, v_chroma_shift;
374         av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
375                                          &h_chroma_shift, &v_chroma_shift);
376         if (s->pict_type == AV_PICTURE_TYPE_B && s->next_picture_ptr && s->next_picture_ptr->f->buf[0])
377             av_log(avctx, AV_LOG_DEBUG,
378                    "allocating dummy last picture for B frame\n");
379         else if (s->pict_type != AV_PICTURE_TYPE_I)
380             av_log(avctx, AV_LOG_ERROR,
381                    "warning: first frame is no keyframe\n");
382 
383         /* Allocate a dummy frame */
384         idx = ff_find_unused_picture(s->avctx, s->picture, 0);
385         if (idx < 0) {
386             av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
387             return idx;
388         }
389         s->last_picture_ptr = &s->picture[idx];
390 
391         s->last_picture_ptr->reference    = 3;
392         s->last_picture_ptr->f->key_frame = 0;
393         s->last_picture_ptr->f->pict_type = AV_PICTURE_TYPE_P;
394 
395         if (alloc_picture(s, s->last_picture_ptr) < 0) {
396             s->last_picture_ptr = NULL;
397             return -1;
398         }
399 
400         if (!avctx->hwaccel) {
401             for (int i = 0; i < avctx->height; i++)
402                 memset(s->last_picture_ptr->f->data[0] + s->last_picture_ptr->f->linesize[0]*i,
403                        0x80, avctx->width);
404             if (s->last_picture_ptr->f->data[2]) {
405                 for (int i = 0; i < AV_CEIL_RSHIFT(avctx->height, v_chroma_shift); i++) {
406                     memset(s->last_picture_ptr->f->data[1] + s->last_picture_ptr->f->linesize[1]*i,
407                         0x80, AV_CEIL_RSHIFT(avctx->width, h_chroma_shift));
408                     memset(s->last_picture_ptr->f->data[2] + s->last_picture_ptr->f->linesize[2]*i,
409                         0x80, AV_CEIL_RSHIFT(avctx->width, h_chroma_shift));
410                 }
411             }
412 
413             if (s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263) {
414                 for (int i = 0; i < avctx->height; i++)
415                     memset(s->last_picture_ptr->f->data[0] + s->last_picture_ptr->f->linesize[0] * i,
416                            16, avctx->width);
417             }
418         }
419 
420         ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
421         ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
422     }
423     if ((!s->next_picture_ptr || !s->next_picture_ptr->f->buf[0]) &&
424         s->pict_type == AV_PICTURE_TYPE_B) {
425         /* Allocate a dummy frame */
426         idx = ff_find_unused_picture(s->avctx, s->picture, 0);
427         if (idx < 0) {
428             av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
429             return idx;
430         }
431         s->next_picture_ptr = &s->picture[idx];
432 
433         s->next_picture_ptr->reference   = 3;
434         s->next_picture_ptr->f->key_frame = 0;
435         s->next_picture_ptr->f->pict_type = AV_PICTURE_TYPE_P;
436 
437         if (alloc_picture(s, s->next_picture_ptr) < 0) {
438             s->next_picture_ptr = NULL;
439             return -1;
440         }
441         ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
442         ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
443     }
444 
445 #if 0 // BUFREF-FIXME
446     memset(s->last_picture.f->data, 0, sizeof(s->last_picture.f->data));
447     memset(s->next_picture.f->data, 0, sizeof(s->next_picture.f->data));
448 #endif
449     if (s->last_picture_ptr) {
450         if (s->last_picture_ptr->f->buf[0] &&
451             (ret = ff_mpeg_ref_picture(s->avctx, &s->last_picture,
452                                        s->last_picture_ptr)) < 0)
453             return ret;
454     }
455     if (s->next_picture_ptr) {
456         if (s->next_picture_ptr->f->buf[0] &&
457             (ret = ff_mpeg_ref_picture(s->avctx, &s->next_picture,
458                                        s->next_picture_ptr)) < 0)
459             return ret;
460     }
461 
462     av_assert0(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
463                                                  s->last_picture_ptr->f->buf[0]));
464 
465     if (s->picture_structure != PICT_FRAME) {
466         for (int i = 0; i < 4; i++) {
467             if (s->picture_structure == PICT_BOTTOM_FIELD) {
468                 s->current_picture.f->data[i] +=
469                     s->current_picture.f->linesize[i];
470             }
471             s->current_picture.f->linesize[i] *= 2;
472             s->last_picture.f->linesize[i]    *= 2;
473             s->next_picture.f->linesize[i]    *= 2;
474         }
475     }
476 
477     /* set dequantizer, we can't do it during init as
478      * it might change for MPEG-4 and we can't do it in the header
479      * decode as init is not called for MPEG-4 there yet */
480     if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
481         s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
482         s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
483     } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
484         s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
485         s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
486     } else {
487         s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
488         s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
489     }
490 
491     if (s->avctx->debug & FF_DEBUG_NOMC)
492         gray_frame(s->current_picture_ptr->f);
493 
494     return 0;
495 }
496 
497 /* called after a frame has been decoded. */
ff_mpv_frame_end(MpegEncContext * s)498 void ff_mpv_frame_end(MpegEncContext *s)
499 {
500     emms_c();
501 
502     if (s->current_picture.reference)
503         ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
504 }
505 
ff_print_debug_info(MpegEncContext * s,Picture * p,AVFrame * pict)506 void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict)
507 {
508     ff_print_debug_info2(s->avctx, pict, s->mbskip_table, p->mb_type,
509                          p->qscale_table, p->motion_val,
510                          s->mb_width, s->mb_height, s->mb_stride, s->quarter_sample);
511 }
512 
ff_mpv_export_qp_table(MpegEncContext * s,AVFrame * f,Picture * p,int qp_type)513 int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type)
514 {
515     AVVideoEncParams *par;
516     int mult = (qp_type == FF_MPV_QSCALE_TYPE_MPEG1) ? 2 : 1;
517     unsigned int nb_mb = p->alloc_mb_height * p->alloc_mb_width;
518 
519     if (!(s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS))
520         return 0;
521 
522     par = av_video_enc_params_create_side_data(f, AV_VIDEO_ENC_PARAMS_MPEG2, nb_mb);
523     if (!par)
524         return AVERROR(ENOMEM);
525 
526     for (unsigned y = 0; y < p->alloc_mb_height; y++)
527         for (unsigned x = 0; x < p->alloc_mb_width; x++) {
528             const unsigned int block_idx = y * p->alloc_mb_width + x;
529             const unsigned int     mb_xy = y * p->alloc_mb_stride + x;
530             AVVideoBlockParams *const b = av_video_enc_params_block(par, block_idx);
531 
532             b->src_x = x * 16;
533             b->src_y = y * 16;
534             b->w     = 16;
535             b->h     = 16;
536 
537             b->delta_qp = p->qscale_table[mb_xy] * mult;
538         }
539 
540     return 0;
541 }
542 
ff_mpeg_draw_horiz_band(MpegEncContext * s,int y,int h)543 void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
544 {
545     ff_draw_horiz_band(s->avctx, s->current_picture_ptr->f,
546                        s->last_picture_ptr ? s->last_picture_ptr->f : NULL,
547                        y, h, s->picture_structure,
548                        s->first_field, s->low_delay);
549 }
550 
ff_mpeg_flush(AVCodecContext * avctx)551 void ff_mpeg_flush(AVCodecContext *avctx)
552 {
553     MpegEncContext *const s = avctx->priv_data;
554 
555     if (!s->picture)
556         return;
557 
558     for (int i = 0; i < MAX_PICTURE_COUNT; i++)
559         ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
560     s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
561 
562     ff_mpeg_unref_picture(s->avctx, &s->current_picture);
563     ff_mpeg_unref_picture(s->avctx, &s->last_picture);
564     ff_mpeg_unref_picture(s->avctx, &s->next_picture);
565 
566     s->mb_x = s->mb_y = 0;
567 
568 #if FF_API_FLAG_TRUNCATED
569     s->parse_context.state = -1;
570     s->parse_context.frame_start_found = 0;
571     s->parse_context.overread = 0;
572     s->parse_context.overread_index = 0;
573     s->parse_context.index = 0;
574     s->parse_context.last_index = 0;
575 #endif
576     s->bitstream_buffer_size = 0;
577     s->pp_time = 0;
578 }
579 
ff_mpv_report_decode_progress(MpegEncContext * s)580 void ff_mpv_report_decode_progress(MpegEncContext *s)
581 {
582     if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
583         ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
584 }
585