FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo_dec.c
Date: 2024-02-29 09:57:37
Exec Total Coverage
Lines: 391 551 71.0%
Functions: 19 19 100.0%
Branches: 212 370 57.3%

Line Branch Exec Source
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 "config_components.h"
26
27 #include "libavutil/avassert.h"
28 #include "libavutil/emms.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/internal.h"
31 #include "libavutil/video_enc_params.h"
32
33 #include "avcodec.h"
34 #include "decode.h"
35 #include "h264chroma.h"
36 #include "internal.h"
37 #include "mpegutils.h"
38 #include "mpegvideo.h"
39 #include "mpegvideodec.h"
40 #include "mpeg4videodec.h"
41 #include "thread.h"
42 #include "threadframe.h"
43 #include "wmv2dec.h"
44
45 625 void ff_mpv_decode_init(MpegEncContext *s, AVCodecContext *avctx)
46 {
47 625 ff_mpv_common_defaults(s);
48
49 625 s->avctx = avctx;
50 625 s->width = avctx->coded_width;
51 625 s->height = avctx->coded_height;
52 625 s->codec_id = avctx->codec->id;
53 625 s->workaround_bugs = avctx->workaround_bugs;
54
55 /* convert fourcc to upper case */
56 625 s->codec_tag = ff_toupper4(avctx->codec_tag);
57
58 625 ff_mpv_idct_init(s);
59 625 ff_h264chroma_init(&s->h264chroma, 8); //for lowres
60 625 }
61
62 18 int ff_mpeg_update_thread_context(AVCodecContext *dst,
63 const AVCodecContext *src)
64 {
65 18 MpegEncContext *const s1 = src->priv_data;
66 18 MpegEncContext *const s = dst->priv_data;
67 int ret;
68
69
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (dst == src)
70 return 0;
71
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 av_assert0(s != s1);
73
74 // FIXME can parameters change on I-frames?
75 // in that case dst may need a reinit
76
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 10 times.
18 if (!s->context_initialized) {
77 8 void *private_ctx = s->private_ctx;
78 int err;
79 8 memcpy(s, s1, sizeof(*s));
80
81 8 s->context_initialized = 0;
82 8 s->context_reinit = 0;
83 8 s->avctx = dst;
84 8 s->private_ctx = private_ctx;
85 8 s->bitstream_buffer = NULL;
86 8 s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
87
88
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (s1->context_initialized) {
89
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 if ((err = ff_mpv_common_init(s)) < 0)
90 return err;
91 }
92 }
93
94
3/6
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 18 times.
18 if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
95 s->height = s1->height;
96 s->width = s1->width;
97 if ((ret = ff_mpv_common_frame_size_change(s)) < 0)
98 return ret;
99 }
100
101 18 s->quarter_sample = s1->quarter_sample;
102
103 18 s->coded_picture_number = s1->coded_picture_number;
104 18 s->picture_number = s1->picture_number;
105
106
2/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
18 av_assert0(!s->picture || s->picture != s1->picture);
107
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (s->picture)
108
2/2
✓ Branch 0 taken 648 times.
✓ Branch 1 taken 18 times.
666 for (int i = 0; i < MAX_PICTURE_COUNT; i++) {
109 648 ff_mpeg_unref_picture(&s->picture[i]);
110
4/6
✓ Branch 0 taken 648 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 35 times.
✓ Branch 3 taken 613 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 35 times.
683 if (s1->picture && s1->picture[i].f->buf[0] &&
111 35 (ret = ff_mpeg_ref_picture(&s->picture[i], &s1->picture[i])) < 0)
112 return ret;
113 }
114
115 #define UPDATE_PICTURE(pic)\
116 do {\
117 ff_mpeg_unref_picture(&s->pic);\
118 if (s1->pic.f && s1->pic.f->buf[0])\
119 ret = ff_mpeg_ref_picture(&s->pic, &s1->pic);\
120 else\
121 ret = ff_update_picture_tables(&s->pic, &s1->pic);\
122 if (ret < 0)\
123 return ret;\
124 } while (0)
125
126
3/6
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
✗ Branch 4 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 18 times.
18 UPDATE_PICTURE(current_picture);
127
4/6
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 17 times.
✓ Branch 4 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 18 times.
18 UPDATE_PICTURE(last_picture);
128
3/6
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
✗ Branch 4 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 18 times.
18 UPDATE_PICTURE(next_picture);
129
130 #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
131 ((pic && pic >= old_ctx->picture && \
132 pic < old_ctx->picture + MAX_PICTURE_COUNT) ? \
133 &new_ctx->picture[pic - old_ctx->picture] : NULL)
134
135
4/6
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 17 times.
✗ Branch 5 not taken.
18 s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
136
3/6
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 18 times.
✗ Branch 5 not taken.
18 s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
137
3/6
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 18 times.
✗ Branch 5 not taken.
18 s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
138
139 // Error/bug resilience
140 18 s->workaround_bugs = s1->workaround_bugs;
141 18 s->padding_bug_score = s1->padding_bug_score;
142
143 // MPEG-4 timing info
144 18 memcpy(&s->last_time_base, &s1->last_time_base,
145 18 (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) -
146 18 (char *) &s1->last_time_base);
147
148 // B-frame info
149 18 s->max_b_frames = s1->max_b_frames;
150 18 s->low_delay = s1->low_delay;
151 18 s->droppable = s1->droppable;
152
153 // DivX handling (doesn't work)
154 18 s->divx_packed = s1->divx_packed;
155
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (s1->bitstream_buffer) {
157 av_fast_padded_malloc(&s->bitstream_buffer,
158 &s->allocated_bitstream_buffer_size,
159 s1->bitstream_buffer_size);
160 if (!s->bitstream_buffer) {
161 s->bitstream_buffer_size = 0;
162 return AVERROR(ENOMEM);
163 }
164 s->bitstream_buffer_size = s1->bitstream_buffer_size;
165 memcpy(s->bitstream_buffer, s1->bitstream_buffer,
166 s1->bitstream_buffer_size);
167 }
168
169 // linesize-dependent scratch buffer allocation
170
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 10 times.
18 if (!s->sc.edge_emu_buffer)
171
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (s1->linesize) {
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (ff_mpeg_framesize_alloc(s->avctx, &s->me,
173 8 &s->sc, s1->linesize) < 0) {
174 av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
175 "scratch buffers.\n");
176 return AVERROR(ENOMEM);
177 }
178 } else {
179 av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
180 "be allocated due to unknown size.\n");
181 }
182
183 // MPEG-2/interlacing info
184 18 memcpy(&s->progressive_sequence, &s1->progressive_sequence,
185 (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
186
187 18 return 0;
188 }
189
190 42 int ff_mpv_common_frame_size_change(MpegEncContext *s)
191 {
192 42 int err = 0;
193
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!s->context_initialized)
195 return AVERROR(EINVAL);
196
197 42 ff_mpv_free_context_frame(s);
198
199
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 if (s->picture)
200
2/2
✓ Branch 0 taken 1512 times.
✓ Branch 1 taken 42 times.
1554 for (int i = 0; i < MAX_PICTURE_COUNT; i++)
201 1512 s->picture[i].needs_realloc = 1;
202
203 42 s->last_picture_ptr =
204 42 s->next_picture_ptr =
205 42 s->current_picture_ptr = NULL;
206
207
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 42 times.
84 if ((s->width || s->height) &&
208 42 (err = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
209 goto fail;
210
211 /* set chroma shifts */
212 42 err = av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
213 &s->chroma_x_shift,
214 &s->chroma_y_shift);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (err < 0)
216 goto fail;
217
218
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
42 if ((err = ff_mpv_init_context_frame(s)))
219 goto fail;
220
221 42 memset(s->thread_context, 0, sizeof(s->thread_context));
222 42 s->thread_context[0] = s;
223
224
2/4
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
42 if (s->width && s->height) {
225 42 err = ff_mpv_init_duplicate_contexts(s);
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (err < 0)
227 goto fail;
228 }
229 42 s->context_reinit = 0;
230
231 42 return 0;
232 fail:
233 ff_mpv_free_context_frame(s);
234 s->context_reinit = 1;
235 return err;
236 }
237
238 11174 static int alloc_picture(MpegEncContext *s, Picture *pic)
239 {
240 11174 AVCodecContext *avctx = s->avctx;
241 int ret;
242
243 11174 pic->tf.f = pic->f;
244
245 /* WM Image / Screen codecs allocate internal buffers with different
246 * dimensions / colorspaces; ignore user-defined callbacks for these. */
247
1/2
✓ Branch 0 taken 11174 times.
✗ Branch 1 not taken.
11174 if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE &&
248
1/2
✓ Branch 0 taken 11174 times.
✗ Branch 1 not taken.
11174 avctx->codec_id != AV_CODEC_ID_VC1IMAGE &&
249
2/2
✓ Branch 0 taken 11092 times.
✓ Branch 1 taken 82 times.
11174 avctx->codec_id != AV_CODEC_ID_MSS2) {
250 11092 ret = ff_thread_get_ext_buffer(avctx, &pic->tf,
251 11092 pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
252 } else {
253 82 pic->f->width = avctx->width;
254 82 pic->f->height = avctx->height;
255 82 pic->f->format = avctx->pix_fmt;
256 82 ret = avcodec_default_get_buffer2(avctx, pic->f, 0);
257 }
258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11174 times.
11174 if (ret < 0)
259 goto fail;
260
261 11174 ret = ff_hwaccel_frame_priv_alloc(avctx, &pic->hwaccel_picture_private);
262
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11174 times.
11174 if (ret < 0)
263 goto fail;
264
265 11174 return ff_alloc_picture(s->avctx, pic, &s->me, &s->sc, 0, s->out_format,
266 s->mb_stride, s->mb_width, s->mb_height, s->b8_stride,
267 &s->linesize, &s->uvlinesize);
268 fail:
269 ff_mpeg_unref_picture(pic);
270 return ret;
271 }
272
273 6 static void color_frame(AVFrame *frame, int luma)
274 {
275 int h_chroma_shift, v_chroma_shift;
276
277
2/2
✓ Branch 0 taken 1728 times.
✓ Branch 1 taken 6 times.
1734 for (int i = 0; i < frame->height; i++)
278 1728 memset(frame->data[0] + frame->linesize[0] * i, luma, frame->width);
279
280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!frame->data[1])
281 return;
282 6 av_pix_fmt_get_chroma_sub_sample(frame->format, &h_chroma_shift, &v_chroma_shift);
283
2/2
✓ Branch 0 taken 864 times.
✓ Branch 1 taken 6 times.
870 for (int i = 0; i < AV_CEIL_RSHIFT(frame->height, v_chroma_shift); i++) {
284 864 memset(frame->data[1] + frame->linesize[1] * i,
285 864 0x80, AV_CEIL_RSHIFT(frame->width, h_chroma_shift));
286 864 memset(frame->data[2] + frame->linesize[2] * i,
287 864 0x80, AV_CEIL_RSHIFT(frame->width, h_chroma_shift));
288 }
289 }
290
291 /**
292 * generic function called after decoding
293 * the header and before a frame is decoded.
294 */
295 11168 int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx)
296 {
297 Picture *pic;
298 int idx, ret;
299
300 11168 s->mb_skipped = 0;
301
302
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11168 times.
11168 if (!ff_thread_can_start_frame(avctx)) {
303 av_log(avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
304 return -1;
305 }
306
307 /* mark & release old frames */
308
4/4
✓ Branch 0 taken 8495 times.
✓ Branch 1 taken 2673 times.
✓ Branch 2 taken 7965 times.
✓ Branch 3 taken 530 times.
11168 if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
309
1/2
✓ Branch 0 taken 7965 times.
✗ Branch 1 not taken.
7965 s->last_picture_ptr != s->next_picture_ptr &&
310
1/2
✓ Branch 0 taken 7965 times.
✗ Branch 1 not taken.
7965 s->last_picture_ptr->f->buf[0]) {
311 7965 ff_mpeg_unref_picture(s->last_picture_ptr);
312 }
313
314 /* release non reference/forgotten frames */
315
2/2
✓ Branch 0 taken 402048 times.
✓ Branch 1 taken 11168 times.
413216 for (int i = 0; i < MAX_PICTURE_COUNT; i++) {
316
2/2
✓ Branch 0 taken 13553 times.
✓ Branch 1 taken 388495 times.
402048 if (!s->picture[i].reference ||
317
2/2
✓ Branch 0 taken 10880 times.
✓ Branch 1 taken 2673 times.
13553 (&s->picture[i] != s->last_picture_ptr &&
318
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 10850 times.
10880 &s->picture[i] != s->next_picture_ptr &&
319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 !s->picture[i].needs_realloc)) {
320 388495 ff_mpeg_unref_picture(&s->picture[i]);
321 }
322 }
323
324 11168 ff_mpeg_unref_picture(&s->current_picture);
325 11168 ff_mpeg_unref_picture(&s->last_picture);
326 11168 ff_mpeg_unref_picture(&s->next_picture);
327
328
4/4
✓ Branch 0 taken 6697 times.
✓ Branch 1 taken 4471 times.
✓ Branch 2 taken 1175 times.
✓ Branch 3 taken 5522 times.
11168 if (s->current_picture_ptr && !s->current_picture_ptr->f->buf[0]) {
329 // we already have an unused image
330 // (maybe it was set before reading the header)
331 1175 pic = s->current_picture_ptr;
332 } else {
333 9993 idx = ff_find_unused_picture(s->avctx, s->picture, 0);
334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9993 times.
9993 if (idx < 0) {
335 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
336 return idx;
337 }
338 9993 pic = &s->picture[idx];
339 }
340
341 11168 pic->reference = 0;
342
1/2
✓ Branch 0 taken 11168 times.
✗ Branch 1 not taken.
11168 if (!s->droppable) {
343
2/2
✓ Branch 0 taken 8495 times.
✓ Branch 1 taken 2673 times.
11168 if (s->pict_type != AV_PICTURE_TYPE_B)
344 8495 pic->reference = 3;
345 }
346
347 #if FF_API_FRAME_PICTURE_NUMBER
348 FF_DISABLE_DEPRECATION_WARNINGS
349 11168 pic->f->coded_picture_number = s->coded_picture_number++;
350 FF_ENABLE_DEPRECATION_WARNINGS
351 #endif
352
353
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11168 times.
11168 if (alloc_picture(s, pic) < 0)
354 return -1;
355
356 11168 s->current_picture_ptr = pic;
357 // FIXME use only the vars from current_pic
358
2/2
✓ Branch 0 taken 779 times.
✓ Branch 1 taken 10389 times.
11168 s->current_picture_ptr->f->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST * !!s->top_field_first;
359
2/2
✓ Branch 0 taken 10688 times.
✓ Branch 1 taken 480 times.
11168 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
360
2/2
✓ Branch 0 taken 3371 times.
✓ Branch 1 taken 7317 times.
10688 s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
361
2/2
✓ Branch 0 taken 712 times.
✓ Branch 1 taken 3139 times.
3851 if (s->picture_structure != PICT_FRAME)
362 712 s->current_picture_ptr->f->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST *
363
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 710 times.
712 ((s->picture_structure == PICT_TOP_FIELD) == s->first_field);
364 }
365
2/2
✓ Branch 0 taken 2287 times.
✓ Branch 1 taken 8881 times.
13455 s->current_picture_ptr->f->flags |= AV_FRAME_FLAG_INTERLACED * (!s->progressive_frame &&
366
1/2
✓ Branch 0 taken 2287 times.
✗ Branch 1 not taken.
2287 !s->progressive_sequence);
367 11168 s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
368
369 11168 s->current_picture_ptr->f->pict_type = s->pict_type;
370
2/2
✓ Branch 0 taken 1232 times.
✓ Branch 1 taken 9936 times.
11168 if (s->pict_type == AV_PICTURE_TYPE_I)
371 1232 s->current_picture_ptr->f->flags |= AV_FRAME_FLAG_KEY;
372 else
373 9936 s->current_picture_ptr->f->flags &= ~AV_FRAME_FLAG_KEY;
374
375
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11168 times.
11168 if ((ret = ff_mpeg_ref_picture(&s->current_picture,
376 s->current_picture_ptr)) < 0)
377 return ret;
378
379
2/2
✓ Branch 0 taken 8495 times.
✓ Branch 1 taken 2673 times.
11168 if (s->pict_type != AV_PICTURE_TYPE_B) {
380 8495 s->last_picture_ptr = s->next_picture_ptr;
381
1/2
✓ Branch 0 taken 8495 times.
✗ Branch 1 not taken.
8495 if (!s->droppable)
382 8495 s->next_picture_ptr = s->current_picture_ptr;
383 }
384 ff_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
385 s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
386 s->last_picture_ptr ? s->last_picture_ptr->f->data[0] : NULL,
387 s->next_picture_ptr ? s->next_picture_ptr->f->data[0] : NULL,
388 s->current_picture_ptr ? s->current_picture_ptr->f->data[0] : NULL,
389 s->pict_type, s->droppable);
390
391
3/4
✓ Branch 0 taken 10850 times.
✓ Branch 1 taken 318 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10850 times.
11168 if ((!s->last_picture_ptr || !s->last_picture_ptr->f->buf[0]) &&
392
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 312 times.
318 (s->pict_type != AV_PICTURE_TYPE_I)) {
393
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
6 if (s->pict_type == AV_PICTURE_TYPE_B && s->next_picture_ptr && s->next_picture_ptr->f->buf[0])
394 av_log(avctx, AV_LOG_DEBUG,
395 "allocating dummy last picture for B frame\n");
396
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 else if (s->pict_type != AV_PICTURE_TYPE_I)
397 6 av_log(avctx, AV_LOG_ERROR,
398 "warning: first frame is no keyframe\n");
399
400 /* Allocate a dummy frame */
401 6 idx = ff_find_unused_picture(s->avctx, s->picture, 0);
402
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (idx < 0) {
403 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
404 return idx;
405 }
406 6 s->last_picture_ptr = &s->picture[idx];
407
408 6 s->last_picture_ptr->reference = 3;
409 6 s->last_picture_ptr->f->flags &= ~AV_FRAME_FLAG_KEY;
410 6 s->last_picture_ptr->f->pict_type = AV_PICTURE_TYPE_P;
411
412
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (alloc_picture(s, s->last_picture_ptr) < 0) {
413 s->last_picture_ptr = NULL;
414 return -1;
415 }
416
417
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (!avctx->hwaccel) {
418
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 int luma_val = s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263 ? 16 : 0x80;
419 6 color_frame(s->last_picture_ptr->f, luma_val);
420 }
421
422 6 ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
423 6 ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
424 }
425
2/4
✓ Branch 0 taken 11168 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11168 times.
11168 if ((!s->next_picture_ptr || !s->next_picture_ptr->f->buf[0]) &&
426 s->pict_type == AV_PICTURE_TYPE_B) {
427 /* Allocate a dummy frame */
428 idx = ff_find_unused_picture(s->avctx, s->picture, 0);
429 if (idx < 0) {
430 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
431 return idx;
432 }
433 s->next_picture_ptr = &s->picture[idx];
434
435 s->next_picture_ptr->reference = 3;
436 s->next_picture_ptr->f->flags &= ~AV_FRAME_FLAG_KEY;
437 s->next_picture_ptr->f->pict_type = AV_PICTURE_TYPE_P;
438
439 if (alloc_picture(s, s->next_picture_ptr) < 0) {
440 s->next_picture_ptr = NULL;
441 return -1;
442 }
443 ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
444 ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
445 }
446
447
2/2
✓ Branch 0 taken 10856 times.
✓ Branch 1 taken 312 times.
11168 if (s->last_picture_ptr) {
448
2/4
✓ Branch 0 taken 10856 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10856 times.
21712 if (s->last_picture_ptr->f->buf[0] &&
449 10856 (ret = ff_mpeg_ref_picture(&s->last_picture,
450 s->last_picture_ptr)) < 0)
451 return ret;
452 }
453
1/2
✓ Branch 0 taken 11168 times.
✗ Branch 1 not taken.
11168 if (s->next_picture_ptr) {
454
2/4
✓ Branch 0 taken 11168 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11168 times.
22336 if (s->next_picture_ptr->f->buf[0] &&
455 11168 (ret = ff_mpeg_ref_picture(&s->next_picture,
456 s->next_picture_ptr)) < 0)
457 return ret;
458 }
459
460
4/6
✓ Branch 0 taken 9936 times.
✓ Branch 1 taken 1232 times.
✓ Branch 2 taken 9936 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 9936 times.
11168 av_assert0(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
461 s->last_picture_ptr->f->buf[0]));
462
463
2/2
✓ Branch 0 taken 712 times.
✓ Branch 1 taken 10456 times.
11168 if (s->picture_structure != PICT_FRAME) {
464
2/2
✓ Branch 0 taken 2848 times.
✓ Branch 1 taken 712 times.
3560 for (int i = 0; i < 4; i++) {
465
2/2
✓ Branch 0 taken 2840 times.
✓ Branch 1 taken 8 times.
2848 if (s->picture_structure == PICT_BOTTOM_FIELD) {
466
2/2
✓ Branch 0 taken 2130 times.
✓ Branch 1 taken 710 times.
2840 s->current_picture.f->data[i] = FF_PTR_ADD(s->current_picture.f->data[i],
467 s->current_picture.f->linesize[i]);
468 }
469 2848 s->current_picture.f->linesize[i] *= 2;
470 2848 s->last_picture.f->linesize[i] *= 2;
471 2848 s->next_picture.f->linesize[i] *= 2;
472 }
473 }
474
475 /* set dequantizer, we can't do it during init as
476 * it might change for MPEG-4 and we can't do it in the header
477 * decode as init is not called for MPEG-4 there yet */
478
4/4
✓ Branch 0 taken 11128 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 3371 times.
✓ Branch 3 taken 7757 times.
11168 if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
479 3411 s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
480 3411 s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
481
4/4
✓ Branch 0 taken 780 times.
✓ Branch 1 taken 6977 times.
✓ Branch 2 taken 300 times.
✓ Branch 3 taken 480 times.
7757 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
482 7277 s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
483 7277 s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
484 } else {
485 480 s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
486 480 s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
487 }
488
489
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11168 times.
11168 if (s->avctx->debug & FF_DEBUG_NOMC)
490 color_frame(s->current_picture_ptr->f, 0x80);
491
492 11168 return 0;
493 }
494
495 /* called after a frame has been decoded. */
496 11168 void ff_mpv_frame_end(MpegEncContext *s)
497 {
498 11168 emms_c();
499
500
2/2
✓ Branch 0 taken 8495 times.
✓ Branch 1 taken 2673 times.
11168 if (s->current_picture.reference)
501 8495 ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
502 11168 }
503
504 10936 void ff_print_debug_info(const MpegEncContext *s, const Picture *p, AVFrame *pict)
505 {
506 10936 ff_print_debug_info2(s->avctx, pict, s->mbskip_table, p->mb_type,
507 10936 p->qscale_table, p->motion_val,
508 10936 s->mb_width, s->mb_height, s->mb_stride, s->quarter_sample);
509 10936 }
510
511 10199 int ff_mpv_export_qp_table(const MpegEncContext *s, AVFrame *f, const Picture *p, int qp_type)
512 {
513 AVVideoEncParams *par;
514
2/2
✓ Branch 0 taken 6409 times.
✓ Branch 1 taken 3790 times.
10199 int mult = (qp_type == FF_MPV_QSCALE_TYPE_MPEG1) ? 2 : 1;
515 10199 unsigned int nb_mb = p->alloc_mb_height * p->alloc_mb_width;
516
517
2/2
✓ Branch 0 taken 10157 times.
✓ Branch 1 taken 42 times.
10199 if (!(s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS))
518 10157 return 0;
519
520 42 par = av_video_enc_params_create_side_data(f, AV_VIDEO_ENC_PARAMS_MPEG2, nb_mb);
521
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!par)
522 return AVERROR(ENOMEM);
523
524
2/2
✓ Branch 0 taken 756 times.
✓ Branch 1 taken 42 times.
798 for (unsigned y = 0; y < p->alloc_mb_height; y++)
525
2/2
✓ Branch 0 taken 16632 times.
✓ Branch 1 taken 756 times.
17388 for (unsigned x = 0; x < p->alloc_mb_width; x++) {
526 16632 const unsigned int block_idx = y * p->alloc_mb_width + x;
527 16632 const unsigned int mb_xy = y * p->alloc_mb_stride + x;
528 16632 AVVideoBlockParams *const b = av_video_enc_params_block(par, block_idx);
529
530 16632 b->src_x = x * 16;
531 16632 b->src_y = y * 16;
532 16632 b->w = 16;
533 16632 b->h = 16;
534
535 16632 b->delta_qp = p->qscale_table[mb_xy] * mult;
536 }
537
538 42 return 0;
539 }
540
541 169208 void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
542 {
543 169208 ff_draw_horiz_band(s->avctx, s->current_picture_ptr->f,
544
2/2
✓ Branch 0 taken 165531 times.
✓ Branch 1 taken 3677 times.
169208 s->last_picture_ptr ? s->last_picture_ptr->f : NULL,
545 y, h, s->picture_structure,
546 s->first_field, s->low_delay);
547 169208 }
548
549 82 void ff_mpeg_flush(AVCodecContext *avctx)
550 {
551 82 MpegEncContext *const s = avctx->priv_data;
552
553
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 82 times.
82 if (!s->picture)
554 return;
555
556
2/2
✓ Branch 0 taken 2952 times.
✓ Branch 1 taken 82 times.
3034 for (int i = 0; i < MAX_PICTURE_COUNT; i++)
557 2952 ff_mpeg_unref_picture(&s->picture[i]);
558 82 s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
559
560 82 ff_mpeg_unref_picture(&s->current_picture);
561 82 ff_mpeg_unref_picture(&s->last_picture);
562 82 ff_mpeg_unref_picture(&s->next_picture);
563
564 82 s->mb_x = s->mb_y = 0;
565
566 82 s->bitstream_buffer_size = 0;
567 82 s->pp_time = 0;
568 }
569
570 169208 void ff_mpv_report_decode_progress(MpegEncContext *s)
571 {
572
5/6
✓ Branch 0 taken 129418 times.
✓ Branch 1 taken 39790 times.
✓ Branch 2 taken 122835 times.
✓ Branch 3 taken 6583 times.
✓ Branch 4 taken 122835 times.
✗ Branch 5 not taken.
169208 if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
573 122835 ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
574 169208 }
575
576
577 126300 static inline int hpel_motion_lowres(MpegEncContext *s,
578 uint8_t *dest, const uint8_t *src,
579 int field_based, int field_select,
580 int src_x, int src_y,
581 int width, int height, ptrdiff_t stride,
582 int h_edge_pos, int v_edge_pos,
583 int w, int h, const h264_chroma_mc_func *pix_op,
584 int motion_x, int motion_y)
585 {
586 126300 const int lowres = s->avctx->lowres;
587 126300 const int op_index = FFMIN(lowres, 3);
588 126300 const int s_mask = (2 << lowres) - 1;
589 126300 int emu = 0;
590 int sx, sy;
591
592
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126300 times.
126300 if (s->quarter_sample) {
593 motion_x /= 2;
594 motion_y /= 2;
595 }
596
597 126300 sx = motion_x & s_mask;
598 126300 sy = motion_y & s_mask;
599 126300 src_x += motion_x >> lowres + 1;
600 126300 src_y += motion_y >> lowres + 1;
601
602 126300 src += src_y * stride + src_x;
603
604
1/2
✓ Branch 0 taken 126300 times.
✗ Branch 1 not taken.
126300 if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w, 0) ||
605
2/2
✓ Branch 0 taken 298 times.
✓ Branch 1 taken 126002 times.
126300 (unsigned)src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
606 298 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, src,
607 s->linesize, s->linesize,
608 298 w + 1, (h + 1) << field_based,
609 src_x, src_y * (1 << field_based),
610 h_edge_pos, v_edge_pos);
611 298 src = s->sc.edge_emu_buffer;
612 298 emu = 1;
613 }
614
615 126300 sx = (sx << 2) >> lowres;
616 126300 sy = (sy << 2) >> lowres;
617
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126300 times.
126300 if (field_select)
618 src += s->linesize;
619 126300 pix_op[op_index](dest, src, stride, h, sx, sy);
620 126300 return emu;
621 }
622
623 /* apply one mpeg motion vector to the three components */
624 24814 static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
625 uint8_t *dest_y,
626 uint8_t *dest_cb,
627 uint8_t *dest_cr,
628 int field_based,
629 int bottom_field,
630 int field_select,
631 uint8_t *const *ref_picture,
632 const h264_chroma_mc_func *pix_op,
633 int motion_x, int motion_y,
634 int h, int mb_y)
635 {
636 const uint8_t *ptr_y, *ptr_cb, *ptr_cr;
637 int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, sx, sy, uvsx, uvsy;
638 ptrdiff_t uvlinesize, linesize;
639 24814 const int lowres = s->avctx->lowres;
640 24814 const int op_index = FFMIN(lowres - 1 + s->chroma_x_shift, 3);
641 24814 const int block_s = 8 >> lowres;
642 24814 const int s_mask = (2 << lowres) - 1;
643 24814 const int h_edge_pos = s->h_edge_pos >> lowres;
644 24814 const int v_edge_pos = s->v_edge_pos >> lowres;
645
1/2
✓ Branch 0 taken 24814 times.
✗ Branch 1 not taken.
24814 int hc = s->chroma_y_shift ? (h+1-bottom_field)>>1 : h;
646 24814 linesize = s->current_picture.f->linesize[0] << field_based;
647 24814 uvlinesize = s->current_picture.f->linesize[1] << field_based;
648
649 // FIXME obviously not perfect but qpel will not work in lowres anyway
650
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24814 times.
24814 if (s->quarter_sample) {
651 motion_x /= 2;
652 motion_y /= 2;
653 }
654
655
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24814 times.
24814 if (field_based) {
656 motion_y += (bottom_field - field_select)*((1 << lowres)-1);
657 }
658
659 24814 sx = motion_x & s_mask;
660 24814 sy = motion_y & s_mask;
661 24814 src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);
662 24814 src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);
663
664
1/2
✓ Branch 0 taken 24814 times.
✗ Branch 1 not taken.
24814 if (s->out_format == FMT_H263) {
665 24814 uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
666 24814 uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
667 24814 uvsrc_x = src_x >> 1;
668 24814 uvsrc_y = src_y >> 1;
669 } else if (s->out_format == FMT_H261) {
670 // even chroma mv's are full pel in H261
671 mx = motion_x / 4;
672 my = motion_y / 4;
673 uvsx = (2 * mx) & s_mask;
674 uvsy = (2 * my) & s_mask;
675 uvsrc_x = s->mb_x * block_s + (mx >> lowres);
676 uvsrc_y = mb_y * block_s + (my >> lowres);
677 } else {
678 if (s->chroma_y_shift) {
679 mx = motion_x / 2;
680 my = motion_y / 2;
681 uvsx = mx & s_mask;
682 uvsy = my & s_mask;
683 uvsrc_x = s->mb_x * block_s + (mx >> lowres + 1);
684 uvsrc_y = (mb_y * block_s >> field_based) + (my >> lowres + 1);
685 } else {
686 if (s->chroma_x_shift) {
687 //Chroma422
688 mx = motion_x / 2;
689 uvsx = mx & s_mask;
690 uvsy = motion_y & s_mask;
691 uvsrc_y = src_y;
692 uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
693 } else {
694 //Chroma444
695 uvsx = motion_x & s_mask;
696 uvsy = motion_y & s_mask;
697 uvsrc_x = src_x;
698 uvsrc_y = src_y;
699 }
700 }
701 }
702
703 24814 ptr_y = ref_picture[0] + src_y * linesize + src_x;
704 24814 ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
705 24814 ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
706
707
3/4
✓ Branch 0 taken 24814 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24712 times.
✓ Branch 3 taken 102 times.
24814 if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s, 0) || uvsrc_y<0 ||
708
2/2
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 24617 times.
24712 (unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - FFMAX(h, hc<<s->chroma_y_shift), 0)) {
709 197 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr_y,
710 linesize >> field_based, linesize >> field_based,
711 17, 17 + field_based,
712 src_x, src_y * (1 << field_based), h_edge_pos,
713 v_edge_pos);
714 197 ptr_y = s->sc.edge_emu_buffer;
715 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
716 197 uint8_t *ubuf = s->sc.edge_emu_buffer + 18 * s->linesize;
717 197 uint8_t *vbuf =ubuf + 10 * s->uvlinesize;
718
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 197 times.
197 if (s->workaround_bugs & FF_BUG_IEDGE)
719 vbuf -= s->uvlinesize;
720 197 s->vdsp.emulated_edge_mc(ubuf, ptr_cb,
721 uvlinesize >> field_based, uvlinesize >> field_based,
722 9, 9 + field_based,
723 uvsrc_x, uvsrc_y * (1 << field_based),
724 h_edge_pos >> 1, v_edge_pos >> 1);
725 197 s->vdsp.emulated_edge_mc(vbuf, ptr_cr,
726 uvlinesize >> field_based,uvlinesize >> field_based,
727 9, 9 + field_based,
728 uvsrc_x, uvsrc_y * (1 << field_based),
729 h_edge_pos >> 1, v_edge_pos >> 1);
730 197 ptr_cb = ubuf;
731 197 ptr_cr = vbuf;
732 }
733 }
734
735 // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f->data
736
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24814 times.
24814 if (bottom_field) {
737 dest_y += s->linesize;
738 dest_cb += s->uvlinesize;
739 dest_cr += s->uvlinesize;
740 }
741
742
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24814 times.
24814 if (field_select) {
743 ptr_y += s->linesize;
744 ptr_cb += s->uvlinesize;
745 ptr_cr += s->uvlinesize;
746 }
747
748 24814 sx = (sx << 2) >> lowres;
749 24814 sy = (sy << 2) >> lowres;
750 24814 pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);
751
752 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
753 24814 uvsx = (uvsx << 2) >> lowres;
754 24814 uvsy = (uvsy << 2) >> lowres;
755
1/2
✓ Branch 0 taken 24814 times.
✗ Branch 1 not taken.
24814 if (hc) {
756 24814 pix_op[op_index](dest_cb, ptr_cb, uvlinesize, hc, uvsx, uvsy);
757 24814 pix_op[op_index](dest_cr, ptr_cr, uvlinesize, hc, uvsx, uvsy);
758 }
759 }
760 // FIXME h261 lowres loop filter
761 24814 }
762
763 31575 static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
764 uint8_t *dest_cb, uint8_t *dest_cr,
765 uint8_t *const *ref_picture,
766 const h264_chroma_mc_func * pix_op,
767 int mx, int my)
768 {
769 31575 const int lowres = s->avctx->lowres;
770 31575 const int op_index = FFMIN(lowres, 3);
771 31575 const int block_s = 8 >> lowres;
772 31575 const int s_mask = (2 << lowres) - 1;
773 31575 const int h_edge_pos = s->h_edge_pos >> lowres + 1;
774 31575 const int v_edge_pos = s->v_edge_pos >> lowres + 1;
775 31575 int emu = 0, src_x, src_y, sx, sy;
776 ptrdiff_t offset;
777 const uint8_t *ptr;
778
779
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31575 times.
31575 if (s->quarter_sample) {
780 mx /= 2;
781 my /= 2;
782 }
783
784 /* In case of 8X8, we construct a single chroma motion vector
785 with a special rounding */
786 31575 mx = ff_h263_round_chroma(mx);
787 31575 my = ff_h263_round_chroma(my);
788
789 31575 sx = mx & s_mask;
790 31575 sy = my & s_mask;
791 31575 src_x = s->mb_x * block_s + (mx >> lowres + 1);
792 31575 src_y = s->mb_y * block_s + (my >> lowres + 1);
793
794 31575 offset = src_y * s->uvlinesize + src_x;
795 31575 ptr = ref_picture[1] + offset;
796
1/2
✓ Branch 0 taken 31575 times.
✗ Branch 1 not taken.
31575 if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
797
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 31431 times.
31575 (unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
798 144 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
799 s->uvlinesize, s->uvlinesize,
800 9, 9,
801 src_x, src_y, h_edge_pos, v_edge_pos);
802 144 ptr = s->sc.edge_emu_buffer;
803 144 emu = 1;
804 }
805 31575 sx = (sx << 2) >> lowres;
806 31575 sy = (sy << 2) >> lowres;
807 31575 pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
808
809 31575 ptr = ref_picture[2] + offset;
810
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 31431 times.
31575 if (emu) {
811 144 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
812 s->uvlinesize, s->uvlinesize,
813 9, 9,
814 src_x, src_y, h_edge_pos, v_edge_pos);
815 144 ptr = s->sc.edge_emu_buffer;
816 }
817 31575 pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
818 31575 }
819
820 /**
821 * motion compensation of a single macroblock
822 * @param s context
823 * @param dest_y luma destination pointer
824 * @param dest_cb chroma cb/u destination pointer
825 * @param dest_cr chroma cr/v destination pointer
826 * @param dir direction (0->forward, 1->backward)
827 * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
828 * @param pix_op halfpel motion compensation function (average or put normally)
829 * the motion vectors are taken from s->mv and the MV type from s->mv_type
830 */
831 56389 static inline void MPV_motion_lowres(MpegEncContext *s,
832 uint8_t *dest_y, uint8_t *dest_cb,
833 uint8_t *dest_cr,
834 int dir, uint8_t *const *ref_picture,
835 const h264_chroma_mc_func *pix_op)
836 {
837 int mx, my;
838 int mb_x, mb_y;
839 56389 const int lowres = s->avctx->lowres;
840 56389 const int block_s = 8 >>lowres;
841
842 56389 mb_x = s->mb_x;
843 56389 mb_y = s->mb_y;
844
845
2/6
✓ Branch 0 taken 24814 times.
✓ Branch 1 taken 31575 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
56389 switch (s->mv_type) {
846 24814 case MV_TYPE_16X16:
847 24814 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
848 0, 0, 0,
849 ref_picture, pix_op,
850 s->mv[dir][0][0], s->mv[dir][0][1],
851 2 * block_s, mb_y);
852 24814 break;
853 31575 case MV_TYPE_8X8:
854 31575 mx = 0;
855 31575 my = 0;
856
2/2
✓ Branch 0 taken 126300 times.
✓ Branch 1 taken 31575 times.
157875 for (int i = 0; i < 4; i++) {
857 126300 hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) *
858 126300 s->linesize) * block_s,
859 ref_picture[0], 0, 0,
860 126300 (2 * mb_x + (i & 1)) * block_s,
861 126300 (2 * mb_y + (i >> 1)) * block_s,
862 s->width, s->height, s->linesize,
863 126300 s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
864 block_s, block_s, pix_op,
865 s->mv[dir][i][0], s->mv[dir][i][1]);
866
867 126300 mx += s->mv[dir][i][0];
868 126300 my += s->mv[dir][i][1];
869 }
870
871 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY))
872 31575 chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture,
873 pix_op, mx, my);
874 31575 break;
875 case MV_TYPE_FIELD:
876 if (s->picture_structure == PICT_FRAME) {
877 /* top field */
878 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
879 1, 0, s->field_select[dir][0],
880 ref_picture, pix_op,
881 s->mv[dir][0][0], s->mv[dir][0][1],
882 block_s, mb_y);
883 /* bottom field */
884 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
885 1, 1, s->field_select[dir][1],
886 ref_picture, pix_op,
887 s->mv[dir][1][0], s->mv[dir][1][1],
888 block_s, mb_y);
889 } else {
890 if ( s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != AV_PICTURE_TYPE_B && !s->first_field
891 || !ref_picture[0]) {
892 ref_picture = s->current_picture_ptr->f->data;
893 }
894 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
895 0, 0, s->field_select[dir][0],
896 ref_picture, pix_op,
897 s->mv[dir][0][0],
898 s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
899 }
900 break;
901 case MV_TYPE_16X8:
902 for (int i = 0; i < 2; i++) {
903 uint8_t *const *ref2picture;
904
905 if ((s->picture_structure == s->field_select[dir][i] + 1 ||
906 s->pict_type == AV_PICTURE_TYPE_B || s->first_field) &&
907 ref_picture[0]) {
908 ref2picture = ref_picture;
909 } else {
910 ref2picture = s->current_picture_ptr->f->data;
911 }
912
913 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
914 0, 0, s->field_select[dir][i],
915 ref2picture, pix_op,
916 s->mv[dir][i][0], s->mv[dir][i][1] +
917 2 * block_s * i, block_s, mb_y >> 1);
918
919 dest_y += 2 * block_s * s->linesize;
920 dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
921 dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
922 }
923 break;
924 case MV_TYPE_DMV:
925 if (s->picture_structure == PICT_FRAME) {
926 for (int i = 0; i < 2; i++) {
927 for (int j = 0; j < 2; j++) {
928 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
929 1, j, j ^ i,
930 ref_picture, pix_op,
931 s->mv[dir][2 * i + j][0],
932 s->mv[dir][2 * i + j][1],
933 block_s, mb_y);
934 }
935 pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
936 }
937 } else {
938 if (!ref_picture[0]) {
939 ref_picture = s->current_picture_ptr->f->data;
940 }
941 for (int i = 0; i < 2; i++) {
942 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
943 0, 0, s->picture_structure != i + 1,
944 ref_picture, pix_op,
945 s->mv[dir][2 * i][0],s->mv[dir][2 * i][1],
946 2 * block_s, mb_y >> 1);
947
948 // after put we make avg of the same block
949 pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
950
951 // opposite parity is always in the same
952 // frame if this is second field
953 if (!s->first_field) {
954 ref_picture = s->current_picture_ptr->f->data;
955 }
956 }
957 }
958 break;
959 56389 default:
960 av_assert2(0);
961 }
962 56389 }
963
964 /**
965 * find the lowest MB row referenced in the MVs
966 */
967 60133 static int lowest_referenced_row(MpegEncContext *s, int dir)
968 {
969 60133 int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
970 int off, mvs;
971
972
2/4
✓ Branch 0 taken 60133 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 60133 times.
60133 if (s->picture_structure != PICT_FRAME || s->mcsel)
973 goto unhandled;
974
975
1/4
✓ Branch 0 taken 60133 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
60133 switch (s->mv_type) {
976 60133 case MV_TYPE_16X16:
977 60133 mvs = 1;
978 60133 break;
979 case MV_TYPE_16X8:
980 mvs = 2;
981 break;
982 case MV_TYPE_8X8:
983 mvs = 4;
984 break;
985 default:
986 goto unhandled;
987 }
988
989
2/2
✓ Branch 0 taken 60133 times.
✓ Branch 1 taken 60133 times.
120266 for (int i = 0; i < mvs; i++) {
990 60133 int my = s->mv[dir][i][1];
991 60133 my_max = FFMAX(my_max, my);
992 60133 my_min = FFMIN(my_min, my);
993 }
994
995 60133 off = ((FFMAX(-my_min, my_max) << qpel_shift) + 63) >> 6;
996
997 60133 return av_clip(s->mb_y + off, 0, s->mb_height - 1);
998 unhandled:
999 return s->mb_height - 1;
1000 }
1001
1002 /* add block[] to dest[] */
1003 22703448 static inline void add_dct(MpegEncContext *s,
1004 int16_t *block, int i, uint8_t *dest, int line_size)
1005 {
1006
2/2
✓ Branch 0 taken 8385800 times.
✓ Branch 1 taken 14317648 times.
22703448 if (s->block_last_index[i] >= 0) {
1007 8385800 s->idsp.idct_add(dest, line_size, block);
1008 }
1009 22703448 }
1010
1011 #define IS_ENCODER 0
1012 #include "mpv_reconstruct_mb_template.c"
1013
1014 5119636 void ff_mpv_reconstruct_mb(MpegEncContext *s, int16_t block[12][64])
1015 {
1016
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5119636 times.
5119636 if (s->avctx->debug & FF_DEBUG_DCT_COEFF) {
1017 /* print DCT coefficients */
1018 av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
1019 for (int i = 0; i < 6; i++) {
1020 for (int j = 0; j < 64; j++) {
1021 av_log(s->avctx, AV_LOG_DEBUG, "%5d",
1022 block[i][s->idsp.idct_permutation[j]]);
1023 }
1024 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1025 }
1026 }
1027
1028
2/2
✓ Branch 0 taken 5076136 times.
✓ Branch 1 taken 43500 times.
5119636 if (!s->avctx->lowres) {
1029 #if !CONFIG_SMALL
1030
2/2
✓ Branch 0 taken 2585370 times.
✓ Branch 1 taken 2490766 times.
5076136 if (s->out_format == FMT_MPEG1)
1031 2585370 mpv_reconstruct_mb_internal(s, block, 0, DEFINITELY_MPEG12);
1032 else
1033 2490766 mpv_reconstruct_mb_internal(s, block, 0, NOT_MPEG12);
1034 #else
1035 mpv_reconstruct_mb_internal(s, block, 0, MAY_BE_MPEG12);
1036 #endif
1037 } else
1038 43500 mpv_reconstruct_mb_internal(s, block, 1, MAY_BE_MPEG12);
1039 5119636 }
1040