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