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 | 611 | void ff_mpv_decode_init(MpegEncContext *s, AVCodecContext *avctx) | |
43 | { | ||
44 | 611 | ff_mpv_common_defaults(s); | |
45 | |||
46 | 611 | s->avctx = avctx; | |
47 | 611 | s->width = avctx->coded_width; | |
48 | 611 | s->height = avctx->coded_height; | |
49 | 611 | s->codec_id = avctx->codec->id; | |
50 | 611 | s->workaround_bugs = avctx->workaround_bugs; | |
51 | |||
52 | /* convert fourcc to upper case */ | ||
53 | 611 | s->codec_tag = ff_toupper4(avctx->codec_tag); | |
54 | |||
55 | 611 | ff_h264chroma_init(&s->h264chroma, 8); //for lowres | |
56 | 611 | } | |
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 | 11102 | static int alloc_picture(MpegEncContext *s, Picture *pic) | |
244 | { | ||
245 | 22204 | return ff_alloc_picture(s->avctx, pic, &s->me, &s->sc, 0, 0, | |
246 | 11102 | 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 | 11096 | int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx) | |
272 | { | ||
273 | Picture *pic; | ||
274 | int idx, ret; | ||
275 | |||
276 | 11096 | s->mb_skipped = 0; | |
277 | |||
278 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 11096 times.
|
11096 | 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 8451 times.
✓ Branch 1 taken 2645 times.
✓ Branch 2 taken 7936 times.
✓ Branch 3 taken 515 times.
|
11096 | if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr && |
285 |
1/2✓ Branch 0 taken 7936 times.
✗ Branch 1 not taken.
|
7936 | s->last_picture_ptr != s->next_picture_ptr && |
286 |
1/2✓ Branch 0 taken 7936 times.
✗ Branch 1 not taken.
|
7936 | s->last_picture_ptr->f->buf[0]) { |
287 | 7936 | ff_mpeg_unref_picture(s->avctx, s->last_picture_ptr); | |
288 | } | ||
289 | |||
290 | /* release non reference/forgotten frames */ | ||
291 |
2/2✓ Branch 0 taken 399456 times.
✓ Branch 1 taken 11096 times.
|
410552 | for (int i = 0; i < MAX_PICTURE_COUNT; i++) { |
292 |
2/2✓ Branch 0 taken 13467 times.
✓ Branch 1 taken 385989 times.
|
399456 | if (!s->picture[i].reference || |
293 |
2/2✓ Branch 0 taken 10822 times.
✓ Branch 1 taken 2645 times.
|
13467 | (&s->picture[i] != s->last_picture_ptr && |
294 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 10792 times.
|
10822 | &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 | 385989 | ff_mpeg_unref_picture(s->avctx, &s->picture[i]); | |
297 | } | ||
298 | } | ||
299 | |||
300 | 11096 | ff_mpeg_unref_picture(s->avctx, &s->current_picture); | |
301 | 11096 | ff_mpeg_unref_picture(s->avctx, &s->last_picture); | |
302 | 11096 | ff_mpeg_unref_picture(s->avctx, &s->next_picture); | |
303 | |||
304 |
4/4✓ Branch 0 taken 6662 times.
✓ Branch 1 taken 4434 times.
✓ Branch 2 taken 1153 times.
✓ Branch 3 taken 5509 times.
|
11096 | 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 | 9943 | idx = ff_find_unused_picture(s->avctx, s->picture, 0); | |
310 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9943 times.
|
9943 | if (idx < 0) { |
311 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n"); | |
312 | ✗ | return idx; | |
313 | } | ||
314 | 9943 | pic = &s->picture[idx]; | |
315 | } | ||
316 | |||
317 | 11096 | pic->reference = 0; | |
318 |
1/2✓ Branch 0 taken 11096 times.
✗ Branch 1 not taken.
|
11096 | if (!s->droppable) { |
319 |
2/2✓ Branch 0 taken 8451 times.
✓ Branch 1 taken 2645 times.
|
11096 | if (s->pict_type != AV_PICTURE_TYPE_B) |
320 | 8451 | pic->reference = 3; | |
321 | } | ||
322 | |||
323 | #if FF_API_FRAME_PICTURE_NUMBER | ||
324 | FF_DISABLE_DEPRECATION_WARNINGS | ||
325 | 11096 | pic->f->coded_picture_number = s->coded_picture_number++; | |
326 | FF_ENABLE_DEPRECATION_WARNINGS | ||
327 | #endif | ||
328 | |||
329 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 11096 times.
|
11096 | if (alloc_picture(s, pic) < 0) |
330 | ✗ | return -1; | |
331 | |||
332 | 11096 | s->current_picture_ptr = pic; | |
333 | // FIXME use only the vars from current_pic | ||
334 | 11096 | s->current_picture_ptr->f->top_field_first = s->top_field_first; | |
335 |
2/2✓ Branch 0 taken 10616 times.
✓ Branch 1 taken 480 times.
|
11096 | if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO || |
336 |
2/2✓ Branch 0 taken 3346 times.
✓ Branch 1 taken 7270 times.
|
10616 | s->codec_id == AV_CODEC_ID_MPEG2VIDEO) { |
337 |
2/2✓ Branch 0 taken 695 times.
✓ Branch 1 taken 3131 times.
|
3826 | if (s->picture_structure != PICT_FRAME) |
338 | 695 | s->current_picture_ptr->f->top_field_first = | |
339 | 695 | (s->picture_structure == PICT_TOP_FIELD) == s->first_field; | |
340 | } | ||
341 |
2/2✓ Branch 0 taken 2265 times.
✓ Branch 1 taken 8831 times.
|
13361 | s->current_picture_ptr->f->interlaced_frame = !s->progressive_frame && |
342 |
1/2✓ Branch 0 taken 2265 times.
✗ Branch 1 not taken.
|
2265 | !s->progressive_sequence; |
343 | 11096 | s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME; | |
344 | |||
345 | 11096 | s->current_picture_ptr->f->pict_type = s->pict_type; | |
346 | 11096 | s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I; | |
347 | |||
348 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 11096 times.
|
11096 | 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 8451 times.
✓ Branch 1 taken 2645 times.
|
11096 | if (s->pict_type != AV_PICTURE_TYPE_B) { |
353 | 8451 | s->last_picture_ptr = s->next_picture_ptr; | |
354 |
1/2✓ Branch 0 taken 8451 times.
✗ Branch 1 not taken.
|
8451 | if (!s->droppable) |
355 | 8451 | 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 10792 times.
✓ Branch 1 taken 304 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10792 times.
|
11096 | if ((!s->last_picture_ptr || !s->last_picture_ptr->f->buf[0]) && |
365 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 298 times.
|
304 | (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->key_frame = 0; | |
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 11096 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11096 times.
|
11096 | 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->key_frame = 0; | |
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 | #if 0 // BUFREF-FIXME | ||
439 | memset(s->last_picture.f->data, 0, sizeof(s->last_picture.f->data)); | ||
440 | memset(s->next_picture.f->data, 0, sizeof(s->next_picture.f->data)); | ||
441 | #endif | ||
442 |
2/2✓ Branch 0 taken 10798 times.
✓ Branch 1 taken 298 times.
|
11096 | if (s->last_picture_ptr) { |
443 |
2/4✓ Branch 0 taken 10798 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10798 times.
|
21596 | if (s->last_picture_ptr->f->buf[0] && |
444 | 10798 | (ret = ff_mpeg_ref_picture(s->avctx, &s->last_picture, | |
445 | s->last_picture_ptr)) < 0) | ||
446 | ✗ | return ret; | |
447 | } | ||
448 |
1/2✓ Branch 0 taken 11096 times.
✗ Branch 1 not taken.
|
11096 | if (s->next_picture_ptr) { |
449 |
2/4✓ Branch 0 taken 11096 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11096 times.
|
22192 | if (s->next_picture_ptr->f->buf[0] && |
450 | 11096 | (ret = ff_mpeg_ref_picture(s->avctx, &s->next_picture, | |
451 | s->next_picture_ptr)) < 0) | ||
452 | ✗ | return ret; | |
453 | } | ||
454 | |||
455 |
4/6✓ Branch 0 taken 9883 times.
✓ Branch 1 taken 1213 times.
✓ Branch 2 taken 9883 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 9883 times.
|
11096 | av_assert0(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr && |
456 | s->last_picture_ptr->f->buf[0])); | ||
457 | |||
458 |
2/2✓ Branch 0 taken 695 times.
✓ Branch 1 taken 10401 times.
|
11096 | if (s->picture_structure != PICT_FRAME) { |
459 |
2/2✓ Branch 0 taken 2780 times.
✓ Branch 1 taken 695 times.
|
3475 | for (int i = 0; i < 4; i++) { |
460 |
2/2✓ Branch 0 taken 2776 times.
✓ Branch 1 taken 4 times.
|
2780 | if (s->picture_structure == PICT_BOTTOM_FIELD) { |
461 |
2/2✓ Branch 0 taken 2082 times.
✓ Branch 1 taken 694 times.
|
2776 | s->current_picture.f->data[i] = FF_PTR_ADD(s->current_picture.f->data[i], |
462 | s->current_picture.f->linesize[i]); | ||
463 | } | ||
464 | 2780 | s->current_picture.f->linesize[i] *= 2; | |
465 | 2780 | s->last_picture.f->linesize[i] *= 2; | |
466 | 2780 | s->next_picture.f->linesize[i] *= 2; | |
467 | } | ||
468 | } | ||
469 | |||
470 | /* set dequantizer, we can't do it during init as | ||
471 | * it might change for MPEG-4 and we can't do it in the header | ||
472 | * decode as init is not called for MPEG-4 there yet */ | ||
473 |
4/4✓ Branch 0 taken 11056 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 3346 times.
✓ Branch 3 taken 7710 times.
|
11096 | if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) { |
474 | 3386 | s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra; | |
475 | 3386 | s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter; | |
476 |
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) { |
477 | 7230 | s->dct_unquantize_intra = s->dct_unquantize_h263_intra; | |
478 | 7230 | s->dct_unquantize_inter = s->dct_unquantize_h263_inter; | |
479 | } else { | ||
480 | 480 | s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra; | |
481 | 480 | s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter; | |
482 | } | ||
483 | |||
484 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11096 times.
|
11096 | if (s->avctx->debug & FF_DEBUG_NOMC) |
485 | ✗ | gray_frame(s->current_picture_ptr->f); | |
486 | |||
487 | 11096 | return 0; | |
488 | } | ||
489 | |||
490 | /* called after a frame has been decoded. */ | ||
491 | 11096 | void ff_mpv_frame_end(MpegEncContext *s) | |
492 | { | ||
493 | 11096 | emms_c(); | |
494 | |||
495 |
2/2✓ Branch 0 taken 8451 times.
✓ Branch 1 taken 2645 times.
|
11096 | if (s->current_picture.reference) |
496 | 8451 | ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0); | |
497 | 11096 | } | |
498 | |||
499 | 10875 | void ff_print_debug_info(const MpegEncContext *s, const Picture *p, AVFrame *pict) | |
500 | { | ||
501 | 10875 | ff_print_debug_info2(s->avctx, pict, s->mbskip_table, p->mb_type, | |
502 | 10875 | p->qscale_table, p->motion_val, | |
503 | 10875 | s->mb_width, s->mb_height, s->mb_stride, s->quarter_sample); | |
504 | 10875 | } | |
505 | |||
506 | 10142 | int ff_mpv_export_qp_table(const MpegEncContext *s, AVFrame *f, const Picture *p, int qp_type) | |
507 | { | ||
508 | AVVideoEncParams *par; | ||
509 |
2/2✓ Branch 0 taken 6376 times.
✓ Branch 1 taken 3766 times.
|
10142 | int mult = (qp_type == FF_MPV_QSCALE_TYPE_MPEG1) ? 2 : 1; |
510 | 10142 | unsigned int nb_mb = p->alloc_mb_height * p->alloc_mb_width; | |
511 | |||
512 |
2/2✓ Branch 0 taken 10127 times.
✓ Branch 1 taken 15 times.
|
10142 | if (!(s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS)) |
513 | 10127 | return 0; | |
514 | |||
515 | 15 | par = av_video_enc_params_create_side_data(f, AV_VIDEO_ENC_PARAMS_MPEG2, nb_mb); | |
516 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | if (!par) |
517 | ✗ | return AVERROR(ENOMEM); | |
518 | |||
519 |
2/2✓ Branch 0 taken 270 times.
✓ Branch 1 taken 15 times.
|
285 | for (unsigned y = 0; y < p->alloc_mb_height; y++) |
520 |
2/2✓ Branch 0 taken 5940 times.
✓ Branch 1 taken 270 times.
|
6210 | for (unsigned x = 0; x < p->alloc_mb_width; x++) { |
521 | 5940 | const unsigned int block_idx = y * p->alloc_mb_width + x; | |
522 | 5940 | const unsigned int mb_xy = y * p->alloc_mb_stride + x; | |
523 | 5940 | AVVideoBlockParams *const b = av_video_enc_params_block(par, block_idx); | |
524 | |||
525 | 5940 | b->src_x = x * 16; | |
526 | 5940 | b->src_y = y * 16; | |
527 | 5940 | b->w = 16; | |
528 | 5940 | b->h = 16; | |
529 | |||
530 | 5940 | b->delta_qp = p->qscale_table[mb_xy] * mult; | |
531 | } | ||
532 | |||
533 | 15 | return 0; | |
534 | } | ||
535 | |||
536 | 167813 | void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h) | |
537 | { | ||
538 | 167813 | ff_draw_horiz_band(s->avctx, s->current_picture_ptr->f, | |
539 |
2/2✓ Branch 0 taken 164166 times.
✓ Branch 1 taken 3647 times.
|
167813 | s->last_picture_ptr ? s->last_picture_ptr->f : NULL, |
540 | y, h, s->picture_structure, | ||
541 | s->first_field, s->low_delay); | ||
542 | 167813 | } | |
543 | |||
544 | 81 | void ff_mpeg_flush(AVCodecContext *avctx) | |
545 | { | ||
546 | 81 | MpegEncContext *const s = avctx->priv_data; | |
547 | |||
548 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
|
81 | if (!s->picture) |
549 | ✗ | return; | |
550 | |||
551 |
2/2✓ Branch 0 taken 2916 times.
✓ Branch 1 taken 81 times.
|
2997 | for (int i = 0; i < MAX_PICTURE_COUNT; i++) |
552 | 2916 | ff_mpeg_unref_picture(s->avctx, &s->picture[i]); | |
553 | 81 | s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL; | |
554 | |||
555 | 81 | ff_mpeg_unref_picture(s->avctx, &s->current_picture); | |
556 | 81 | ff_mpeg_unref_picture(s->avctx, &s->last_picture); | |
557 | 81 | ff_mpeg_unref_picture(s->avctx, &s->next_picture); | |
558 | |||
559 | 81 | s->mb_x = s->mb_y = 0; | |
560 | |||
561 | 81 | s->bitstream_buffer_size = 0; | |
562 | 81 | s->pp_time = 0; | |
563 | } | ||
564 | |||
565 | 167813 | void ff_mpv_report_decode_progress(MpegEncContext *s) | |
566 | { | ||
567 |
6/6✓ Branch 0 taken 128599 times.
✓ Branch 1 taken 39214 times.
✓ Branch 2 taken 122016 times.
✓ Branch 3 taken 6583 times.
✓ Branch 4 taken 114737 times.
✓ Branch 5 taken 7279 times.
|
167813 | if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred) |
568 | 114737 | ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0); | |
569 | 167813 | } | |
570 | |||
571 | |||
572 | 126300 | static inline int hpel_motion_lowres(MpegEncContext *s, | |
573 | uint8_t *dest, const uint8_t *src, | ||
574 | int field_based, int field_select, | ||
575 | int src_x, int src_y, | ||
576 | int width, int height, ptrdiff_t stride, | ||
577 | int h_edge_pos, int v_edge_pos, | ||
578 | int w, int h, const h264_chroma_mc_func *pix_op, | ||
579 | int motion_x, int motion_y) | ||
580 | { | ||
581 | 126300 | const int lowres = s->avctx->lowres; | |
582 | 126300 | const int op_index = FFMIN(lowres, 3); | |
583 | 126300 | const int s_mask = (2 << lowres) - 1; | |
584 | 126300 | int emu = 0; | |
585 | int sx, sy; | ||
586 | |||
587 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 126300 times.
|
126300 | if (s->quarter_sample) { |
588 | ✗ | motion_x /= 2; | |
589 | ✗ | motion_y /= 2; | |
590 | } | ||
591 | |||
592 | 126300 | sx = motion_x & s_mask; | |
593 | 126300 | sy = motion_y & s_mask; | |
594 | 126300 | src_x += motion_x >> lowres + 1; | |
595 | 126300 | src_y += motion_y >> lowres + 1; | |
596 | |||
597 | 126300 | src += src_y * stride + src_x; | |
598 | |||
599 |
1/2✓ Branch 0 taken 126300 times.
✗ Branch 1 not taken.
|
126300 | if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w, 0) || |
600 |
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)) { |
601 | 298 | s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, src, | |
602 | s->linesize, s->linesize, | ||
603 | 298 | w + 1, (h + 1) << field_based, | |
604 | src_x, src_y * (1 << field_based), | ||
605 | h_edge_pos, v_edge_pos); | ||
606 | 298 | src = s->sc.edge_emu_buffer; | |
607 | 298 | emu = 1; | |
608 | } | ||
609 | |||
610 | 126300 | sx = (sx << 2) >> lowres; | |
611 | 126300 | sy = (sy << 2) >> lowres; | |
612 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 126300 times.
|
126300 | if (field_select) |
613 | ✗ | src += s->linesize; | |
614 | 126300 | pix_op[op_index](dest, src, stride, h, sx, sy); | |
615 | 126300 | return emu; | |
616 | } | ||
617 | |||
618 | /* apply one mpeg motion vector to the three components */ | ||
619 | 24814 | static av_always_inline void mpeg_motion_lowres(MpegEncContext *s, | |
620 | uint8_t *dest_y, | ||
621 | uint8_t *dest_cb, | ||
622 | uint8_t *dest_cr, | ||
623 | int field_based, | ||
624 | int bottom_field, | ||
625 | int field_select, | ||
626 | uint8_t *const *ref_picture, | ||
627 | const h264_chroma_mc_func *pix_op, | ||
628 | int motion_x, int motion_y, | ||
629 | int h, int mb_y) | ||
630 | { | ||
631 | const uint8_t *ptr_y, *ptr_cb, *ptr_cr; | ||
632 | int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, sx, sy, uvsx, uvsy; | ||
633 | ptrdiff_t uvlinesize, linesize; | ||
634 | 24814 | const int lowres = s->avctx->lowres; | |
635 | 24814 | const int op_index = FFMIN(lowres - 1 + s->chroma_x_shift, 3); | |
636 | 24814 | const int block_s = 8 >> lowres; | |
637 | 24814 | const int s_mask = (2 << lowres) - 1; | |
638 | 24814 | const int h_edge_pos = s->h_edge_pos >> lowres; | |
639 | 24814 | const int v_edge_pos = s->v_edge_pos >> lowres; | |
640 | 24814 | linesize = s->current_picture.f->linesize[0] << field_based; | |
641 | 24814 | uvlinesize = s->current_picture.f->linesize[1] << field_based; | |
642 | |||
643 | // FIXME obviously not perfect but qpel will not work in lowres anyway | ||
644 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24814 times.
|
24814 | if (s->quarter_sample) { |
645 | ✗ | motion_x /= 2; | |
646 | ✗ | motion_y /= 2; | |
647 | } | ||
648 | |||
649 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24814 times.
|
24814 | if (field_based) { |
650 | ✗ | motion_y += (bottom_field - field_select)*((1 << lowres)-1); | |
651 | } | ||
652 | |||
653 | 24814 | sx = motion_x & s_mask; | |
654 | 24814 | sy = motion_y & s_mask; | |
655 | 24814 | src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1); | |
656 | 24814 | src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1); | |
657 | |||
658 |
1/2✓ Branch 0 taken 24814 times.
✗ Branch 1 not taken.
|
24814 | if (s->out_format == FMT_H263) { |
659 | 24814 | uvsx = ((motion_x >> 1) & s_mask) | (sx & 1); | |
660 | 24814 | uvsy = ((motion_y >> 1) & s_mask) | (sy & 1); | |
661 | 24814 | uvsrc_x = src_x >> 1; | |
662 | 24814 | uvsrc_y = src_y >> 1; | |
663 | ✗ | } else if (s->out_format == FMT_H261) { | |
664 | // even chroma mv's are full pel in H261 | ||
665 | ✗ | mx = motion_x / 4; | |
666 | ✗ | my = motion_y / 4; | |
667 | ✗ | uvsx = (2 * mx) & s_mask; | |
668 | ✗ | uvsy = (2 * my) & s_mask; | |
669 | ✗ | uvsrc_x = s->mb_x * block_s + (mx >> lowres); | |
670 | ✗ | uvsrc_y = mb_y * block_s + (my >> lowres); | |
671 | } else { | ||
672 | ✗ | if (s->chroma_y_shift) { | |
673 | ✗ | mx = motion_x / 2; | |
674 | ✗ | my = motion_y / 2; | |
675 | ✗ | uvsx = mx & s_mask; | |
676 | ✗ | uvsy = my & s_mask; | |
677 | ✗ | uvsrc_x = s->mb_x * block_s + (mx >> lowres + 1); | |
678 | ✗ | uvsrc_y = (mb_y * block_s >> field_based) + (my >> lowres + 1); | |
679 | } else { | ||
680 | ✗ | if (s->chroma_x_shift) { | |
681 | //Chroma422 | ||
682 | ✗ | mx = motion_x / 2; | |
683 | ✗ | uvsx = mx & s_mask; | |
684 | ✗ | uvsy = motion_y & s_mask; | |
685 | ✗ | uvsrc_y = src_y; | |
686 | ✗ | uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1)); | |
687 | } else { | ||
688 | //Chroma444 | ||
689 | ✗ | uvsx = motion_x & s_mask; | |
690 | ✗ | uvsy = motion_y & s_mask; | |
691 | ✗ | uvsrc_x = src_x; | |
692 | ✗ | uvsrc_y = src_y; | |
693 | } | ||
694 | } | ||
695 | } | ||
696 | |||
697 | 24814 | ptr_y = ref_picture[0] + src_y * linesize + src_x; | |
698 | 24814 | ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x; | |
699 | 24814 | ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x; | |
700 | |||
701 |
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 || |
702 |
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)) { |
703 | 197 | s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr_y, | |
704 | linesize >> field_based, linesize >> field_based, | ||
705 | 17, 17 + field_based, | ||
706 | src_x, src_y * (1 << field_based), h_edge_pos, | ||
707 | v_edge_pos); | ||
708 | 197 | ptr_y = s->sc.edge_emu_buffer; | |
709 | if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) { | ||
710 | 197 | uint8_t *ubuf = s->sc.edge_emu_buffer + 18 * s->linesize; | |
711 | 197 | uint8_t *vbuf =ubuf + 10 * s->uvlinesize; | |
712 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 197 times.
|
197 | if (s->workaround_bugs & FF_BUG_IEDGE) |
713 | ✗ | vbuf -= s->uvlinesize; | |
714 | 197 | s->vdsp.emulated_edge_mc(ubuf, ptr_cb, | |
715 | uvlinesize >> field_based, uvlinesize >> field_based, | ||
716 | 9, 9 + field_based, | ||
717 | uvsrc_x, uvsrc_y * (1 << field_based), | ||
718 | h_edge_pos >> 1, v_edge_pos >> 1); | ||
719 | 197 | s->vdsp.emulated_edge_mc(vbuf, ptr_cr, | |
720 | uvlinesize >> field_based,uvlinesize >> field_based, | ||
721 | 9, 9 + field_based, | ||
722 | uvsrc_x, uvsrc_y * (1 << field_based), | ||
723 | h_edge_pos >> 1, v_edge_pos >> 1); | ||
724 | 197 | ptr_cb = ubuf; | |
725 | 197 | ptr_cr = vbuf; | |
726 | } | ||
727 | } | ||
728 | |||
729 | // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f->data | ||
730 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24814 times.
|
24814 | if (bottom_field) { |
731 | ✗ | dest_y += s->linesize; | |
732 | ✗ | dest_cb += s->uvlinesize; | |
733 | ✗ | dest_cr += s->uvlinesize; | |
734 | } | ||
735 | |||
736 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24814 times.
|
24814 | if (field_select) { |
737 | ✗ | ptr_y += s->linesize; | |
738 | ✗ | ptr_cb += s->uvlinesize; | |
739 | ✗ | ptr_cr += s->uvlinesize; | |
740 | } | ||
741 | |||
742 | 24814 | sx = (sx << 2) >> lowres; | |
743 | 24814 | sy = (sy << 2) >> lowres; | |
744 | 24814 | pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy); | |
745 | |||
746 | if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) { | ||
747 |
1/2✓ Branch 0 taken 24814 times.
✗ Branch 1 not taken.
|
24814 | int hc = s->chroma_y_shift ? (h+1-bottom_field)>>1 : h; |
748 | 24814 | uvsx = (uvsx << 2) >> lowres; | |
749 | 24814 | uvsy = (uvsy << 2) >> lowres; | |
750 |
1/2✓ Branch 0 taken 24814 times.
✗ Branch 1 not taken.
|
24814 | if (hc) { |
751 | 24814 | pix_op[op_index](dest_cb, ptr_cb, uvlinesize, hc, uvsx, uvsy); | |
752 | 24814 | pix_op[op_index](dest_cr, ptr_cr, uvlinesize, hc, uvsx, uvsy); | |
753 | } | ||
754 | } | ||
755 | // FIXME h261 lowres loop filter | ||
756 | 24814 | } | |
757 | |||
758 | 31575 | static inline void chroma_4mv_motion_lowres(MpegEncContext *s, | |
759 | uint8_t *dest_cb, uint8_t *dest_cr, | ||
760 | uint8_t *const *ref_picture, | ||
761 | const h264_chroma_mc_func * pix_op, | ||
762 | int mx, int my) | ||
763 | { | ||
764 | 31575 | const int lowres = s->avctx->lowres; | |
765 | 31575 | const int op_index = FFMIN(lowres, 3); | |
766 | 31575 | const int block_s = 8 >> lowres; | |
767 | 31575 | const int s_mask = (2 << lowres) - 1; | |
768 | 31575 | const int h_edge_pos = s->h_edge_pos >> lowres + 1; | |
769 | 31575 | const int v_edge_pos = s->v_edge_pos >> lowres + 1; | |
770 | 31575 | int emu = 0, src_x, src_y, sx, sy; | |
771 | ptrdiff_t offset; | ||
772 | const uint8_t *ptr; | ||
773 | |||
774 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31575 times.
|
31575 | if (s->quarter_sample) { |
775 | ✗ | mx /= 2; | |
776 | ✗ | my /= 2; | |
777 | } | ||
778 | |||
779 | /* In case of 8X8, we construct a single chroma motion vector | ||
780 | with a special rounding */ | ||
781 | 31575 | mx = ff_h263_round_chroma(mx); | |
782 | 31575 | my = ff_h263_round_chroma(my); | |
783 | |||
784 | 31575 | sx = mx & s_mask; | |
785 | 31575 | sy = my & s_mask; | |
786 | 31575 | src_x = s->mb_x * block_s + (mx >> lowres + 1); | |
787 | 31575 | src_y = s->mb_y * block_s + (my >> lowres + 1); | |
788 | |||
789 | 31575 | offset = src_y * s->uvlinesize + src_x; | |
790 | 31575 | ptr = ref_picture[1] + offset; | |
791 |
1/2✓ Branch 0 taken 31575 times.
✗ Branch 1 not taken.
|
31575 | if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) || |
792 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 31431 times.
|
31575 | (unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) { |
793 | 144 | s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr, | |
794 | s->uvlinesize, s->uvlinesize, | ||
795 | 9, 9, | ||
796 | src_x, src_y, h_edge_pos, v_edge_pos); | ||
797 | 144 | ptr = s->sc.edge_emu_buffer; | |
798 | 144 | emu = 1; | |
799 | } | ||
800 | 31575 | sx = (sx << 2) >> lowres; | |
801 | 31575 | sy = (sy << 2) >> lowres; | |
802 | 31575 | pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy); | |
803 | |||
804 | 31575 | ptr = ref_picture[2] + offset; | |
805 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 31431 times.
|
31575 | if (emu) { |
806 | 144 | s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr, | |
807 | s->uvlinesize, s->uvlinesize, | ||
808 | 9, 9, | ||
809 | src_x, src_y, h_edge_pos, v_edge_pos); | ||
810 | 144 | ptr = s->sc.edge_emu_buffer; | |
811 | } | ||
812 | 31575 | pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy); | |
813 | 31575 | } | |
814 | |||
815 | /** | ||
816 | * motion compensation of a single macroblock | ||
817 | * @param s context | ||
818 | * @param dest_y luma destination pointer | ||
819 | * @param dest_cb chroma cb/u destination pointer | ||
820 | * @param dest_cr chroma cr/v destination pointer | ||
821 | * @param dir direction (0->forward, 1->backward) | ||
822 | * @param ref_picture array[3] of pointers to the 3 planes of the reference picture | ||
823 | * @param pix_op halfpel motion compensation function (average or put normally) | ||
824 | * the motion vectors are taken from s->mv and the MV type from s->mv_type | ||
825 | */ | ||
826 | 56389 | static inline void MPV_motion_lowres(MpegEncContext *s, | |
827 | uint8_t *dest_y, uint8_t *dest_cb, | ||
828 | uint8_t *dest_cr, | ||
829 | int dir, uint8_t *const *ref_picture, | ||
830 | const h264_chroma_mc_func *pix_op) | ||
831 | { | ||
832 | int mx, my; | ||
833 | int mb_x, mb_y; | ||
834 | 56389 | const int lowres = s->avctx->lowres; | |
835 | 56389 | const int block_s = 8 >>lowres; | |
836 | |||
837 | 56389 | mb_x = s->mb_x; | |
838 | 56389 | mb_y = s->mb_y; | |
839 | |||
840 |
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) { |
841 | 24814 | case MV_TYPE_16X16: | |
842 | 24814 | mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr, | |
843 | 0, 0, 0, | ||
844 | ref_picture, pix_op, | ||
845 | s->mv[dir][0][0], s->mv[dir][0][1], | ||
846 | 2 * block_s, mb_y); | ||
847 | 24814 | break; | |
848 | 31575 | case MV_TYPE_8X8: | |
849 | 31575 | mx = 0; | |
850 | 31575 | my = 0; | |
851 |
2/2✓ Branch 0 taken 126300 times.
✓ Branch 1 taken 31575 times.
|
157875 | for (int i = 0; i < 4; i++) { |
852 | 126300 | hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) * | |
853 | 126300 | s->linesize) * block_s, | |
854 | ref_picture[0], 0, 0, | ||
855 | 126300 | (2 * mb_x + (i & 1)) * block_s, | |
856 | 126300 | (2 * mb_y + (i >> 1)) * block_s, | |
857 | s->width, s->height, s->linesize, | ||
858 | 126300 | s->h_edge_pos >> lowres, s->v_edge_pos >> lowres, | |
859 | block_s, block_s, pix_op, | ||
860 | s->mv[dir][i][0], s->mv[dir][i][1]); | ||
861 | |||
862 | 126300 | mx += s->mv[dir][i][0]; | |
863 | 126300 | my += s->mv[dir][i][1]; | |
864 | } | ||
865 | |||
866 | if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) | ||
867 | 31575 | chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture, | |
868 | pix_op, mx, my); | ||
869 | 31575 | break; | |
870 | ✗ | case MV_TYPE_FIELD: | |
871 | ✗ | if (s->picture_structure == PICT_FRAME) { | |
872 | /* top field */ | ||
873 | ✗ | mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr, | |
874 | 1, 0, s->field_select[dir][0], | ||
875 | ref_picture, pix_op, | ||
876 | s->mv[dir][0][0], s->mv[dir][0][1], | ||
877 | block_s, mb_y); | ||
878 | /* bottom field */ | ||
879 | ✗ | mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr, | |
880 | 1, 1, s->field_select[dir][1], | ||
881 | ref_picture, pix_op, | ||
882 | s->mv[dir][1][0], s->mv[dir][1][1], | ||
883 | block_s, mb_y); | ||
884 | } else { | ||
885 | ✗ | if (s->picture_structure != s->field_select[dir][0] + 1 && | |
886 | ✗ | s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) { | |
887 | ✗ | ref_picture = s->current_picture_ptr->f->data; | |
888 | |||
889 | } | ||
890 | ✗ | mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr, | |
891 | 0, 0, s->field_select[dir][0], | ||
892 | ref_picture, pix_op, | ||
893 | s->mv[dir][0][0], | ||
894 | s->mv[dir][0][1], 2 * block_s, mb_y >> 1); | ||
895 | } | ||
896 | ✗ | break; | |
897 | ✗ | case MV_TYPE_16X8: | |
898 | ✗ | for (int i = 0; i < 2; i++) { | |
899 | uint8_t *const *ref2picture; | ||
900 | |||
901 | ✗ | if (s->picture_structure == s->field_select[dir][i] + 1 || | |
902 | ✗ | s->pict_type == AV_PICTURE_TYPE_B || s->first_field) { | |
903 | ✗ | ref2picture = ref_picture; | |
904 | } else { | ||
905 | ✗ | ref2picture = s->current_picture_ptr->f->data; | |
906 | } | ||
907 | |||
908 | ✗ | mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr, | |
909 | 0, 0, s->field_select[dir][i], | ||
910 | ref2picture, pix_op, | ||
911 | ✗ | s->mv[dir][i][0], s->mv[dir][i][1] + | |
912 | ✗ | 2 * block_s * i, block_s, mb_y >> 1); | |
913 | |||
914 | ✗ | dest_y += 2 * block_s * s->linesize; | |
915 | ✗ | dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize; | |
916 | ✗ | dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize; | |
917 | } | ||
918 | ✗ | break; | |
919 | ✗ | case MV_TYPE_DMV: | |
920 | ✗ | if (s->picture_structure == PICT_FRAME) { | |
921 | ✗ | for (int i = 0; i < 2; i++) { | |
922 | ✗ | for (int j = 0; j < 2; j++) { | |
923 | ✗ | mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr, | |
924 | 1, j, j ^ i, | ||
925 | ref_picture, pix_op, | ||
926 | ✗ | s->mv[dir][2 * i + j][0], | |
927 | ✗ | s->mv[dir][2 * i + j][1], | |
928 | block_s, mb_y); | ||
929 | } | ||
930 | ✗ | pix_op = s->h264chroma.avg_h264_chroma_pixels_tab; | |
931 | } | ||
932 | } else { | ||
933 | ✗ | for (int i = 0; i < 2; i++) { | |
934 | ✗ | mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr, | |
935 | ✗ | 0, 0, s->picture_structure != i + 1, | |
936 | ref_picture, pix_op, | ||
937 | ✗ | s->mv[dir][2 * i][0],s->mv[dir][2 * i][1], | |
938 | 2 * block_s, mb_y >> 1); | ||
939 | |||
940 | // after put we make avg of the same block | ||
941 | ✗ | pix_op = s->h264chroma.avg_h264_chroma_pixels_tab; | |
942 | |||
943 | // opposite parity is always in the same | ||
944 | // frame if this is second field | ||
945 | ✗ | if (!s->first_field) { | |
946 | ✗ | ref_picture = s->current_picture_ptr->f->data; | |
947 | } | ||
948 | } | ||
949 | } | ||
950 | ✗ | break; | |
951 | 56389 | default: | |
952 | av_assert2(0); | ||
953 | } | ||
954 | 56389 | } | |
955 | |||
956 | /** | ||
957 | * find the lowest MB row referenced in the MVs | ||
958 | */ | ||
959 | 60133 | static int lowest_referenced_row(MpegEncContext *s, int dir) | |
960 | { | ||
961 | 60133 | int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample; | |
962 | int off, mvs; | ||
963 | |||
964 |
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) |
965 | ✗ | goto unhandled; | |
966 | |||
967 |
1/4✓ Branch 0 taken 60133 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
60133 | switch (s->mv_type) { |
968 | 60133 | case MV_TYPE_16X16: | |
969 | 60133 | mvs = 1; | |
970 | 60133 | break; | |
971 | ✗ | case MV_TYPE_16X8: | |
972 | ✗ | mvs = 2; | |
973 | ✗ | break; | |
974 | ✗ | case MV_TYPE_8X8: | |
975 | ✗ | mvs = 4; | |
976 | ✗ | break; | |
977 | ✗ | default: | |
978 | ✗ | goto unhandled; | |
979 | } | ||
980 | |||
981 |
2/2✓ Branch 0 taken 60133 times.
✓ Branch 1 taken 60133 times.
|
120266 | for (int i = 0; i < mvs; i++) { |
982 | 60133 | int my = s->mv[dir][i][1]; | |
983 | 60133 | my_max = FFMAX(my_max, my); | |
984 | 60133 | my_min = FFMIN(my_min, my); | |
985 | } | ||
986 | |||
987 | 60133 | off = ((FFMAX(-my_min, my_max) << qpel_shift) + 63) >> 6; | |
988 | |||
989 | 60133 | return av_clip(s->mb_y + off, 0, s->mb_height - 1); | |
990 | ✗ | unhandled: | |
991 | ✗ | return s->mb_height - 1; | |
992 | } | ||
993 | |||
994 | /* add block[] to dest[] */ | ||
995 | 22441896 | static inline void add_dct(MpegEncContext *s, | |
996 | int16_t *block, int i, uint8_t *dest, int line_size) | ||
997 | { | ||
998 |
2/2✓ Branch 0 taken 8244919 times.
✓ Branch 1 taken 14196977 times.
|
22441896 | if (s->block_last_index[i] >= 0) { |
999 | 8244919 | s->idsp.idct_add(dest, line_size, block); | |
1000 | } | ||
1001 | 22441896 | } | |
1002 | |||
1003 | #define IS_ENCODER 0 | ||
1004 | #include "mpv_reconstruct_mb_template.c" | ||
1005 | |||
1006 | 5071059 | void ff_mpv_reconstruct_mb(MpegEncContext *s, int16_t block[12][64]) | |
1007 | { | ||
1008 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5071059 times.
|
5071059 | if (s->avctx->debug & FF_DEBUG_DCT_COEFF) { |
1009 | /* print DCT coefficients */ | ||
1010 | ✗ | av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y); | |
1011 | ✗ | for (int i = 0; i < 6; i++) { | |
1012 | ✗ | for (int j = 0; j < 64; j++) { | |
1013 | ✗ | av_log(s->avctx, AV_LOG_DEBUG, "%5d", | |
1014 | ✗ | block[i][s->idsp.idct_permutation[j]]); | |
1015 | } | ||
1016 | ✗ | av_log(s->avctx, AV_LOG_DEBUG, "\n"); | |
1017 | } | ||
1018 | } | ||
1019 | |||
1020 |
2/2✓ Branch 0 taken 5027559 times.
✓ Branch 1 taken 43500 times.
|
5071059 | if (!s->avctx->lowres) { |
1021 | #if !CONFIG_SMALL | ||
1022 |
2/2✓ Branch 0 taken 2549289 times.
✓ Branch 1 taken 2478270 times.
|
5027559 | if (s->out_format == FMT_MPEG1) |
1023 | 2549289 | mpv_reconstruct_mb_internal(s, block, 0, DEFINITELY_MPEG12); | |
1024 | else | ||
1025 | 2478270 | mpv_reconstruct_mb_internal(s, block, 0, NOT_MPEG12); | |
1026 | #else | ||
1027 | mpv_reconstruct_mb_internal(s, block, 0, MAY_BE_MPEG12); | ||
1028 | #endif | ||
1029 | } else | ||
1030 | 43500 | mpv_reconstruct_mb_internal(s, block, 1, MAY_BE_MPEG12); | |
1031 | 5071059 | } | |
1032 |