Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * The simplest mpeg encoder (well, it was the simplest!) | ||
3 | * Copyright (c) 2000,2001 Fabrice Bellard | ||
4 | * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at> | ||
5 | * | ||
6 | * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at> | ||
7 | * | ||
8 | * This file is part of FFmpeg. | ||
9 | * | ||
10 | * FFmpeg is free software; you can redistribute it and/or | ||
11 | * modify it under the terms of the GNU Lesser General Public | ||
12 | * License as published by the Free Software Foundation; either | ||
13 | * version 2.1 of the License, or (at your option) any later version. | ||
14 | * | ||
15 | * FFmpeg is distributed in the hope that it will be useful, | ||
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
18 | * Lesser General Public License for more details. | ||
19 | * | ||
20 | * You should have received a copy of the GNU Lesser General Public | ||
21 | * License along with FFmpeg; if not, write to the Free Software | ||
22 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
23 | */ | ||
24 | |||
25 | /** | ||
26 | * @file | ||
27 | * The simplest mpeg encoder (well, it was the simplest!). | ||
28 | */ | ||
29 | |||
30 | #include "libavutil/attributes.h" | ||
31 | #include "libavutil/avassert.h" | ||
32 | #include "libavutil/imgutils.h" | ||
33 | #include "libavutil/internal.h" | ||
34 | #include "libavutil/intreadwrite.h" | ||
35 | #include "libavutil/mem.h" | ||
36 | |||
37 | #include "avcodec.h" | ||
38 | #include "blockdsp.h" | ||
39 | #include "idctdsp.h" | ||
40 | #include "mathops.h" | ||
41 | #include "mpeg_er.h" | ||
42 | #include "mpegutils.h" | ||
43 | #include "mpegvideo.h" | ||
44 | #include "mpegvideodata.h" | ||
45 | #include "mpegvideo_unquantize.h" | ||
46 | #include "libavutil/refstruct.h" | ||
47 | |||
48 | |||
49 | ✗ | static void gray16(uint8_t *dst, const uint8_t *src, ptrdiff_t linesize, int h) | |
50 | { | ||
51 | ✗ | while(h--) | |
52 | ✗ | memset(dst + h*linesize, 128, 16); | |
53 | ✗ | } | |
54 | |||
55 | ✗ | static void gray8(uint8_t *dst, const uint8_t *src, ptrdiff_t linesize, int h) | |
56 | { | ||
57 | ✗ | while(h--) | |
58 | ✗ | memset(dst + h*linesize, 128, 8); | |
59 | ✗ | } | |
60 | |||
61 | /* init common dct for both encoder and decoder */ | ||
62 | 800 | static av_cold void dsp_init(MpegEncContext *s) | |
63 | { | ||
64 | 800 | ff_blockdsp_init(&s->bdsp); | |
65 | 800 | ff_hpeldsp_init(&s->hdsp, s->avctx->flags); | |
66 | 800 | ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample); | |
67 | |||
68 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 800 times.
|
800 | if (s->avctx->debug & FF_DEBUG_NOMC) { |
69 | int i; | ||
70 | ✗ | for (i=0; i<4; i++) { | |
71 | ✗ | s->hdsp.avg_pixels_tab[0][i] = gray16; | |
72 | ✗ | s->hdsp.put_pixels_tab[0][i] = gray16; | |
73 | ✗ | s->hdsp.put_no_rnd_pixels_tab[0][i] = gray16; | |
74 | |||
75 | ✗ | s->hdsp.avg_pixels_tab[1][i] = gray8; | |
76 | ✗ | s->hdsp.put_pixels_tab[1][i] = gray8; | |
77 | ✗ | s->hdsp.put_no_rnd_pixels_tab[1][i] = gray8; | |
78 | } | ||
79 | } | ||
80 | 800 | } | |
81 | |||
82 | 5452 | av_cold void ff_init_scantable(const uint8_t *permutation, ScanTable *st, | |
83 | const uint8_t *src_scantable) | ||
84 | { | ||
85 | 5452 | st->scantable = src_scantable; | |
86 | |||
87 |
2/2✓ Branch 0 taken 348928 times.
✓ Branch 1 taken 5452 times.
|
354380 | for (int i = 0, end = -1; i < 64; i++) { |
88 | 348928 | int j = src_scantable[i]; | |
89 | 348928 | st->permutated[i] = permutation[j]; | |
90 |
2/2✓ Branch 0 taken 102778 times.
✓ Branch 1 taken 246150 times.
|
348928 | if (permutation[j] > end) |
91 | 102778 | end = permutation[j]; | |
92 | 348928 | st->raster_end[i] = end; | |
93 | } | ||
94 | 5452 | } | |
95 | |||
96 | 916 | av_cold void ff_mpv_idct_init(MpegEncContext *s) | |
97 | { | ||
98 |
2/2✓ Branch 0 taken 253 times.
✓ Branch 1 taken 663 times.
|
916 | if (s->codec_id == AV_CODEC_ID_MPEG4) |
99 | 253 | s->idsp.mpeg4_studio_profile = s->studio_profile; | |
100 | 916 | ff_idctdsp_init(&s->idsp, s->avctx); | |
101 | |||
102 | /* load & permutate scantables | ||
103 | * note: only wmv uses different ones | ||
104 | */ | ||
105 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 912 times.
|
916 | if (s->alternate_scan) { |
106 | 4 | ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan); | |
107 | 4 | ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan); | |
108 | } else { | ||
109 | 912 | ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct); | |
110 | 912 | ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct); | |
111 | } | ||
112 | 916 | ff_permute_scantable(s->permutated_intra_h_scantable, ff_alternate_horizontal_scan, | |
113 | 916 | s->idsp.idct_permutation); | |
114 | 916 | ff_permute_scantable(s->permutated_intra_v_scantable, ff_alternate_vertical_scan, | |
115 | 916 | s->idsp.idct_permutation); | |
116 | 916 | } | |
117 | |||
118 | 847 | av_cold int ff_mpv_init_duplicate_contexts(MpegEncContext *s) | |
119 | { | ||
120 | 847 | const int nb_slices = s->slice_context_count; | |
121 | 847 | const size_t slice_size = s->slice_ctx_size; | |
122 | |||
123 |
2/2✓ Branch 0 taken 74 times.
✓ Branch 1 taken 847 times.
|
921 | for (int i = 1; i < nb_slices; i++) { |
124 | 74 | s->thread_context[i] = av_memdup(s, slice_size); | |
125 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 74 times.
|
74 | if (!s->thread_context[i]) |
126 | ✗ | return AVERROR(ENOMEM); | |
127 | 74 | s->thread_context[i]->start_mb_y = | |
128 | 74 | (s->mb_height * (i ) + nb_slices / 2) / nb_slices; | |
129 | 74 | s->thread_context[i]->end_mb_y = | |
130 | 74 | (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices; | |
131 | } | ||
132 | 847 | s->start_mb_y = 0; | |
133 | 869 | s->end_mb_y = nb_slices > 1 ? (s->mb_height + nb_slices / 2) / nb_slices | |
134 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 825 times.
|
847 | : s->mb_height; |
135 | 847 | return 0; | |
136 | } | ||
137 | |||
138 | 993 | static av_cold void free_duplicate_context(MpegEncContext *s) | |
139 | { | ||
140 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 993 times.
|
993 | if (!s) |
141 | ✗ | return; | |
142 | |||
143 | 993 | av_freep(&s->sc.edge_emu_buffer); | |
144 | 993 | av_freep(&s->sc.scratchpad_buf); | |
145 | 993 | s->sc.obmc_scratchpad = NULL; | |
146 | 993 | s->sc.linesize = 0; | |
147 | } | ||
148 | |||
149 | 919 | static av_cold void free_duplicate_contexts(MpegEncContext *s) | |
150 | { | ||
151 |
2/2✓ Branch 0 taken 74 times.
✓ Branch 1 taken 919 times.
|
993 | for (int i = 1; i < s->slice_context_count; i++) { |
152 | 74 | free_duplicate_context(s->thread_context[i]); | |
153 | 74 | av_freep(&s->thread_context[i]); | |
154 | } | ||
155 | 919 | free_duplicate_context(s); | |
156 | 919 | } | |
157 | |||
158 | 8316 | int ff_update_duplicate_context(MpegEncContext *dst, const MpegEncContext *src) | |
159 | { | ||
160 | #define COPY(M) \ | ||
161 | M(ScratchpadContext, sc) \ | ||
162 | M(int, start_mb_y) \ | ||
163 | M(int, end_mb_y) \ | ||
164 | M(int16_t*, dc_val) \ | ||
165 | M(void*, ac_val) | ||
166 | |||
167 | int ret; | ||
168 | // FIXME copy only needed parts | ||
169 | #define BACKUP(T, member) T member = dst->member; | ||
170 | 8316 | COPY(BACKUP) | |
171 | 8316 | memcpy(dst, src, sizeof(MpegEncContext)); | |
172 | #define RESTORE(T, member) dst->member = member; | ||
173 | 8316 | COPY(RESTORE) | |
174 | |||
175 | 8316 | ret = ff_mpv_framesize_alloc(dst->avctx, &dst->sc, dst->linesize); | |
176 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8316 times.
|
8316 | if (ret < 0) { |
177 | ✗ | av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context " | |
178 | "scratch buffers.\n"); | ||
179 | ✗ | return ret; | |
180 | } | ||
181 | 8316 | return 0; | |
182 | } | ||
183 | |||
184 | /** | ||
185 | * Set the given MpegEncContext to common defaults | ||
186 | * (same for encoding and decoding). | ||
187 | * The changed fields will not depend upon the | ||
188 | * prior state of the MpegEncContext. | ||
189 | */ | ||
190 | 838 | av_cold void ff_mpv_common_defaults(MpegEncContext *s) | |
191 | { | ||
192 | 838 | s->chroma_qscale_table = ff_default_chroma_qscale_table; | |
193 | 838 | s->progressive_frame = 1; | |
194 | 838 | s->progressive_sequence = 1; | |
195 | 838 | s->picture_structure = PICT_FRAME; | |
196 | |||
197 | 838 | s->slice_context_count = 1; | |
198 | 838 | } | |
199 | |||
200 | 919 | static av_cold void free_buffer_pools(BufferPoolContext *pools) | |
201 | { | ||
202 | 919 | av_refstruct_pool_uninit(&pools->mbskip_table_pool); | |
203 | 919 | av_refstruct_pool_uninit(&pools->qscale_table_pool); | |
204 | 919 | av_refstruct_pool_uninit(&pools->mb_type_pool); | |
205 | 919 | av_refstruct_pool_uninit(&pools->motion_val_pool); | |
206 | 919 | av_refstruct_pool_uninit(&pools->ref_index_pool); | |
207 | 919 | pools->alloc_mb_height = pools->alloc_mb_width = pools->alloc_mb_stride = 0; | |
208 | 919 | } | |
209 | |||
210 | 847 | av_cold int ff_mpv_init_context_frame(MpegEncContext *s) | |
211 | { | ||
212 | 847 | int nb_slices = (HAVE_THREADS && | |
213 | 847 | s->avctx->active_thread_type & FF_THREAD_SLICE) ? | |
214 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 825 times.
|
847 | s->avctx->thread_count : 1; |
215 | 847 | BufferPoolContext *const pools = &s->buffer_pools; | |
216 | int y_size, c_size, yc_size, mb_array_size, mv_table_size, x, y; | ||
217 | int mb_height; | ||
218 | |||
219 |
4/4✓ Branch 0 taken 204 times.
✓ Branch 1 taken 643 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 192 times.
|
847 | if (s->encoding && s->avctx->slices) |
220 | 12 | nb_slices = s->avctx->slices; | |
221 | |||
222 |
4/4✓ Branch 0 taken 280 times.
✓ Branch 1 taken 567 times.
✓ Branch 2 taken 159 times.
✓ Branch 3 taken 121 times.
|
847 | if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence) |
223 | 159 | s->mb_height = (s->height + 31) / 32 * 2; | |
224 | else | ||
225 | 688 | s->mb_height = (s->height + 15) / 16; | |
226 | |||
227 |
5/6✓ Branch 0 taken 847 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 834 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 12 times.
|
847 | if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) { |
228 | int max_slices; | ||
229 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (s->mb_height) |
230 | 1 | max_slices = FFMIN(MAX_THREADS, s->mb_height); | |
231 | else | ||
232 | ✗ | max_slices = MAX_THREADS; | |
233 | 1 | av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d)," | |
234 | " reducing to %d\n", nb_slices, max_slices); | ||
235 | 1 | nb_slices = max_slices; | |
236 | } | ||
237 | |||
238 | 847 | s->slice_context_count = nb_slices; | |
239 | |||
240 | /* VC-1 can change from being progressive to interlaced on a per-frame | ||
241 | * basis. We therefore allocate certain buffers so big that they work | ||
242 | * in both instances. */ | ||
243 | 1694 | mb_height = s->msmpeg4_version == MSMP4_VC1 ? | |
244 |
2/2✓ Branch 0 taken 34 times.
✓ Branch 1 taken 813 times.
|
847 | FFALIGN(s->mb_height, 2) : s->mb_height; |
245 | |||
246 | 847 | s->mb_width = (s->width + 15) / 16; | |
247 | 847 | s->mb_stride = s->mb_width + 1; | |
248 | 847 | s->b8_stride = s->mb_width * 2 + 1; | |
249 | 847 | mb_array_size = mb_height * s->mb_stride; | |
250 | 847 | mv_table_size = (mb_height + 2) * s->mb_stride + 1; | |
251 | |||
252 | /* set default edge pos, will be overridden | ||
253 | * in decode_header if needed */ | ||
254 | 847 | s->h_edge_pos = s->mb_width * 16; | |
255 | 847 | s->v_edge_pos = s->mb_height * 16; | |
256 | |||
257 | 847 | s->mb_num = s->mb_width * s->mb_height; | |
258 | |||
259 | 847 | s->block_wrap[0] = | |
260 | 847 | s->block_wrap[1] = | |
261 | 847 | s->block_wrap[2] = | |
262 | 847 | s->block_wrap[3] = s->b8_stride; | |
263 | 847 | s->block_wrap[4] = | |
264 | 847 | s->block_wrap[5] = s->mb_stride; | |
265 | |||
266 | 847 | y_size = s->b8_stride * (2 * mb_height + 1); | |
267 | 847 | c_size = s->mb_stride * (mb_height + 1); | |
268 | 847 | yc_size = y_size + 2 * c_size; | |
269 | |||
270 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 847 times.
|
847 | if (!FF_ALLOCZ_TYPED_ARRAY(s->mb_index2xy, s->mb_num + 1)) |
271 | ✗ | return AVERROR(ENOMEM); | |
272 |
2/2✓ Branch 0 taken 17043 times.
✓ Branch 1 taken 847 times.
|
17890 | for (y = 0; y < s->mb_height; y++) |
273 |
2/2✓ Branch 0 taken 552603 times.
✓ Branch 1 taken 17043 times.
|
569646 | for (x = 0; x < s->mb_width; x++) |
274 | 552603 | s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride; | |
275 | |||
276 | 847 | s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed? | |
277 | |||
278 | #define ALLOC_POOL(name, size, flags) do { \ | ||
279 | pools->name ##_pool = av_refstruct_pool_alloc((size), (flags)); \ | ||
280 | if (!pools->name ##_pool) \ | ||
281 | return AVERROR(ENOMEM); \ | ||
282 | } while (0) | ||
283 | |||
284 |
2/2✓ Branch 0 taken 568 times.
✓ Branch 1 taken 279 times.
|
847 | if (s->codec_id == AV_CODEC_ID_MPEG4 || |
285 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 552 times.
|
568 | (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)) { |
286 | /* interlaced direct mode decoding tables */ | ||
287 | 295 | int16_t (*tmp)[2] = av_calloc(mv_table_size, 4 * sizeof(*tmp)); | |
288 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 295 times.
|
295 | if (!tmp) |
289 | ✗ | return AVERROR(ENOMEM); | |
290 | 295 | s->p_field_mv_table_base = tmp; | |
291 | 295 | tmp += s->mb_stride + 1; | |
292 |
2/2✓ Branch 0 taken 590 times.
✓ Branch 1 taken 295 times.
|
885 | for (int i = 0; i < 2; i++) { |
293 |
2/2✓ Branch 0 taken 1180 times.
✓ Branch 1 taken 590 times.
|
1770 | for (int j = 0; j < 2; j++) { |
294 | 1180 | s->p_field_mv_table[i][j] = tmp; | |
295 | 1180 | tmp += mv_table_size; | |
296 | } | ||
297 | } | ||
298 |
2/2✓ Branch 0 taken 279 times.
✓ Branch 1 taken 16 times.
|
295 | if (s->codec_id == AV_CODEC_ID_MPEG4) { |
299 |
3/4✓ Branch 0 taken 216 times.
✓ Branch 1 taken 63 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 279 times.
|
279 | ALLOC_POOL(mbskip_table, mb_array_size + 2, |
300 | !s->encoding ? AV_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME : 0); | ||
301 |
2/2✓ Branch 0 taken 216 times.
✓ Branch 1 taken 63 times.
|
279 | if (!s->encoding) { |
302 | /* cbp, pred_dir */ | ||
303 |
1/2✓ Branch 1 taken 216 times.
✗ Branch 2 not taken.
|
216 | if (!(s->cbp_table = av_mallocz(mb_array_size)) || |
304 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 216 times.
|
216 | !(s->pred_dir_table = av_mallocz(mb_array_size))) |
305 | ✗ | return AVERROR(ENOMEM); | |
306 | } | ||
307 | } | ||
308 | } | ||
309 | |||
310 |
2/2✓ Branch 0 taken 81 times.
✓ Branch 1 taken 766 times.
|
847 | if (s->msmpeg4_version >= MSMP4_V3) { |
311 | 81 | s->coded_block_base = av_mallocz(y_size); | |
312 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
|
81 | if (!s->coded_block_base) |
313 | ✗ | return AVERROR(ENOMEM); | |
314 | 81 | s->coded_block = s->coded_block_base + s->b8_stride + 1; | |
315 | } | ||
316 | |||
317 |
6/6✓ Branch 0 taken 472 times.
✓ Branch 1 taken 375 times.
✓ Branch 2 taken 459 times.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 341 times.
✓ Branch 5 taken 118 times.
|
847 | if (s->h263_pred || s->h263_aic || !s->encoding) { |
318 | // When encoding, each slice (and therefore each thread) | ||
319 | // gets its own ac_val and dc_val buffers in order to avoid | ||
320 | // races. | ||
321 |
2/2✓ Branch 0 taken 86 times.
✓ Branch 1 taken 643 times.
|
729 | size_t allslice_yc_size = yc_size * (s->encoding ? nb_slices : 1); |
322 |
2/2✓ Branch 0 taken 462 times.
✓ Branch 1 taken 267 times.
|
729 | if (s->out_format == FMT_H263) { |
323 | /* ac values */ | ||
324 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 462 times.
|
462 | if (!FF_ALLOCZ_TYPED_ARRAY(s->ac_val_base, allslice_yc_size)) |
325 | ✗ | return AVERROR(ENOMEM); | |
326 | 462 | s->ac_val = s->ac_val_base + s->b8_stride + 1; | |
327 | } | ||
328 | |||
329 | /* dc values */ | ||
330 | // MN: we need these for error resilience of intra-frames | ||
331 | // Allocating them unconditionally for decoders also means | ||
332 | // that we don't need to reinitialize when e.g. h263_aic changes. | ||
333 | |||
334 | // y_size and therefore yc_size is always odd; allocate one element | ||
335 | // more for each encoder slice in order to be able to align each slice's | ||
336 | // dc_val to four in order to use aligned stores when cleaning dc_val. | ||
337 | 729 | allslice_yc_size += s->encoding * nb_slices; | |
338 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 729 times.
|
729 | if (!FF_ALLOC_TYPED_ARRAY(s->dc_val_base, allslice_yc_size)) |
339 | ✗ | return AVERROR(ENOMEM); | |
340 | 729 | s->dc_val = s->dc_val_base + s->b8_stride + 1; | |
341 |
2/2✓ Branch 0 taken 3175977 times.
✓ Branch 1 taken 729 times.
|
3176706 | for (size_t i = 0; i < allslice_yc_size; ++i) |
342 | 3175977 | s->dc_val_base[i] = 1024; | |
343 | } | ||
344 | |||
345 | // Note the + 1 is for a quicker MPEG-4 slice_end detection | ||
346 |
1/2✓ Branch 1 taken 847 times.
✗ Branch 2 not taken.
|
847 | if (!(s->mbskip_table = av_mallocz(mb_array_size + 2)) || |
347 | /* which mb is an intra block, init macroblock skip table */ | ||
348 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 847 times.
|
847 | !(s->mbintra_table = av_mallocz(mb_array_size))) |
349 | ✗ | return AVERROR(ENOMEM); | |
350 | |||
351 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 847 times.
|
847 | ALLOC_POOL(qscale_table, mv_table_size, 0); |
352 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 847 times.
|
847 | ALLOC_POOL(mb_type, mv_table_size * sizeof(uint32_t), 0); |
353 | |||
354 |
4/4✓ Branch 0 taken 368 times.
✓ Branch 1 taken 479 times.
✓ Branch 2 taken 267 times.
✓ Branch 3 taken 101 times.
|
847 | if (s->out_format == FMT_H263 || s->encoding || |
355 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 267 times.
|
267 | (s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_MVS)) { |
356 | 580 | const int b8_array_size = s->b8_stride * mb_height * 2; | |
357 | 580 | int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t); | |
358 | 580 | int ref_index_size = 4 * mb_array_size; | |
359 | |||
360 | /* FIXME: The output of H.263 with OBMC depends upon | ||
361 | * the earlier content of the buffer; therefore we set | ||
362 | * the flags to always reset returned buffers here. */ | ||
363 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 580 times.
|
580 | ALLOC_POOL(motion_val, mv_size, AV_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME); |
364 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 580 times.
|
580 | ALLOC_POOL(ref_index, ref_index_size, 0); |
365 | } | ||
366 | #undef ALLOC_POOL | ||
367 | 847 | pools->alloc_mb_width = s->mb_width; | |
368 | 847 | pools->alloc_mb_height = mb_height; | |
369 | 847 | pools->alloc_mb_stride = s->mb_stride; | |
370 | |||
371 |
2/2✓ Branch 0 taken 643 times.
✓ Branch 1 taken 204 times.
|
847 | return !CONFIG_MPEGVIDEODEC || s->encoding ? 0 : ff_mpeg_er_init(s); |
372 | } | ||
373 | |||
374 | /** | ||
375 | * init common structure for both encoder and decoder. | ||
376 | * this assumes that some variables like width/height are already set | ||
377 | */ | ||
378 | 800 | av_cold int ff_mpv_common_init(MpegEncContext *s) | |
379 | { | ||
380 | int ret; | ||
381 | |||
382 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 800 times.
|
800 | if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) { |
383 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
384 | "decoding to AV_PIX_FMT_NONE is not supported.\n"); | ||
385 | ✗ | return AVERROR(EINVAL); | |
386 | } | ||
387 | |||
388 |
4/6✓ Branch 0 taken 12 times.
✓ Branch 1 taken 788 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 788 times.
|
1588 | if ((s->width || s->height) && |
389 | 788 | av_image_check_size(s->width, s->height, 0, s->avctx)) | |
390 | ✗ | return AVERROR(EINVAL); | |
391 | |||
392 | 800 | dsp_init(s); | |
393 | |||
394 | /* set chroma shifts */ | ||
395 | 800 | ret = av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt, | |
396 | &s->chroma_x_shift, | ||
397 | &s->chroma_y_shift); | ||
398 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 800 times.
|
800 | if (ret) |
399 | ✗ | return ret; | |
400 | |||
401 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 800 times.
|
800 | if ((ret = ff_mpv_init_context_frame(s))) |
402 | ✗ | goto fail; | |
403 | |||
404 | 800 | s->context_initialized = 1; | |
405 | 800 | s->thread_context[0] = s; | |
406 | |||
407 | // if (s->width && s->height) { | ||
408 |
2/2✓ Branch 0 taken 596 times.
✓ Branch 1 taken 204 times.
|
800 | if (!s->encoding) { |
409 | 596 | ret = ff_mpv_init_duplicate_contexts(s); | |
410 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 596 times.
|
596 | if (ret < 0) |
411 | ✗ | goto fail; | |
412 | } | ||
413 | // } | ||
414 | |||
415 | 800 | return 0; | |
416 | ✗ | fail: | |
417 | ✗ | ff_mpv_common_end(s); | |
418 | ✗ | return ret; | |
419 | } | ||
420 | |||
421 | 919 | av_cold void ff_mpv_free_context_frame(MpegEncContext *s) | |
422 | { | ||
423 | 919 | free_duplicate_contexts(s); | |
424 | |||
425 | 919 | free_buffer_pools(&s->buffer_pools); | |
426 | 919 | av_freep(&s->p_field_mv_table_base); | |
427 |
2/2✓ Branch 0 taken 1838 times.
✓ Branch 1 taken 919 times.
|
2757 | for (int i = 0; i < 2; i++) |
428 |
2/2✓ Branch 0 taken 3676 times.
✓ Branch 1 taken 1838 times.
|
5514 | for (int j = 0; j < 2; j++) |
429 | 3676 | s->p_field_mv_table[i][j] = NULL; | |
430 | |||
431 | 919 | av_freep(&s->ac_val_base); | |
432 | 919 | av_freep(&s->dc_val_base); | |
433 | 919 | av_freep(&s->coded_block_base); | |
434 | 919 | av_freep(&s->mbintra_table); | |
435 | 919 | av_freep(&s->cbp_table); | |
436 | 919 | av_freep(&s->pred_dir_table); | |
437 | |||
438 | 919 | av_freep(&s->mbskip_table); | |
439 | |||
440 | 919 | av_freep(&s->er.error_status_table); | |
441 | 919 | av_freep(&s->er.er_temp_buffer); | |
442 | 919 | av_freep(&s->mb_index2xy); | |
443 | |||
444 | 919 | s->linesize = s->uvlinesize = 0; | |
445 | 919 | } | |
446 | |||
447 | 872 | av_cold void ff_mpv_common_end(MpegEncContext *s) | |
448 | { | ||
449 | 872 | ff_mpv_free_context_frame(s); | |
450 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 850 times.
|
872 | if (s->slice_context_count > 1) |
451 | 22 | s->slice_context_count = 1; | |
452 | |||
453 | 872 | ff_mpv_unref_picture(&s->last_pic); | |
454 | 872 | ff_mpv_unref_picture(&s->cur_pic); | |
455 | 872 | ff_mpv_unref_picture(&s->next_pic); | |
456 | |||
457 | 872 | s->context_initialized = 0; | |
458 | 872 | s->context_reinit = 0; | |
459 | 872 | s->linesize = s->uvlinesize = 0; | |
460 | 872 | } | |
461 | |||
462 | |||
463 | /** | ||
464 | * Clean dc, ac for the current non-intra MB. | ||
465 | */ | ||
466 | 394875 | void ff_clean_intra_table_entries(MpegEncContext *s) | |
467 | { | ||
468 | 394875 | int wrap = s->b8_stride; | |
469 | 394875 | int xy = s->block_index[0]; | |
470 | /* chroma */ | ||
471 | 394875 | unsigned uxy = s->block_index[4]; | |
472 | 394875 | unsigned vxy = s->block_index[5]; | |
473 | 394875 | int16_t *dc_val = s->dc_val; | |
474 | |||
475 | 394875 | AV_WN32A(dc_val + xy, 1024 << 16 | 1024); | |
476 | 394875 | AV_WN32 (dc_val + xy + wrap, 1024 << 16 | 1024); | |
477 | 394875 | dc_val[uxy] = | |
478 | 394875 | dc_val[vxy] = 1024; | |
479 | /* ac pred */ | ||
480 | 394875 | int16_t (*ac_val)[16] = s->ac_val; | |
481 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 394875 times.
|
394875 | av_assume(!((uintptr_t)ac_val & 0xF)); |
482 | // Don't reset the upper-left luma block, as it will only ever be | ||
483 | // referenced by blocks from the same macroblock. | ||
484 | 394875 | memset(ac_val[xy + 1], 0, sizeof(*ac_val)); | |
485 | 394875 | memset(ac_val[xy + wrap], 0, 2 * sizeof(*ac_val)); | |
486 | /* ac pred */ | ||
487 | 394875 | memset(ac_val[uxy], 0, sizeof(*ac_val)); | |
488 | 394875 | memset(ac_val[vxy], 0, sizeof(*ac_val)); | |
489 | 394875 | } | |
490 | |||
491 | 840959 | void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename | |
492 | 840959 | const int linesize = s->cur_pic.linesize[0]; //not s->linesize as this would be wrong for field pics | |
493 | 840959 | const int uvlinesize = s->cur_pic.linesize[1]; | |
494 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 840929 times.
|
840959 | const int width_of_mb = (4 + (s->avctx->bits_per_raw_sample > 8)) - s->avctx->lowres; |
495 | 840959 | const int height_of_mb = 4 - s->avctx->lowres; | |
496 | |||
497 | 840959 | s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2; | |
498 | 840959 | s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2; | |
499 | 840959 | s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2; | |
500 | 840959 | s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2; | |
501 | 840959 | s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1; | |
502 | 840959 | s->block_index[5]= s->mb_stride*(s->mb_y + s->mb_height + 2) + s->b8_stride*s->mb_height*2 + s->mb_x - 1; | |
503 | //block_index is not used by mpeg2, so it is not affected by chroma_format | ||
504 | |||
505 | 840959 | s->dest[0] = s->cur_pic.data[0] + (int)((s->mb_x - 1U) << width_of_mb); | |
506 | 840959 | s->dest[1] = s->cur_pic.data[1] + (int)((s->mb_x - 1U) << (width_of_mb - s->chroma_x_shift)); | |
507 | 840959 | s->dest[2] = s->cur_pic.data[2] + (int)((s->mb_x - 1U) << (width_of_mb - s->chroma_x_shift)); | |
508 | |||
509 |
2/2✓ Branch 0 taken 801015 times.
✓ Branch 1 taken 39944 times.
|
840959 | if (s->picture_structure == PICT_FRAME) { |
510 | 801015 | s->dest[0] += s->mb_y * linesize << height_of_mb; | |
511 | 801015 | s->dest[1] += s->mb_y * uvlinesize << (height_of_mb - s->chroma_y_shift); | |
512 | 801015 | s->dest[2] += s->mb_y * uvlinesize << (height_of_mb - s->chroma_y_shift); | |
513 | } else { | ||
514 | 39944 | s->dest[0] += (s->mb_y>>1) * linesize << height_of_mb; | |
515 | 39944 | s->dest[1] += (s->mb_y>>1) * uvlinesize << (height_of_mb - s->chroma_y_shift); | |
516 | 39944 | s->dest[2] += (s->mb_y>>1) * uvlinesize << (height_of_mb - s->chroma_y_shift); | |
517 | av_assert1((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD)); | ||
518 | } | ||
519 | 840959 | } | |
520 | |||
521 | /** | ||
522 | * set qscale and update qscale dependent variables. | ||
523 | */ | ||
524 | 1454638 | void ff_set_qscale(MpegEncContext * s, int qscale) | |
525 | { | ||
526 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1454637 times.
|
1454638 | if (qscale < 1) |
527 | 1 | qscale = 1; | |
528 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 1454608 times.
|
1454637 | else if (qscale > 31) |
529 | 29 | qscale = 31; | |
530 | |||
531 | 1454638 | s->qscale = qscale; | |
532 | 1454638 | s->chroma_qscale= s->chroma_qscale_table[qscale]; | |
533 | |||
534 | 1454638 | s->y_dc_scale= s->y_dc_scale_table[ qscale ]; | |
535 | 1454638 | s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ]; | |
536 | 1454638 | } | |
537 |