FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo_dec.c
Date: 2022-11-28 23:49:43
Exec Total Coverage
Lines: 379 549 69.0%
Branches: 200 356 56.2%

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