Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * DV encoder | ||
3 | * Copyright (c) 2003 Roman Shaposhnik | ||
4 | * | ||
5 | * This file is part of FFmpeg. | ||
6 | * | ||
7 | * FFmpeg is free software; you can redistribute it and/or | ||
8 | * modify it under the terms of the GNU Lesser General Public | ||
9 | * License as published by the Free Software Foundation; either | ||
10 | * version 2.1 of the License, or (at your option) any later version. | ||
11 | * | ||
12 | * FFmpeg is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with FFmpeg; if not, write to the Free Software | ||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
20 | * | ||
21 | * quant_deadzone code and fixes sponsored by NOA GmbH | ||
22 | */ | ||
23 | |||
24 | /** | ||
25 | * @file | ||
26 | * DV encoder | ||
27 | */ | ||
28 | |||
29 | #include "config.h" | ||
30 | |||
31 | #include "libavutil/attributes.h" | ||
32 | #include "libavutil/emms.h" | ||
33 | #include "libavutil/internal.h" | ||
34 | #include "libavutil/mem_internal.h" | ||
35 | #include "libavutil/opt.h" | ||
36 | #include "libavutil/pixdesc.h" | ||
37 | #include "libavutil/thread.h" | ||
38 | |||
39 | #include "avcodec.h" | ||
40 | #include "codec_internal.h" | ||
41 | #include "dv.h" | ||
42 | #include "dv_internal.h" | ||
43 | #include "dv_profile_internal.h" | ||
44 | #include "dv_tablegen.h" | ||
45 | #include "encode.h" | ||
46 | #include "fdctdsp.h" | ||
47 | #include "mathops.h" | ||
48 | #include "me_cmp.h" | ||
49 | #include "pixblockdsp.h" | ||
50 | #include "put_bits.h" | ||
51 | |||
52 | typedef struct DVEncContext { | ||
53 | const AVClass *class; | ||
54 | const AVDVProfile *sys; | ||
55 | const AVFrame *frame; | ||
56 | AVCodecContext *avctx; | ||
57 | uint8_t *buf; | ||
58 | |||
59 | void (*get_pixels)(int16_t *block, const uint8_t *pixels, ptrdiff_t linesize); | ||
60 | void (*fdct[2])(int16_t *block); | ||
61 | |||
62 | me_cmp_func ildct_cmp; | ||
63 | DVwork_chunk work_chunks[4 * 12 * 27]; | ||
64 | |||
65 | int quant_deadzone; | ||
66 | } DVEncContext; | ||
67 | |||
68 | |||
69 | 23 | static av_cold int dvvideo_encode_init(AVCodecContext *avctx) | |
70 | { | ||
71 | 23 | DVEncContext *s = avctx->priv_data; | |
72 | FDCTDSPContext fdsp; | ||
73 | PixblockDSPContext pdsp; | ||
74 | int ret; | ||
75 | |||
76 | 23 | s->avctx = avctx; | |
77 | |||
78 |
1/2✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
|
23 | if (avctx->chroma_sample_location != AVCHROMA_LOC_TOPLEFT) { |
79 | 23 | const char *name = av_chroma_location_name(avctx->chroma_sample_location); | |
80 |
1/2✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
|
23 | av_log(avctx, AV_LOG_WARNING, "Only top-left chroma location is supported " |
81 | "in DV, input value is: %s\n", name ? name : "unknown"); | ||
82 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | if (avctx->strict_std_compliance > FF_COMPLIANCE_NORMAL) |
83 | ✗ | return AVERROR(EINVAL); | |
84 | } | ||
85 | |||
86 | 23 | s->sys = av_dv_codec_profile2(avctx->width, avctx->height, avctx->pix_fmt, avctx->time_base); | |
87 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | if (!s->sys) { |
88 | ✗ | av_log(avctx, AV_LOG_ERROR, "Found no DV profile for %ix%i %s video. " | |
89 | "Valid DV profiles are:\n", | ||
90 | avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt)); | ||
91 | ✗ | ff_dv_print_profiles(avctx, AV_LOG_ERROR); | |
92 | ✗ | return AVERROR(EINVAL); | |
93 | } | ||
94 | |||
95 | 23 | ff_dv_init_dynamic_tables(s->work_chunks, s->sys); | |
96 | |||
97 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) { |
98 | MECmpContext mecc; | ||
99 | me_cmp_func ildct_cmp[6]; | ||
100 | |||
101 | ✗ | ff_me_cmp_init(&mecc, avctx); | |
102 | ✗ | ret = ff_set_cmp(&mecc, ildct_cmp, avctx->ildct_cmp, 0); | |
103 | ✗ | if (ret < 0) | |
104 | ✗ | return ret; | |
105 | ✗ | if (!ildct_cmp[5]) | |
106 | ✗ | return AVERROR(EINVAL); | |
107 | ✗ | s->ildct_cmp = ildct_cmp[5]; | |
108 | } | ||
109 | |||
110 | 23 | memset(&fdsp,0, sizeof(fdsp)); | |
111 | 23 | memset(&pdsp,0, sizeof(pdsp)); | |
112 | 23 | ff_fdctdsp_init(&fdsp, avctx); | |
113 | 23 | ff_pixblockdsp_init(&pdsp, avctx); | |
114 | 23 | s->get_pixels = pdsp.get_pixels; | |
115 | 23 | s->fdct[0] = fdsp.fdct; | |
116 | 23 | s->fdct[1] = fdsp.fdct248; | |
117 | |||
118 | #if !CONFIG_HARDCODED_TABLES | ||
119 | { | ||
120 | static AVOnce init_static_once = AV_ONCE_INIT; | ||
121 | 23 | ff_thread_once(&init_static_once, dv_vlc_map_tableinit); | |
122 | } | ||
123 | #endif | ||
124 | |||
125 | 23 | return 0; | |
126 | } | ||
127 | |||
128 | /* bit budget for AC only in 5 MBs */ | ||
129 | static const int vs_total_ac_bits_hd = (68 * 6 + 52*2) * 5; | ||
130 | static const int vs_total_ac_bits = (100 * 4 + 68 * 2) * 5; | ||
131 | static const int mb_area_start[5] = { 1, 6, 21, 43, 64 }; | ||
132 | |||
133 | #if CONFIG_SMALL | ||
134 | /* Convert run and level (where level != 0) pair into VLC, returning bit size */ | ||
135 | static av_always_inline int dv_rl2vlc(int run, int level, int sign, | ||
136 | uint32_t *vlc) | ||
137 | { | ||
138 | int size; | ||
139 | if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) { | ||
140 | *vlc = dv_vlc_map[run][level].vlc | sign; | ||
141 | size = dv_vlc_map[run][level].size; | ||
142 | } else { | ||
143 | if (level < DV_VLC_MAP_LEV_SIZE) { | ||
144 | *vlc = dv_vlc_map[0][level].vlc | sign; | ||
145 | size = dv_vlc_map[0][level].size; | ||
146 | } else { | ||
147 | *vlc = 0xfe00 | (level << 1) | sign; | ||
148 | size = 16; | ||
149 | } | ||
150 | if (run) { | ||
151 | *vlc |= ((run < 16) ? dv_vlc_map[run - 1][0].vlc : | ||
152 | (0x1f80 | (run - 1))) << size; | ||
153 | size += (run < 16) ? dv_vlc_map[run - 1][0].size : 13; | ||
154 | } | ||
155 | } | ||
156 | |||
157 | return size; | ||
158 | } | ||
159 | |||
160 | static av_always_inline int dv_rl2vlc_size(int run, int level) | ||
161 | { | ||
162 | int size; | ||
163 | |||
164 | if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) { | ||
165 | size = dv_vlc_map[run][level].size; | ||
166 | } else { | ||
167 | size = (level < DV_VLC_MAP_LEV_SIZE) ? dv_vlc_map[0][level].size : 16; | ||
168 | if (run) | ||
169 | size += (run < 16) ? dv_vlc_map[run - 1][0].size : 13; | ||
170 | } | ||
171 | return size; | ||
172 | } | ||
173 | #else | ||
174 | 205750889 | static av_always_inline int dv_rl2vlc(int run, int l, int sign, uint32_t *vlc) | |
175 | { | ||
176 | 205750889 | *vlc = dv_vlc_map[run][l].vlc | sign; | |
177 | 205750889 | return dv_vlc_map[run][l].size; | |
178 | } | ||
179 | |||
180 | 653621732 | static av_always_inline int dv_rl2vlc_size(int run, int l) | |
181 | { | ||
182 | 653621732 | return dv_vlc_map[run][l].size; | |
183 | } | ||
184 | #endif | ||
185 | |||
186 | typedef struct EncBlockInfo { | ||
187 | int area_q[4]; | ||
188 | int bit_size[4]; | ||
189 | int prev[5]; | ||
190 | int cur_ac; | ||
191 | int cno; | ||
192 | int dct_mode; | ||
193 | int16_t mb[64]; | ||
194 | uint8_t next[64]; | ||
195 | uint8_t sign[64]; | ||
196 | uint8_t partial_bit_count; | ||
197 | uint32_t partial_bit_buffer; /* we can't use uint16_t here */ | ||
198 | /* used by DV100 only: a copy of the weighted and classified but | ||
199 | not-yet-quantized AC coefficients. This is necessary for | ||
200 | re-quantizing at different steps. */ | ||
201 | int16_t save[64]; | ||
202 | int min_qlevel; /* DV100 only: minimum qlevel (for AC coefficients >255) */ | ||
203 | } EncBlockInfo; | ||
204 | |||
205 | 34301211 | static av_always_inline PutBitContext *dv_encode_ac(EncBlockInfo *bi, | |
206 | PutBitContext *pb_pool, | ||
207 | PutBitContext *pb_end) | ||
208 | { | ||
209 | int prev, bits_left; | ||
210 | 34301211 | PutBitContext *pb = pb_pool; | |
211 | 34301211 | int size = bi->partial_bit_count; | |
212 | 34301211 | uint32_t vlc = bi->partial_bit_buffer; | |
213 | |||
214 | 34301211 | bi->partial_bit_count = | |
215 | 34301211 | bi->partial_bit_buffer = 0; | |
216 | for (;;) { | ||
217 | /* Find suitable storage space */ | ||
218 |
2/2✓ Branch 1 taken 33621069 times.
✓ Branch 2 taken 250840889 times.
|
284461958 | for (; size > (bits_left = put_bits_left(pb)); pb++) { |
219 |
2/2✓ Branch 0 taken 14151902 times.
✓ Branch 1 taken 19469167 times.
|
33621069 | if (bits_left) { |
220 | 14151902 | size -= bits_left; | |
221 | 14151902 | put_bits(pb, bits_left, vlc >> size); | |
222 | 14151902 | vlc = av_zero_extend(vlc, size); | |
223 | } | ||
224 |
2/2✓ Branch 0 taken 11756211 times.
✓ Branch 1 taken 21864858 times.
|
33621069 | if (pb + 1 >= pb_end) { |
225 | 11756211 | bi->partial_bit_count = size; | |
226 | 11756211 | bi->partial_bit_buffer = vlc; | |
227 | 11756211 | return pb; | |
228 | } | ||
229 | } | ||
230 | |||
231 | /* Store VLC */ | ||
232 | 250840889 | put_bits(pb, size, vlc); | |
233 | |||
234 |
2/2✓ Branch 0 taken 22545000 times.
✓ Branch 1 taken 228295889 times.
|
250840889 | if (bi->cur_ac >= 64) |
235 | 22545000 | break; | |
236 | |||
237 | /* Construct the next VLC */ | ||
238 | 228295889 | prev = bi->cur_ac; | |
239 | 228295889 | bi->cur_ac = bi->next[prev]; | |
240 |
2/2✓ Branch 0 taken 205750889 times.
✓ Branch 1 taken 22545000 times.
|
228295889 | if (bi->cur_ac < 64) { |
241 | 205750889 | size = dv_rl2vlc(bi->cur_ac - prev - 1, bi->mb[bi->cur_ac], | |
242 | 205750889 | bi->sign[bi->cur_ac], &vlc); | |
243 | } else { | ||
244 | 22545000 | size = 4; | |
245 | 22545000 | vlc = 6; /* End Of Block stamp */ | |
246 | } | ||
247 | } | ||
248 | 22545000 | return pb; | |
249 | } | ||
250 | |||
251 | 7512750 | static av_always_inline int dv_guess_dct_mode(DVEncContext *s, const uint8_t *data, | |
252 | ptrdiff_t linesize) | ||
253 | { | ||
254 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7512750 times.
|
7512750 | if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) { |
255 | ✗ | int ps = s->ildct_cmp(NULL, data, NULL, linesize, 8) - 400; | |
256 | ✗ | if (ps > 0) { | |
257 | ✗ | int is = s->ildct_cmp(NULL, data, NULL, linesize * 2, 4) + | |
258 | ✗ | s->ildct_cmp(NULL, data + linesize, NULL, linesize * 2, 4); | |
259 | ✗ | return ps > is; | |
260 | } | ||
261 | } | ||
262 | |||
263 | 7512750 | return 0; | |
264 | } | ||
265 | |||
266 | static const int dv_weight_bits = 18; | ||
267 | static const int dv_weight_88[64] = { | ||
268 | 131072, 257107, 257107, 242189, 252167, 242189, 235923, 237536, | ||
269 | 237536, 235923, 229376, 231390, 223754, 231390, 229376, 222935, | ||
270 | 224969, 217965, 217965, 224969, 222935, 200636, 218652, 211916, | ||
271 | 212325, 211916, 218652, 200636, 188995, 196781, 205965, 206433, | ||
272 | 206433, 205965, 196781, 188995, 185364, 185364, 200636, 200704, | ||
273 | 200636, 185364, 185364, 174609, 180568, 195068, 195068, 180568, | ||
274 | 174609, 170091, 175557, 189591, 175557, 170091, 165371, 170627, | ||
275 | 170627, 165371, 160727, 153560, 160727, 144651, 144651, 136258, | ||
276 | }; | ||
277 | static const int dv_weight_248[64] = { | ||
278 | 131072, 262144, 257107, 257107, 242189, 242189, 242189, 242189, | ||
279 | 237536, 237536, 229376, 229376, 200636, 200636, 224973, 224973, | ||
280 | 223754, 223754, 235923, 235923, 229376, 229376, 217965, 217965, | ||
281 | 211916, 211916, 196781, 196781, 185364, 185364, 206433, 206433, | ||
282 | 211916, 211916, 222935, 222935, 200636, 200636, 205964, 205964, | ||
283 | 200704, 200704, 180568, 180568, 175557, 175557, 195068, 195068, | ||
284 | 185364, 185364, 188995, 188995, 174606, 174606, 175557, 175557, | ||
285 | 170627, 170627, 153560, 153560, 165371, 165371, 144651, 144651, | ||
286 | }; | ||
287 | |||
288 | /* setting this to 1 results in a faster codec but | ||
289 | * somewhat lower image quality */ | ||
290 | #define DV100_SACRIFICE_QUALITY_FOR_SPEED 1 | ||
291 | #define DV100_ENABLE_FINER 1 | ||
292 | |||
293 | /* pack combination of QNO and CNO into a single 8-bit value */ | ||
294 | #define DV100_MAKE_QLEVEL(qno,cno) ((qno<<2) | (cno)) | ||
295 | #define DV100_QLEVEL_QNO(qlevel) (qlevel>>2) | ||
296 | #define DV100_QLEVEL_CNO(qlevel) (qlevel&0x3) | ||
297 | |||
298 | #define DV100_NUM_QLEVELS 31 | ||
299 | |||
300 | /* The quantization step is determined by a combination of QNO and | ||
301 | CNO. We refer to these combinations as "qlevels" (this term is our | ||
302 | own, it's not mentioned in the spec). We use CNO, a multiplier on | ||
303 | the quantization step, to "fill in the gaps" between quantization | ||
304 | steps associated with successive values of QNO. e.g. there is no | ||
305 | QNO for a quantization step of 10, but we can use QNO=5 CNO=1 to | ||
306 | get the same result. The table below encodes combinations of QNO | ||
307 | and CNO in order of increasing quantization coarseness. */ | ||
308 | static const uint8_t dv100_qlevels[DV100_NUM_QLEVELS] = { | ||
309 | DV100_MAKE_QLEVEL( 1,0), // 1*1= 1 | ||
310 | DV100_MAKE_QLEVEL( 1,0), // 1*1= 1 | ||
311 | DV100_MAKE_QLEVEL( 2,0), // 2*1= 2 | ||
312 | DV100_MAKE_QLEVEL( 3,0), // 3*1= 3 | ||
313 | DV100_MAKE_QLEVEL( 4,0), // 4*1= 4 | ||
314 | DV100_MAKE_QLEVEL( 5,0), // 5*1= 5 | ||
315 | DV100_MAKE_QLEVEL( 6,0), // 6*1= 6 | ||
316 | DV100_MAKE_QLEVEL( 7,0), // 7*1= 7 | ||
317 | DV100_MAKE_QLEVEL( 8,0), // 8*1= 8 | ||
318 | DV100_MAKE_QLEVEL( 5,1), // 5*2=10 | ||
319 | DV100_MAKE_QLEVEL( 6,1), // 6*2=12 | ||
320 | DV100_MAKE_QLEVEL( 7,1), // 7*2=14 | ||
321 | DV100_MAKE_QLEVEL( 9,0), // 16*1=16 | ||
322 | DV100_MAKE_QLEVEL(10,0), // 18*1=18 | ||
323 | DV100_MAKE_QLEVEL(11,0), // 20*1=20 | ||
324 | DV100_MAKE_QLEVEL(12,0), // 22*1=22 | ||
325 | DV100_MAKE_QLEVEL(13,0), // 24*1=24 | ||
326 | DV100_MAKE_QLEVEL(14,0), // 28*1=28 | ||
327 | DV100_MAKE_QLEVEL( 9,1), // 16*2=32 | ||
328 | DV100_MAKE_QLEVEL(10,1), // 18*2=36 | ||
329 | DV100_MAKE_QLEVEL(11,1), // 20*2=40 | ||
330 | DV100_MAKE_QLEVEL(12,1), // 22*2=44 | ||
331 | DV100_MAKE_QLEVEL(13,1), // 24*2=48 | ||
332 | DV100_MAKE_QLEVEL(15,0), // 52*1=52 | ||
333 | DV100_MAKE_QLEVEL(14,1), // 28*2=56 | ||
334 | DV100_MAKE_QLEVEL( 9,2), // 16*4=64 | ||
335 | DV100_MAKE_QLEVEL(10,2), // 18*4=72 | ||
336 | DV100_MAKE_QLEVEL(11,2), // 20*4=80 | ||
337 | DV100_MAKE_QLEVEL(12,2), // 22*4=88 | ||
338 | DV100_MAKE_QLEVEL(13,2), // 24*4=96 | ||
339 | // ... | ||
340 | DV100_MAKE_QLEVEL(15,3), // 52*8=416 | ||
341 | }; | ||
342 | |||
343 | static const int dv100_min_bias = 0; | ||
344 | static const int dv100_chroma_bias = 0; | ||
345 | static const int dv100_starting_qno = 1; | ||
346 | |||
347 | #if DV100_SACRIFICE_QUALITY_FOR_SPEED | ||
348 | static const int dv100_qlevel_inc = 4; | ||
349 | #else | ||
350 | static const int dv100_qlevel_inc = 1; | ||
351 | #endif | ||
352 | |||
353 | // 1/qstep, shifted up by 16 bits | ||
354 | static const int dv100_qstep_bits = 16; | ||
355 | static const int dv100_qstep_inv[16] = { | ||
356 | 65536, 65536, 32768, 21845, 16384, 13107, 10923, 9362, 8192, 4096, 3641, 3277, 2979, 2731, 2341, 1260, | ||
357 | }; | ||
358 | |||
359 | /* DV100 weights are pre-zigzagged, inverted and multiplied by 2^16 | ||
360 | (in DV100 the AC components are divided by the spec weights) */ | ||
361 | static const int dv_weight_1080[2][64] = { | ||
362 | { 8192, 65536, 65536, 61681, 61681, 61681, 58254, 58254, | ||
363 | 58254, 58254, 58254, 58254, 55188, 58254, 58254, 55188, | ||
364 | 55188, 55188, 55188, 55188, 55188, 24966, 27594, 26214, | ||
365 | 26214, 26214, 27594, 24966, 23831, 24385, 25575, 25575, | ||
366 | 25575, 25575, 24385, 23831, 23302, 23302, 24966, 24966, | ||
367 | 24966, 23302, 23302, 21845, 22795, 24385, 24385, 22795, | ||
368 | 21845, 21400, 21845, 23831, 21845, 21400, 10382, 10700, | ||
369 | 10700, 10382, 10082, 9620, 10082, 9039, 9039, 8525, }, | ||
370 | { 8192, 65536, 65536, 61681, 61681, 61681, 41943, 41943, | ||
371 | 41943, 41943, 40330, 41943, 40330, 41943, 40330, 40330, | ||
372 | 40330, 38836, 38836, 40330, 40330, 24966, 27594, 26214, | ||
373 | 26214, 26214, 27594, 24966, 23831, 24385, 25575, 25575, | ||
374 | 25575, 25575, 24385, 23831, 11523, 11523, 12483, 12483, | ||
375 | 12483, 11523, 11523, 10923, 11275, 12193, 12193, 11275, | ||
376 | 10923, 5323, 5490, 5924, 5490, 5323, 5165, 5323, | ||
377 | 5323, 5165, 5017, 4788, 5017, 4520, 4520, 4263, } | ||
378 | }; | ||
379 | |||
380 | static const int dv_weight_720[2][64] = { | ||
381 | { 8192, 65536, 65536, 61681, 61681, 61681, 58254, 58254, | ||
382 | 58254, 58254, 58254, 58254, 55188, 58254, 58254, 55188, | ||
383 | 55188, 55188, 55188, 55188, 55188, 24966, 27594, 26214, | ||
384 | 26214, 26214, 27594, 24966, 23831, 24385, 25575, 25575, | ||
385 | 25575, 25575, 24385, 23831, 15420, 15420, 16644, 16644, | ||
386 | 16644, 15420, 15420, 10923, 11398, 12193, 12193, 11398, | ||
387 | 10923, 10700, 10923, 11916, 10923, 10700, 5191, 5350, | ||
388 | 5350, 5191, 5041, 4810, 5041, 4520, 4520, 4263, }, | ||
389 | { 8192, 43691, 43691, 40330, 40330, 40330, 29127, 29127, | ||
390 | 29127, 29127, 29127, 29127, 27594, 29127, 29127, 27594, | ||
391 | 27594, 27594, 27594, 27594, 27594, 12483, 13797, 13107, | ||
392 | 13107, 13107, 13797, 12483, 11916, 12193, 12788, 12788, | ||
393 | 12788, 12788, 12193, 11916, 5761, 5761, 6242, 6242, | ||
394 | 6242, 5761, 5761, 5461, 5638, 5461, 6096, 5638, | ||
395 | 5461, 2661, 2745, 2962, 2745, 2661, 2583, 2661, | ||
396 | 2661, 2583, 2509, 2394, 2509, 2260, 2260, 2131, } | ||
397 | }; | ||
398 | |||
399 | 7290000 | static av_always_inline int dv_set_class_number_sd(DVEncContext *s, | |
400 | int16_t *blk, EncBlockInfo *bi, | ||
401 | const uint8_t *zigzag_scan, | ||
402 | const int *weight, int bias) | ||
403 | { | ||
404 | int i, area; | ||
405 | /* We offer two different methods for class number assignment: the | ||
406 | * method suggested in SMPTE 314M Table 22, and an improved | ||
407 | * method. The SMPTE method is very conservative; it assigns class | ||
408 | * 3 (i.e. severe quantization) to any block where the largest AC | ||
409 | * component is greater than 36. FFmpeg's DV encoder tracks AC bit | ||
410 | * consumption precisely, so there is no need to bias most blocks | ||
411 | * towards strongly lossy compression. Instead, we assign class 2 | ||
412 | * to most blocks, and use class 3 only when strictly necessary | ||
413 | * (for blocks whose largest AC component exceeds 255). */ | ||
414 | |||
415 | #if 0 /* SMPTE spec method */ | ||
416 | static const int classes[] = { 12, 24, 36, 0xffff }; | ||
417 | #else /* improved FFmpeg method */ | ||
418 | static const int classes[] = { -1, -1, 255, 0xffff }; | ||
419 | #endif | ||
420 | 7290000 | int max = classes[0]; | |
421 | 7290000 | int prev = 0; | |
422 | 7290000 | const unsigned deadzone = s->quant_deadzone; | |
423 | 7290000 | const unsigned threshold = 2 * deadzone; | |
424 | |||
425 | 7290000 | bi->mb[0] = blk[0]; | |
426 | |||
427 |
2/2✓ Branch 0 taken 29160000 times.
✓ Branch 1 taken 7290000 times.
|
36450000 | for (area = 0; area < 4; area++) { |
428 | 29160000 | bi->prev[area] = prev; | |
429 | 29160000 | bi->bit_size[area] = 1; // 4 areas 4 bits for EOB :) | |
430 |
2/2✓ Branch 0 taken 459270000 times.
✓ Branch 1 taken 29160000 times.
|
488430000 | for (i = mb_area_start[area]; i < mb_area_start[area + 1]; i++) { |
431 | 459270000 | int level = blk[zigzag_scan[i]]; | |
432 | |||
433 |
2/2✓ Branch 0 taken 140563841 times.
✓ Branch 1 taken 318706159 times.
|
459270000 | if (level + deadzone > threshold) { |
434 | 140563841 | bi->sign[i] = (level >> 31) & 1; | |
435 | /* Weight it and shift down into range, adding for rounding. | ||
436 | * The extra division by a factor of 2^4 reverses the 8x | ||
437 | * expansion of the DCT AND the 2x doubling of the weights. */ | ||
438 | 140563841 | level = (FFABS(level) * weight[i] + (1 << (dv_weight_bits + 3))) >> | |
439 | 140563841 | (dv_weight_bits + 4); | |
440 |
2/2✓ Branch 0 taken 16556474 times.
✓ Branch 1 taken 124007367 times.
|
140563841 | if (!level) |
441 | 16556474 | continue; | |
442 | 124007367 | bi->mb[i] = level; | |
443 |
2/2✓ Branch 0 taken 10763592 times.
✓ Branch 1 taken 113243775 times.
|
124007367 | if (level > max) |
444 | 10763592 | max = level; | |
445 | 124007367 | bi->bit_size[area] += dv_rl2vlc_size(i - prev - 1, level); | |
446 | 124007367 | bi->next[prev] = i; | |
447 | 124007367 | prev = i; | |
448 | } | ||
449 | } | ||
450 | } | ||
451 | 7290000 | bi->next[prev] = i; | |
452 |
2/2✓ Branch 0 taken 12295335 times.
✓ Branch 1 taken 7290000 times.
|
19585335 | for (bi->cno = 0; max > classes[bi->cno]; bi->cno++) |
453 | ; | ||
454 | |||
455 | 7290000 | bi->cno += bias; | |
456 | |||
457 |
2/2✓ Branch 0 taken 2431643 times.
✓ Branch 1 taken 4858357 times.
|
7290000 | if (bi->cno >= 3) { |
458 | 2431643 | bi->cno = 3; | |
459 | 2431643 | prev = 0; | |
460 | 2431643 | i = bi->next[prev]; | |
461 |
2/2✓ Branch 0 taken 9726572 times.
✓ Branch 1 taken 2431643 times.
|
12158215 | for (area = 0; area < 4; area++) { |
462 | 9726572 | bi->prev[area] = prev; | |
463 | 9726572 | bi->bit_size[area] = 1; // 4 areas 4 bits for EOB :) | |
464 |
2/2✓ Branch 0 taken 42987151 times.
✓ Branch 1 taken 9726572 times.
|
52713723 | for (; i < mb_area_start[area + 1]; i = bi->next[i]) { |
465 | 42987151 | bi->mb[i] >>= 1; | |
466 | |||
467 |
2/2✓ Branch 0 taken 25005901 times.
✓ Branch 1 taken 17981250 times.
|
42987151 | if (bi->mb[i]) { |
468 | 25005901 | bi->bit_size[area] += dv_rl2vlc_size(i - prev - 1, bi->mb[i]); | |
469 | 25005901 | bi->next[prev] = i; | |
470 | 25005901 | prev = i; | |
471 | } | ||
472 | } | ||
473 | } | ||
474 | 2431643 | bi->next[prev] = i; | |
475 | } | ||
476 | |||
477 | 7290000 | return bi->bit_size[0] + bi->bit_size[1] + | |
478 | 7290000 | bi->bit_size[2] + bi->bit_size[3]; | |
479 | } | ||
480 | |||
481 | /* this function just copies the DCT coefficients and performs | ||
482 | the initial (non-)quantization. */ | ||
483 | 15255000 | static inline void dv_set_class_number_hd(DVEncContext *s, | |
484 | int16_t *blk, EncBlockInfo *bi, | ||
485 | const uint8_t *zigzag_scan, | ||
486 | const int *weight, int bias) | ||
487 | { | ||
488 | 15255000 | int i, max = 0; | |
489 | |||
490 | /* the first quantization (none at all) */ | ||
491 | 15255000 | bi->area_q[0] = 1; | |
492 | |||
493 | /* weigh AC components and store to save[] */ | ||
494 | /* (i=0 is the DC component; we only include it to make the | ||
495 | number of loop iterations even, for future possible SIMD optimization) */ | ||
496 |
2/2✓ Branch 0 taken 488160000 times.
✓ Branch 1 taken 15255000 times.
|
503415000 | for (i = 0; i < 64; i += 2) { |
497 | int level0, level1; | ||
498 | |||
499 | /* get the AC component (in zig-zag order) */ | ||
500 | 488160000 | level0 = blk[zigzag_scan[i+0]]; | |
501 | 488160000 | level1 = blk[zigzag_scan[i+1]]; | |
502 | |||
503 | /* extract sign and make it the lowest bit */ | ||
504 | 488160000 | bi->sign[i+0] = (level0>>31)&1; | |
505 | 488160000 | bi->sign[i+1] = (level1>>31)&1; | |
506 | |||
507 | /* take absolute value of the level */ | ||
508 | 488160000 | level0 = FFABS(level0); | |
509 | 488160000 | level1 = FFABS(level1); | |
510 | |||
511 | /* weigh it */ | ||
512 | 488160000 | level0 = (level0*weight[i+0] + 4096 + (1<<17)) >> 18; | |
513 | 488160000 | level1 = (level1*weight[i+1] + 4096 + (1<<17)) >> 18; | |
514 | |||
515 | /* save unquantized value */ | ||
516 | 488160000 | bi->save[i+0] = level0; | |
517 | 488160000 | bi->save[i+1] = level1; | |
518 | |||
519 | /* find max component */ | ||
520 |
2/2✓ Branch 0 taken 16055162 times.
✓ Branch 1 taken 472104838 times.
|
488160000 | if (bi->save[i+0] > max) |
521 | 16055162 | max = bi->save[i+0]; | |
522 |
2/2✓ Branch 0 taken 939862 times.
✓ Branch 1 taken 487220138 times.
|
488160000 | if (bi->save[i+1] > max) |
523 | 939862 | max = bi->save[i+1]; | |
524 | } | ||
525 | |||
526 | /* copy DC component */ | ||
527 | 15255000 | bi->mb[0] = blk[0]; | |
528 | |||
529 | /* the EOB code is 4 bits */ | ||
530 | 15255000 | bi->bit_size[0] = 4; | |
531 | 15255000 | bi->bit_size[1] = bi->bit_size[2] = bi->bit_size[3] = 0; | |
532 | |||
533 | /* ensure that no AC coefficients are cut off */ | ||
534 | 15255000 | bi->min_qlevel = ((max+256) >> 8); | |
535 | |||
536 | 15255000 | bi->area_q[0] = 25; /* set to an "impossible" value */ | |
537 | 15255000 | bi->cno = 0; | |
538 | 15255000 | } | |
539 | |||
540 | 22545000 | static av_always_inline int dv_init_enc_block(EncBlockInfo* bi, const uint8_t *data, int linesize, | |
541 | DVEncContext *s, int chroma) | ||
542 | { | ||
543 | 22545000 | LOCAL_ALIGNED_16(int16_t, blk, [64]); | |
544 | |||
545 | 22545000 | bi->area_q[0] = bi->area_q[1] = bi->area_q[2] = bi->area_q[3] = 0; | |
546 | 22545000 | bi->partial_bit_count = 0; | |
547 | 22545000 | bi->partial_bit_buffer = 0; | |
548 | 22545000 | bi->cur_ac = 0; | |
549 | |||
550 |
2/2✓ Branch 0 taken 21411000 times.
✓ Branch 1 taken 1134000 times.
|
22545000 | if (data) { |
551 |
2/2✓ Branch 0 taken 15255000 times.
✓ Branch 1 taken 6156000 times.
|
21411000 | if (DV_PROFILE_IS_HD(s->sys)) { |
552 | 15255000 | s->get_pixels(blk, data, linesize * (1 << bi->dct_mode)); | |
553 | 15255000 | s->fdct[0](blk); | |
554 | } else { | ||
555 | 6156000 | bi->dct_mode = dv_guess_dct_mode(s, data, linesize); | |
556 | 6156000 | s->get_pixels(blk, data, linesize); | |
557 | 6156000 | s->fdct[bi->dct_mode](blk); | |
558 | } | ||
559 | } else { | ||
560 | /* We rely on the fact that encoding all zeros leads to an immediate EOB, | ||
561 | which is precisely what the spec calls for in the "dummy" blocks. */ | ||
562 | 1134000 | memset(blk, 0, 64*sizeof(*blk)); | |
563 | 1134000 | bi->dct_mode = 0; | |
564 | } | ||
565 | |||
566 |
2/2✓ Branch 0 taken 15255000 times.
✓ Branch 1 taken 7290000 times.
|
22545000 | if (DV_PROFILE_IS_HD(s->sys)) { |
567 | const int *weights; | ||
568 |
2/2✓ Branch 0 taken 10935000 times.
✓ Branch 1 taken 4320000 times.
|
15255000 | if (s->sys->height == 1080) { |
569 | 10935000 | weights = dv_weight_1080[chroma]; | |
570 | } else { /* 720p */ | ||
571 | 4320000 | weights = dv_weight_720[chroma]; | |
572 | } | ||
573 | 15255000 | dv_set_class_number_hd(s, blk, bi, | |
574 | ff_zigzag_direct, | ||
575 | weights, | ||
576 | 15255000 | dv100_min_bias+chroma*dv100_chroma_bias); | |
577 | } else { | ||
578 | 14580000 | dv_set_class_number_sd(s, blk, bi, | |
579 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7290000 times.
|
7290000 | bi->dct_mode ? ff_dv_zigzag248_direct : ff_zigzag_direct, |
580 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7290000 times.
|
7290000 | bi->dct_mode ? dv_weight_248 : dv_weight_88, |
581 | chroma); | ||
582 | } | ||
583 | |||
584 | 22545000 | return bi->bit_size[0] + bi->bit_size[1] + bi->bit_size[2] + bi->bit_size[3]; | |
585 | } | ||
586 | |||
587 | /* DV100 quantize | ||
588 | Perform quantization by divinding the AC component by the qstep. | ||
589 | As an optimization we use a fixed-point integer multiply instead | ||
590 | of a divide. */ | ||
591 | 2070413856 | static av_always_inline int dv100_quantize(int level, int qsinv) | |
592 | { | ||
593 | /* this code is equivalent to */ | ||
594 | /* return (level + qs/2) / qs; */ | ||
595 | |||
596 | 2070413856 | return (level * qsinv + 1024 + (1<<(dv100_qstep_bits-1))) >> dv100_qstep_bits; | |
597 | |||
598 | /* the extra +1024 is needed to make the rounding come out right. */ | ||
599 | |||
600 | /* I (DJM) have verified that the results are exactly the same as | ||
601 | division for level 0-2048 at all QNOs. */ | ||
602 | } | ||
603 | |||
604 | 48118712 | static int dv100_actual_quantize(EncBlockInfo *b, int qlevel) | |
605 | { | ||
606 | int prev, k, qsinv; | ||
607 | |||
608 | 48118712 | int qno = DV100_QLEVEL_QNO(dv100_qlevels[qlevel]); | |
609 | 48118712 | int cno = DV100_QLEVEL_CNO(dv100_qlevels[qlevel]); | |
610 | |||
611 |
4/4✓ Branch 0 taken 19994488 times.
✓ Branch 1 taken 28124224 times.
✓ Branch 2 taken 15255000 times.
✓ Branch 3 taken 4739488 times.
|
48118712 | if (b->area_q[0] == qno && b->cno == cno) |
612 | 15255000 | return b->bit_size[0]; | |
613 | |||
614 | 32863712 | qsinv = dv100_qstep_inv[qno]; | |
615 | |||
616 | /* record the new qstep */ | ||
617 | 32863712 | b->area_q[0] = qno; | |
618 | 32863712 | b->cno = cno; | |
619 | |||
620 | /* reset encoded size (EOB = 4 bits) */ | ||
621 | 32863712 | b->bit_size[0] = 4; | |
622 | |||
623 | /* visit nonzero components and quantize */ | ||
624 | 32863712 | prev = 0; | |
625 |
2/2✓ Branch 0 taken 2070413856 times.
✓ Branch 1 taken 32863712 times.
|
2103277568 | for (k = 1; k < 64; k++) { |
626 | /* quantize */ | ||
627 | 2070413856 | int ac = dv100_quantize(b->save[k], qsinv) >> cno; | |
628 |
2/2✓ Branch 0 taken 464760442 times.
✓ Branch 1 taken 1605653414 times.
|
2070413856 | if (ac) { |
629 |
2/2✓ Branch 0 taken 1068 times.
✓ Branch 1 taken 464759374 times.
|
464760442 | if (ac > 255) |
630 | 1068 | ac = 255; | |
631 | 464760442 | b->mb[k] = ac; | |
632 | 464760442 | b->bit_size[0] += dv_rl2vlc_size(k - prev - 1, ac); | |
633 | 464760442 | b->next[prev] = k; | |
634 | 464760442 | prev = k; | |
635 | } | ||
636 | } | ||
637 | 32863712 | b->next[prev] = k; | |
638 | |||
639 | 32863712 | return b->bit_size[0]; | |
640 | } | ||
641 | |||
642 | 381375 | static inline void dv_guess_qnos_hd(EncBlockInfo *blks, int *qnos) | |
643 | { | ||
644 | EncBlockInfo *b; | ||
645 | int min_qlevel[5]; | ||
646 | int qlevels[5]; | ||
647 | int size[5]; | ||
648 | int i, j; | ||
649 | /* cache block sizes at hypothetical qlevels */ | ||
650 | 381375 | uint16_t size_cache[5*8][DV100_NUM_QLEVELS] = {{0}}; | |
651 | |||
652 | /* get minimum qlevels */ | ||
653 |
2/2✓ Branch 0 taken 1906875 times.
✓ Branch 1 taken 381375 times.
|
2288250 | for (i = 0; i < 5; i++) { |
654 | 1906875 | min_qlevel[i] = 1; | |
655 |
2/2✓ Branch 0 taken 15255000 times.
✓ Branch 1 taken 1906875 times.
|
17161875 | for (j = 0; j < 8; j++) { |
656 |
2/2✓ Branch 0 taken 2020785 times.
✓ Branch 1 taken 13234215 times.
|
15255000 | if (blks[8*i+j].min_qlevel > min_qlevel[i]) |
657 | 2020785 | min_qlevel[i] = blks[8*i+j].min_qlevel; | |
658 | } | ||
659 | } | ||
660 | |||
661 | /* initialize sizes */ | ||
662 |
2/2✓ Branch 0 taken 1906875 times.
✓ Branch 1 taken 381375 times.
|
2288250 | for (i = 0; i < 5; i++) { |
663 | 1906875 | qlevels[i] = dv100_starting_qno; | |
664 |
2/2✓ Branch 0 taken 1873179 times.
✓ Branch 1 taken 33696 times.
|
1906875 | if (qlevels[i] < min_qlevel[i]) |
665 | 1873179 | qlevels[i] = min_qlevel[i]; | |
666 | |||
667 | 1906875 | qnos[i] = DV100_QLEVEL_QNO(dv100_qlevels[qlevels[i]]); | |
668 | 1906875 | size[i] = 0; | |
669 |
2/2✓ Branch 0 taken 15255000 times.
✓ Branch 1 taken 1906875 times.
|
17161875 | for (j = 0; j < 8; j++) { |
670 | 15255000 | size_cache[8*i+j][qlevels[i]] = dv100_actual_quantize(&blks[8*i+j], qlevels[i]); | |
671 | 15255000 | size[i] += size_cache[8*i+j][qlevels[i]]; | |
672 | } | ||
673 | } | ||
674 | |||
675 | /* must we go coarser? */ | ||
676 |
2/2✓ Branch 0 taken 295029 times.
✓ Branch 1 taken 86346 times.
|
381375 | if (size[0]+size[1]+size[2]+size[3]+size[4] > vs_total_ac_bits_hd) { |
677 | 295029 | int largest = size[0] % 5; /* 'random' number */ | |
678 | 295029 | int qlevels_done = 0; | |
679 | |||
680 | do { | ||
681 | /* find the macroblock with the lowest qlevel */ | ||
682 |
2/2✓ Branch 0 taken 11005445 times.
✓ Branch 1 taken 2201089 times.
|
13206534 | for (i = 0; i < 5; i++) { |
683 |
2/2✓ Branch 0 taken 406032 times.
✓ Branch 1 taken 10599413 times.
|
11005445 | if (qlevels[i] < qlevels[largest]) |
684 | 406032 | largest = i; | |
685 | } | ||
686 | |||
687 | 2201089 | i = largest; | |
688 | /* ensure that we don't enter infinite loop */ | ||
689 | 2201089 | largest = (largest+1) % 5; | |
690 | |||
691 | /* quantize a little bit more */ | ||
692 | 2201089 | qlevels[i] += dv100_qlevel_inc; | |
693 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2201089 times.
|
2201089 | if (qlevels[i] > DV100_NUM_QLEVELS-1) { |
694 | ✗ | qlevels[i] = DV100_NUM_QLEVELS-1; | |
695 | ✗ | qlevels_done++; | |
696 | } | ||
697 | |||
698 | 2201089 | qnos[i] = DV100_QLEVEL_QNO(dv100_qlevels[qlevels[i]]); | |
699 | 2201089 | size[i] = 0; | |
700 | |||
701 | /* for each block */ | ||
702 | 2201089 | b = &blks[8*i]; | |
703 |
2/2✓ Branch 0 taken 17608712 times.
✓ Branch 1 taken 2201089 times.
|
19809801 | for (j = 0; j < 8; j++, b++) { |
704 | /* accumulate block size into macroblock */ | ||
705 |
1/2✓ Branch 0 taken 17608712 times.
✗ Branch 1 not taken.
|
17608712 | if(size_cache[8*i+j][qlevels[i]] == 0) { |
706 | /* it is safe to use actual_quantize() here because we only go from finer to coarser, | ||
707 | and it saves the final actual_quantize() down below */ | ||
708 | 17608712 | size_cache[8*i+j][qlevels[i]] = dv100_actual_quantize(b, qlevels[i]); | |
709 | } | ||
710 | 17608712 | size[i] += size_cache[8*i+j][qlevels[i]]; | |
711 | } /* for each block */ | ||
712 | |||
713 |
3/4✓ Branch 0 taken 1906060 times.
✓ Branch 1 taken 295029 times.
✓ Branch 2 taken 1906060 times.
✗ Branch 3 not taken.
|
2201089 | } while (vs_total_ac_bits_hd < size[0] + size[1] + size[2] + size[3] + size[4] && qlevels_done < 5); |
714 | |||
715 | // can we go finer? | ||
716 | 86346 | } else if (DV100_ENABLE_FINER && | |
717 |
2/2✓ Branch 0 taken 86340 times.
✓ Branch 1 taken 6 times.
|
86346 | size[0]+size[1]+size[2]+size[3]+size[4] < vs_total_ac_bits_hd) { |
718 | int save_qlevel; | ||
719 | 86340 | int largest = size[0] % 5; /* 'random' number */ | |
720 | |||
721 | 86340 | while (qlevels[0] > min_qlevel[0] || | |
722 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 86340 times.
|
86340 | qlevels[1] > min_qlevel[1] || |
723 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 86340 times.
|
86340 | qlevels[2] > min_qlevel[2] || |
724 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 86340 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 86340 times.
|
172680 | qlevels[3] > min_qlevel[3] || |
725 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 86340 times.
|
86340 | qlevels[4] > min_qlevel[4]) { |
726 | |||
727 | /* find the macroblock with the highest qlevel */ | ||
728 | ✗ | for (i = 0; i < 5; i++) { | |
729 | ✗ | if (qlevels[i] > min_qlevel[i] && qlevels[i] > qlevels[largest]) | |
730 | ✗ | largest = i; | |
731 | } | ||
732 | |||
733 | ✗ | i = largest; | |
734 | |||
735 | /* ensure that we don't enter infinite loop */ | ||
736 | ✗ | largest = (largest+1) % 5; | |
737 | |||
738 | ✗ | if (qlevels[i] <= min_qlevel[i]) { | |
739 | /* can't unquantize any more */ | ||
740 | ✗ | continue; | |
741 | } | ||
742 | /* quantize a little bit less */ | ||
743 | ✗ | save_qlevel = qlevels[i]; | |
744 | ✗ | qlevels[i] -= dv100_qlevel_inc; | |
745 | ✗ | if (qlevels[i] < min_qlevel[i]) | |
746 | ✗ | qlevels[i] = min_qlevel[i]; | |
747 | |||
748 | ✗ | qnos[i] = DV100_QLEVEL_QNO(dv100_qlevels[qlevels[i]]); | |
749 | |||
750 | ✗ | size[i] = 0; | |
751 | |||
752 | /* for each block */ | ||
753 | ✗ | b = &blks[8*i]; | |
754 | ✗ | for (j = 0; j < 8; j++, b++) { | |
755 | /* accumulate block size into macroblock */ | ||
756 | ✗ | if(size_cache[8*i+j][qlevels[i]] == 0) { | |
757 | ✗ | size_cache[8*i+j][qlevels[i]] = dv100_actual_quantize(b, qlevels[i]); | |
758 | } | ||
759 | ✗ | size[i] += size_cache[8*i+j][qlevels[i]]; | |
760 | } /* for each block */ | ||
761 | |||
762 | /* did we bust the limit? */ | ||
763 | ✗ | if (vs_total_ac_bits_hd < size[0] + size[1] + size[2] + size[3] + size[4]) { | |
764 | /* go back down and exit */ | ||
765 | ✗ | qlevels[i] = save_qlevel; | |
766 | ✗ | qnos[i] = DV100_QLEVEL_QNO(dv100_qlevels[qlevels[i]]); | |
767 | ✗ | break; | |
768 | } | ||
769 | } | ||
770 | } | ||
771 | |||
772 | /* now do the actual quantization */ | ||
773 |
2/2✓ Branch 0 taken 1906875 times.
✓ Branch 1 taken 381375 times.
|
2288250 | for (i = 0; i < 5; i++) { |
774 | /* for each block */ | ||
775 | 1906875 | b = &blks[8*i]; | |
776 | 1906875 | size[i] = 0; | |
777 |
2/2✓ Branch 0 taken 15255000 times.
✓ Branch 1 taken 1906875 times.
|
17161875 | for (j = 0; j < 8; j++, b++) { |
778 | /* accumulate block size into macroblock */ | ||
779 | 15255000 | size[i] += dv100_actual_quantize(b, qlevels[i]); | |
780 | } /* for each block */ | ||
781 | } | ||
782 | 381375 | } | |
783 | |||
784 | 134207 | static inline void dv_guess_qnos(EncBlockInfo *blks, int *qnos) | |
785 | { | ||
786 | int size[5]; | ||
787 | int i, j, k, a, prev, a2; | ||
788 | EncBlockInfo *b; | ||
789 | |||
790 | 134207 | size[0] = | |
791 | 134207 | size[1] = | |
792 | 134207 | size[2] = | |
793 | 134207 | size[3] = | |
794 | 134207 | size[4] = 1 << 24; | |
795 | do { | ||
796 | 664637 | b = blks; | |
797 |
2/2✓ Branch 0 taken 3058425 times.
✓ Branch 1 taken 530430 times.
|
3588855 | for (i = 0; i < 5; i++) { |
798 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3058425 times.
|
3058425 | if (!qnos[i]) |
799 | ✗ | continue; | |
800 | |||
801 | 3058425 | qnos[i]--; | |
802 | 3058425 | size[i] = 0; | |
803 |
2/2✓ Branch 0 taken 18350550 times.
✓ Branch 1 taken 3058425 times.
|
21408975 | for (j = 0; j < 6; j++, b++) { |
804 |
2/2✓ Branch 0 taken 73402200 times.
✓ Branch 1 taken 18350550 times.
|
91752750 | for (a = 0; a < 4; a++) { |
805 |
2/2✓ Branch 0 taken 14355952 times.
✓ Branch 1 taken 59046248 times.
|
73402200 | if (b->area_q[a] != ff_dv_quant_shifts[qnos[i] + ff_dv_quant_offset[b->cno]][a]) { |
806 | 14355952 | b->bit_size[a] = 1; // 4 areas 4 bits for EOB :) | |
807 | 14355952 | b->area_q[a]++; | |
808 | 14355952 | prev = b->prev[a]; | |
809 | av_assert2(b->next[prev] >= mb_area_start[a + 1] || b->mb[prev]); | ||
810 |
2/2✓ Branch 0 taken 61234306 times.
✓ Branch 1 taken 14355952 times.
|
75590258 | for (k = b->next[prev]; k < mb_area_start[a + 1]; k = b->next[k]) { |
811 | 61234306 | b->mb[k] >>= 1; | |
812 |
2/2✓ Branch 0 taken 37588012 times.
✓ Branch 1 taken 23646294 times.
|
61234306 | if (b->mb[k]) { |
813 | 37588012 | b->bit_size[a] += dv_rl2vlc_size(k - prev - 1, b->mb[k]); | |
814 | 37588012 | prev = k; | |
815 | } else { | ||
816 |
4/4✓ Branch 0 taken 5339066 times.
✓ Branch 1 taken 18307228 times.
✓ Branch 2 taken 1130005 times.
✓ Branch 3 taken 4209061 times.
|
23646294 | if (b->next[k] >= mb_area_start[a + 1] && b->next[k] < 64) { |
817 |
2/2✓ Branch 0 taken 74 times.
✓ Branch 1 taken 1130005 times.
|
1130079 | for (a2 = a + 1; b->next[k] >= mb_area_start[a2 + 1]; a2++) |
818 | 74 | b->prev[a2] = prev; | |
819 | av_assert2(a2 < 4); | ||
820 | av_assert2(b->mb[b->next[k]]); | ||
821 | 1130005 | b->bit_size[a2] += dv_rl2vlc_size(b->next[k] - prev - 1, b->mb[b->next[k]]) - | |
822 | 1130005 | dv_rl2vlc_size(b->next[k] - k - 1, b->mb[b->next[k]]); | |
823 | av_assert2(b->prev[a2] == k && (a2 + 1 >= 4 || b->prev[a2 + 1] != k)); | ||
824 | 1130005 | b->prev[a2] = prev; | |
825 | } | ||
826 | 23646294 | b->next[prev] = b->next[k]; | |
827 | } | ||
828 | } | ||
829 | 14355952 | b->prev[a + 1] = prev; | |
830 | } | ||
831 | 73402200 | size[i] += b->bit_size[a]; | |
832 | } | ||
833 | } | ||
834 |
2/2✓ Branch 0 taken 134207 times.
✓ Branch 1 taken 2924218 times.
|
3058425 | if (vs_total_ac_bits >= size[0] + size[1] + size[2] + size[3] + size[4]) |
835 | 134207 | return; | |
836 | } | ||
837 |
1/2✓ Branch 0 taken 530430 times.
✗ Branch 1 not taken.
|
530430 | } while (qnos[0] | qnos[1] | qnos[2] | qnos[3] | qnos[4]); |
838 | |||
839 | ✗ | for (a = 2; a == 2 || vs_total_ac_bits < size[0]; a += a) { | |
840 | ✗ | b = blks; | |
841 | ✗ | size[0] = 5 * 6 * 4; // EOB | |
842 | ✗ | for (j = 0; j < 6 * 5; j++, b++) { | |
843 | ✗ | prev = b->prev[0]; | |
844 | ✗ | for (k = b->next[prev]; k < 64; k = b->next[k]) { | |
845 | ✗ | if (b->mb[k] < a && b->mb[k] > -a) { | |
846 | ✗ | b->next[prev] = b->next[k]; | |
847 | } else { | ||
848 | ✗ | size[0] += dv_rl2vlc_size(k - prev - 1, b->mb[k]); | |
849 | ✗ | prev = k; | |
850 | } | ||
851 | } | ||
852 | } | ||
853 | } | ||
854 | } | ||
855 | |||
856 | /* update all cno values into the blocks, over-writing the old values without | ||
857 | touching anything else. (only used for DV100) */ | ||
858 | 381375 | static inline void dv_revise_cnos(uint8_t *dif, EncBlockInfo *blk, const AVDVProfile *profile) | |
859 | { | ||
860 | uint8_t *data; | ||
861 | int mb_index, i; | ||
862 | |||
863 |
2/2✓ Branch 0 taken 1906875 times.
✓ Branch 1 taken 381375 times.
|
2288250 | for (mb_index = 0; mb_index < 5; mb_index++) { |
864 | 1906875 | data = dif + mb_index*80 + 4; | |
865 |
2/2✓ Branch 0 taken 15255000 times.
✓ Branch 1 taken 1906875 times.
|
17161875 | for (i = 0; i < profile->bpm; i++) { |
866 | /* zero out the class number */ | ||
867 | 15255000 | data[1] &= 0xCF; | |
868 | /* add the new one */ | ||
869 | 15255000 | data[1] |= blk[profile->bpm*mb_index+i].cno << 4; | |
870 | |||
871 | 15255000 | data += profile->block_sizes[i] >> 3; | |
872 | } | ||
873 | } | ||
874 | 381375 | } | |
875 | |||
876 | 624375 | static int dv_encode_video_segment(AVCodecContext *avctx, void *arg) | |
877 | { | ||
878 | 624375 | DVEncContext *s = avctx->priv_data; | |
879 | 624375 | DVwork_chunk *work_chunk = arg; | |
880 | int mb_index, i, j; | ||
881 | int mb_x, mb_y, c_offset; | ||
882 | ptrdiff_t linesize, y_stride; | ||
883 | const uint8_t *y_ptr; | ||
884 | uint8_t *dif, *p; | ||
885 | 624375 | LOCAL_ALIGNED_8(uint8_t, scratch, [128]); | |
886 | EncBlockInfo enc_blks[5 * DV_MAX_BPM]; | ||
887 | PutBitContext pbs[5 * DV_MAX_BPM]; | ||
888 | PutBitContext *pb; | ||
889 | EncBlockInfo *enc_blk; | ||
890 | 624375 | int vs_bit_size = 0; | |
891 | int qnos[5]; | ||
892 | 624375 | int *qnosp = &qnos[0]; | |
893 | |||
894 | 624375 | p = dif = &s->buf[work_chunk->buf_offset * 80]; | |
895 | 624375 | enc_blk = &enc_blks[0]; | |
896 |
2/2✓ Branch 0 taken 3121875 times.
✓ Branch 1 taken 624375 times.
|
3746250 | for (mb_index = 0; mb_index < 5; mb_index++) { |
897 | 3121875 | dv_calculate_mb_xy(s->sys, s->buf, work_chunk, mb_index, &mb_x, &mb_y); | |
898 | |||
899 |
2/2✓ Branch 0 taken 1906875 times.
✓ Branch 1 taken 1215000 times.
|
3121875 | qnos[mb_index] = DV_PROFILE_IS_HD(s->sys) ? 1 : 15; |
900 | |||
901 | 3121875 | y_ptr = s->frame->data[0] + (mb_y * s->frame->linesize[0] + mb_x) * 8; | |
902 | 3121875 | linesize = s->frame->linesize[0]; | |
903 | |||
904 |
4/4✓ Branch 0 taken 1366875 times.
✓ Branch 1 taken 1755000 times.
✓ Branch 2 taken 1356750 times.
✓ Branch 3 taken 10125 times.
|
3121875 | if (s->sys->height == 1080 && mb_y < 134) |
905 | 1356750 | enc_blk->dct_mode = dv_guess_dct_mode(s, y_ptr, linesize); | |
906 | else | ||
907 | 1765125 | enc_blk->dct_mode = 0; | |
908 |
2/2✓ Branch 0 taken 21853125 times.
✓ Branch 1 taken 3121875 times.
|
24975000 | for (i = 1; i < 8; i++) |
909 | 21853125 | enc_blk[i].dct_mode = enc_blk->dct_mode; | |
910 | |||
911 | /* initializing luminance blocks */ | ||
912 |
2/2✓ Branch 0 taken 2757375 times.
✓ Branch 1 taken 364500 times.
|
3121875 | if ((s->sys->pix_fmt == AV_PIX_FMT_YUV420P) || |
913 |
4/4✓ Branch 0 taken 283500 times.
✓ Branch 1 taken 2473875 times.
✓ Branch 2 taken 277200 times.
✓ Branch 3 taken 6300 times.
|
2757375 | (s->sys->pix_fmt == AV_PIX_FMT_YUV411P && mb_x >= (704 / 8)) || |
914 |
4/4✓ Branch 0 taken 1906875 times.
✓ Branch 1 taken 844200 times.
✓ Branch 2 taken 1896750 times.
✓ Branch 3 taken 10125 times.
|
2751075 | (s->sys->height >= 720 && mb_y != 134)) { |
915 |
1/2✓ Branch 0 taken 2267550 times.
✗ Branch 1 not taken.
|
2267550 | y_stride = s->frame->linesize[0] * (1 << (3*!enc_blk->dct_mode)); |
916 | } else { | ||
917 | 854325 | y_stride = 16; | |
918 | } | ||
919 | 3121875 | y_ptr = s->frame->data[0] + | |
920 | 3121875 | (mb_y * s->frame->linesize[0] + mb_x) * 8; | |
921 | 3121875 | linesize = s->frame->linesize[0]; | |
922 | |||
923 |
2/2✓ Branch 0 taken 567000 times.
✓ Branch 1 taken 2554875 times.
|
3121875 | if (s->sys->video_stype == 4) { /* SD 422 */ |
924 | 567000 | vs_bit_size += | |
925 | 567000 | dv_init_enc_block(enc_blk + 0, y_ptr, linesize, s, 0) + | |
926 | 567000 | dv_init_enc_block(enc_blk + 1, NULL, linesize, s, 0) + | |
927 | 1134000 | dv_init_enc_block(enc_blk + 2, y_ptr + 8, linesize, s, 0) + | |
928 | 567000 | dv_init_enc_block(enc_blk + 3, NULL, linesize, s, 0); | |
929 | } else { | ||
930 | 2554875 | vs_bit_size += | |
931 | 2554875 | dv_init_enc_block(enc_blk + 0, y_ptr, linesize, s, 0) + | |
932 | 2554875 | dv_init_enc_block(enc_blk + 1, y_ptr + 8, linesize, s, 0) + | |
933 | 5109750 | dv_init_enc_block(enc_blk + 2, y_ptr + y_stride, linesize, s, 0) + | |
934 | 2554875 | dv_init_enc_block(enc_blk + 3, y_ptr + 8 + y_stride, linesize, s, 0); | |
935 | } | ||
936 | 3121875 | enc_blk += 4; | |
937 | |||
938 | /* initializing chrominance blocks */ | ||
939 | 6243750 | c_offset = ((mb_y >> (s->sys->pix_fmt == AV_PIX_FMT_YUV420P)) * s->frame->linesize[1] + | |
940 |
2/2✓ Branch 0 taken 283500 times.
✓ Branch 1 taken 2838375 times.
|
3121875 | (mb_x >> ((s->sys->pix_fmt == AV_PIX_FMT_YUV411P) ? 2 : 1))) * 8; |
941 |
2/2✓ Branch 0 taken 6243750 times.
✓ Branch 1 taken 3121875 times.
|
9365625 | for (j = 2; j; j--) { |
942 | 6243750 | const uint8_t *c_ptr = s->frame->data[j] + c_offset; | |
943 | 6243750 | linesize = s->frame->linesize[j]; | |
944 |
3/4✓ Branch 0 taken 6223500 times.
✓ Branch 1 taken 20250 times.
✓ Branch 2 taken 6223500 times.
✗ Branch 3 not taken.
|
6243750 | y_stride = (mb_y == 134) ? 8 : (s->frame->linesize[j] * (1 << (3*!enc_blk->dct_mode))); |
945 |
4/4✓ Branch 0 taken 567000 times.
✓ Branch 1 taken 5676750 times.
✓ Branch 2 taken 12600 times.
✓ Branch 3 taken 554400 times.
|
6243750 | if (s->sys->pix_fmt == AV_PIX_FMT_YUV411P && mb_x >= (704 / 8)) { |
946 | 12600 | uint8_t *b = scratch; | |
947 |
2/2✓ Branch 0 taken 100800 times.
✓ Branch 1 taken 12600 times.
|
113400 | for (i = 0; i < 8; i++) { |
948 | 100800 | const uint8_t *d = c_ptr + linesize * 8; | |
949 | 100800 | b[0] = c_ptr[0]; | |
950 | 100800 | b[1] = c_ptr[1]; | |
951 | 100800 | b[2] = c_ptr[2]; | |
952 | 100800 | b[3] = c_ptr[3]; | |
953 | 100800 | b[4] = d[0]; | |
954 | 100800 | b[5] = d[1]; | |
955 | 100800 | b[6] = d[2]; | |
956 | 100800 | b[7] = d[3]; | |
957 | 100800 | c_ptr += linesize; | |
958 | 100800 | b += 16; | |
959 | } | ||
960 | 12600 | c_ptr = scratch; | |
961 | 12600 | linesize = 16; | |
962 | } | ||
963 | |||
964 | 6243750 | vs_bit_size += dv_init_enc_block(enc_blk++, c_ptr, linesize, s, 1); | |
965 |
2/2✓ Branch 0 taken 3813750 times.
✓ Branch 1 taken 2430000 times.
|
6243750 | if (s->sys->bpm == 8) |
966 | 3813750 | vs_bit_size += dv_init_enc_block(enc_blk++, c_ptr + y_stride, | |
967 | linesize, s, 1); | ||
968 | } | ||
969 | } | ||
970 | |||
971 |
2/2✓ Branch 0 taken 381375 times.
✓ Branch 1 taken 243000 times.
|
624375 | if (DV_PROFILE_IS_HD(s->sys)) { |
972 | /* unconditional */ | ||
973 | 381375 | dv_guess_qnos_hd(&enc_blks[0], qnosp); | |
974 |
2/2✓ Branch 0 taken 134207 times.
✓ Branch 1 taken 108793 times.
|
243000 | } else if (vs_total_ac_bits < vs_bit_size) { |
975 | 134207 | dv_guess_qnos(&enc_blks[0], qnosp); | |
976 | } | ||
977 | |||
978 | /* DIF encoding process */ | ||
979 |
2/2✓ Branch 0 taken 3121875 times.
✓ Branch 1 taken 624375 times.
|
3746250 | for (j = 0; j < 5 * s->sys->bpm;) { |
980 | 3121875 | int start_mb = j; | |
981 | |||
982 | 3121875 | p[3] = *qnosp++; | |
983 | 3121875 | p += 4; | |
984 | |||
985 | /* First pass over individual cells only */ | ||
986 |
2/2✓ Branch 0 taken 22545000 times.
✓ Branch 1 taken 3121875 times.
|
25666875 | for (i = 0; i < s->sys->bpm; i++, j++) { |
987 | 22545000 | int sz = s->sys->block_sizes[i] >> 3; | |
988 | |||
989 | 22545000 | init_put_bits(&pbs[j], p, sz); | |
990 | 22545000 | put_sbits(&pbs[j], 9, ((enc_blks[j].mb[0] >> 3) - 1024 + 2) >> 2); | |
991 |
4/4✓ Branch 0 taken 15255000 times.
✓ Branch 1 taken 7290000 times.
✓ Branch 2 taken 1906875 times.
✓ Branch 3 taken 13348125 times.
|
22545000 | put_bits(&pbs[j], 1, DV_PROFILE_IS_HD(s->sys) && i ? 1 : enc_blks[j].dct_mode); |
992 | 22545000 | put_bits(&pbs[j], 2, enc_blks[j].cno); | |
993 | |||
994 | 22545000 | dv_encode_ac(&enc_blks[j], &pbs[j], &pbs[j + 1]); | |
995 | 22545000 | p += sz; | |
996 | } | ||
997 | |||
998 | /* Second pass over each MB space */ | ||
999 | 3121875 | pb = &pbs[start_mb]; | |
1000 |
2/2✓ Branch 0 taken 22545000 times.
✓ Branch 1 taken 3121875 times.
|
25666875 | for (i = 0; i < s->sys->bpm; i++) |
1001 |
2/2✓ Branch 0 taken 7636928 times.
✓ Branch 1 taken 14908072 times.
|
22545000 | if (enc_blks[start_mb + i].partial_bit_count) |
1002 | 7636928 | pb = dv_encode_ac(&enc_blks[start_mb + i], pb, | |
1003 | 7636928 | &pbs[start_mb + s->sys->bpm]); | |
1004 | } | ||
1005 | |||
1006 | /* Third and final pass over the whole video segment space */ | ||
1007 | 624375 | pb = &pbs[0]; | |
1008 |
2/2✓ Branch 0 taken 22545000 times.
✓ Branch 1 taken 624375 times.
|
23169375 | for (j = 0; j < 5 * s->sys->bpm; j++) { |
1009 |
2/2✓ Branch 0 taken 4119283 times.
✓ Branch 1 taken 18425717 times.
|
22545000 | if (enc_blks[j].partial_bit_count) |
1010 | 4119283 | pb = dv_encode_ac(&enc_blks[j], pb, &pbs[s->sys->bpm * 5]); | |
1011 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22545000 times.
|
22545000 | if (enc_blks[j].partial_bit_count) |
1012 | ✗ | av_log(avctx, AV_LOG_ERROR, "ac bitstream overflow\n"); | |
1013 | } | ||
1014 | |||
1015 |
2/2✓ Branch 0 taken 22545000 times.
✓ Branch 1 taken 624375 times.
|
23169375 | for (j = 0; j < 5 * s->sys->bpm; j++) { |
1016 | 22545000 | flush_put_bits(&pbs[j]); | |
1017 | 22545000 | memset(put_bits_ptr(&pbs[j]), 0xff, put_bytes_left(&pbs[j], 0)); | |
1018 | } | ||
1019 | |||
1020 |
2/2✓ Branch 0 taken 381375 times.
✓ Branch 1 taken 243000 times.
|
624375 | if (DV_PROFILE_IS_HD(s->sys)) |
1021 | 381375 | dv_revise_cnos(dif, enc_blks, s->sys); | |
1022 | |||
1023 | 624375 | return 0; | |
1024 | } | ||
1025 | |||
1026 | 319800 | static inline int dv_write_pack(enum DVPackType pack_id, DVEncContext *c, | |
1027 | uint8_t *buf) | ||
1028 | { | ||
1029 | /* | ||
1030 | * Here's what SMPTE314M says about these two: | ||
1031 | * (page 6) APTn, AP1n, AP2n, AP3n: These data shall be identical | ||
1032 | * as track application IDs (APTn = 001, AP1n = | ||
1033 | * 001, AP2n = 001, AP3n = 001), if the source signal | ||
1034 | * comes from a digital VCR. If the signal source is | ||
1035 | * unknown, all bits for these data shall be set to 1. | ||
1036 | * (page 12) STYPE: STYPE defines a signal type of video signal | ||
1037 | * 00000b = 4:1:1 compression | ||
1038 | * 00100b = 4:2:2 compression | ||
1039 | * XXXXXX = Reserved | ||
1040 | * Now, I've got two problems with these statements: | ||
1041 | * 1. it looks like APT == 111b should be a safe bet, but it isn't. | ||
1042 | * It seems that for PAL as defined in IEC 61834 we have to set | ||
1043 | * APT to 000 and for SMPTE314M to 001. | ||
1044 | * 2. It is not at all clear what STYPE is used for 4:2:0 PAL | ||
1045 | * compression scheme (if any). | ||
1046 | */ | ||
1047 | 319800 | uint8_t aspect = 0; | |
1048 | 319800 | int apt = (c->sys->pix_fmt == AV_PIX_FMT_YUV420P ? 0 : 1); | |
1049 | int fs; | ||
1050 | |||
1051 |
2/2✓ Branch 0 taken 202800 times.
✓ Branch 1 taken 117000 times.
|
319800 | if (c->avctx->height >= 720) |
1052 |
3/4✓ Branch 0 taken 140400 times.
✓ Branch 1 taken 62400 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 140400 times.
|
202800 | fs = c->avctx->height == 720 || (c->frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST) ? 0x40 : 0x00; |
1053 | else | ||
1054 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 117000 times.
|
117000 | fs = (c->frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST) ? 0x00 : 0x40; |
1055 | |||
1056 |
2/2✓ Branch 0 taken 117000 times.
✓ Branch 1 taken 202800 times.
|
319800 | if (DV_PROFILE_IS_HD(c->sys) || |
1057 | 117000 | (int)(av_q2d(c->avctx->sample_aspect_ratio) * | |
1058 |
2/2✓ Branch 0 taken 7800 times.
✓ Branch 1 taken 109200 times.
|
117000 | c->avctx->width / c->avctx->height * 10) >= 17) |
1059 | /* HD formats are always 16:9 */ | ||
1060 | 210600 | aspect = 0x02; | |
1061 | |||
1062 | 319800 | buf[0] = (uint8_t) pack_id; | |
1063 |
3/4✓ Branch 0 taken 24600 times.
✓ Branch 1 taken 147600 times.
✓ Branch 2 taken 147600 times.
✗ Branch 3 not taken.
|
319800 | switch (pack_id) { |
1064 | 24600 | case DV_HEADER525: /* I can't imagine why these two weren't defined as real */ | |
1065 | case DV_HEADER625: /* packs in SMPTE314M -- they definitely look like ones */ | ||
1066 | 24600 | buf[1] = 0xf8 | /* reserved -- always 1 */ | |
1067 | (apt & 0x07); /* APT: Track application ID */ | ||
1068 | 24600 | buf[2] = (0 << 7) | /* TF1: audio data is 0 - valid; 1 - invalid */ | |
1069 | 24600 | (0x0f << 3) | /* reserved -- always 1 */ | |
1070 | 24600 | (apt & 0x07); /* AP1: Audio application ID */ | |
1071 | 24600 | buf[3] = (0 << 7) | /* TF2: video data is 0 - valid; 1 - invalid */ | |
1072 | 24600 | (0x0f << 3) | /* reserved -- always 1 */ | |
1073 | 24600 | (apt & 0x07); /* AP2: Video application ID */ | |
1074 | 24600 | buf[4] = (0 << 7) | /* TF3: subcode(SSYB) is 0 - valid; 1 - invalid */ | |
1075 | 24600 | (0x0f << 3) | /* reserved -- always 1 */ | |
1076 | 24600 | (apt & 0x07); /* AP3: Subcode application ID */ | |
1077 | 24600 | break; | |
1078 | 147600 | case DV_VIDEO_SOURCE: | |
1079 | 147600 | buf[1] = 0xff; /* reserved -- always 1 */ | |
1080 | 147600 | buf[2] = (1 << 7) | /* B/W: 0 - b/w, 1 - color */ | |
1081 | (1 << 6) | /* following CLF is valid - 0, invalid - 1 */ | ||
1082 | (3 << 4) | /* CLF: color frames ID (see ITU-R BT.470-4) */ | ||
1083 | 0xf; /* reserved -- always 1 */ | ||
1084 | 147600 | buf[3] = (3 << 6) | /* reserved -- always 1 */ | |
1085 | 147600 | (c->sys->dsf << 5) | /* system: 60fields/50fields */ | |
1086 | 147600 | c->sys->video_stype; /* signal type video compression */ | |
1087 | 147600 | buf[4] = 0xff; /* VISC: 0xff -- no information */ | |
1088 | 147600 | break; | |
1089 | 147600 | case DV_VIDEO_CONTROL: | |
1090 | 147600 | buf[1] = (0 << 6) | /* Copy generation management (CGMS) 0 -- free */ | |
1091 | 0x3f; /* reserved -- always 1 */ | ||
1092 | 147600 | buf[2] = 0xc8 | /* reserved -- always b11001xxx */ | |
1093 | aspect; | ||
1094 | 147600 | buf[3] = (1 << 7) | /* frame/field flag 1 -- frame, 0 -- field */ | |
1095 | fs | /* first/second field flag 0 -- field 2, 1 -- field 1 */ | ||
1096 | (1 << 5) | /* frame change flag 0 -- same picture as before, 1 -- different */ | ||
1097 | 147600 | (1 << 4) | /* 1 - interlaced, 0 - noninterlaced */ | |
1098 | 0xc; /* reserved -- always b1100 */ | ||
1099 | 147600 | buf[4] = 0xff; /* reserved -- always 1 */ | |
1100 | 147600 | break; | |
1101 | ✗ | default: | |
1102 | ✗ | buf[1] = | |
1103 | ✗ | buf[2] = | |
1104 | ✗ | buf[3] = | |
1105 | ✗ | buf[4] = 0xff; | |
1106 | } | ||
1107 | 319800 | return 5; | |
1108 | } | ||
1109 | |||
1110 | 3690000 | static inline int dv_write_dif_id(enum DVSectionType t, uint8_t chan_num, | |
1111 | uint8_t seq_num, uint8_t dif_num, | ||
1112 | uint8_t *buf) | ||
1113 | { | ||
1114 | 3690000 | int fsc = chan_num & 1; | |
1115 | 3690000 | int fsp = 1 - (chan_num >> 1); | |
1116 | |||
1117 | 3690000 | buf[0] = (uint8_t) t; /* Section type */ | |
1118 | 3690000 | buf[1] = (seq_num << 4) | /* DIF seq number 0-9 for 525/60; 0-11 for 625/50 */ | |
1119 | 3690000 | (fsc << 3) | /* FSC: for 50 and 100Mb/s 0 - first channel; 1 - second */ | |
1120 | 3690000 | (fsp << 2) | /* FSP: for 100Mb/s 1 - channels 0-1; 0 - channels 2-3 */ | |
1121 | 3; /* reserved -- always 1 */ | ||
1122 | 3690000 | buf[2] = dif_num; /* DIF block number Video: 0-134, Audio: 0-8 */ | |
1123 | 3690000 | return 3; | |
1124 | } | ||
1125 | |||
1126 | 295200 | static inline int dv_write_ssyb_id(uint8_t syb_num, uint8_t fr, uint8_t *buf) | |
1127 | { | ||
1128 |
3/4✓ Branch 0 taken 246000 times.
✓ Branch 1 taken 49200 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 246000 times.
|
295200 | if (syb_num == 0 || syb_num == 6) { |
1129 | 49200 | buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */ | |
1130 | 49200 | (0 << 4) | /* AP3 (Subcode application ID) */ | |
1131 | 0x0f; /* reserved -- always 1 */ | ||
1132 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 246000 times.
|
246000 | } else if (syb_num == 11) { |
1133 | ✗ | buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */ | |
1134 | 0x7f; /* reserved -- always 1 */ | ||
1135 | } else { | ||
1136 | 246000 | buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */ | |
1137 | 246000 | (0 << 4) | /* APT (Track application ID) */ | |
1138 | 0x0f; /* reserved -- always 1 */ | ||
1139 | } | ||
1140 | 295200 | buf[1] = 0xf0 | /* reserved -- always 1 */ | |
1141 | (syb_num & 0x0f); /* SSYB number 0 - 11 */ | ||
1142 | 295200 | buf[2] = 0xff; /* reserved -- always 1 */ | |
1143 | 295200 | return 3; | |
1144 | } | ||
1145 | |||
1146 | 1005 | static void dv_format_frame(DVEncContext *c, uint8_t *buf) | |
1147 | { | ||
1148 | int chan, i, j, k; | ||
1149 | /* We work with 720p frames split in half. The odd half-frame is chan 2,3 */ | ||
1150 |
4/4✓ Branch 0 taken 200 times.
✓ Branch 1 taken 805 times.
✓ Branch 2 taken 100 times.
✓ Branch 3 taken 100 times.
|
1005 | int chan_offset = 2*(c->sys->height == 720 && c->avctx->frame_num & 1); |
1151 | |||
1152 |
2/2✓ Branch 0 taken 2055 times.
✓ Branch 1 taken 1005 times.
|
3060 | for (chan = 0; chan < c->sys->n_difchan; chan++) { |
1153 |
2/2✓ Branch 0 taken 24600 times.
✓ Branch 1 taken 2055 times.
|
26655 | for (i = 0; i < c->sys->difseg_size; i++) { |
1154 | 24600 | memset(buf, 0xff, 80 * 6); /* first 6 DIF blocks are for control data */ | |
1155 | |||
1156 | /* DV header: 1DIF */ | ||
1157 | 24600 | buf += dv_write_dif_id(DV_SECT_HEADER, chan+chan_offset, i, 0, buf); | |
1158 |
2/2✓ Branch 0 taken 24300 times.
✓ Branch 1 taken 300 times.
|
24600 | buf += dv_write_pack((c->sys->dsf ? DV_HEADER625 : DV_HEADER525), |
1159 | c, buf); | ||
1160 | 24600 | buf += 72; /* unused bytes */ | |
1161 | |||
1162 | /* DV subcode: 2DIFs */ | ||
1163 |
2/2✓ Branch 0 taken 49200 times.
✓ Branch 1 taken 24600 times.
|
73800 | for (j = 0; j < 2; j++) { |
1164 | 49200 | buf += dv_write_dif_id(DV_SECT_SUBCODE, chan+chan_offset, i, j, buf); | |
1165 |
2/2✓ Branch 0 taken 295200 times.
✓ Branch 1 taken 49200 times.
|
344400 | for (k = 0; k < 6; k++) |
1166 | 295200 | buf += dv_write_ssyb_id(k, (i < c->sys->difseg_size / 2), buf) + 5; | |
1167 | 49200 | buf += 29; /* unused bytes */ | |
1168 | } | ||
1169 | |||
1170 | /* DV VAUX: 3DIFS */ | ||
1171 |
2/2✓ Branch 0 taken 73800 times.
✓ Branch 1 taken 24600 times.
|
98400 | for (j = 0; j < 3; j++) { |
1172 | 73800 | buf += dv_write_dif_id(DV_SECT_VAUX, chan+chan_offset, i, j, buf); | |
1173 | 73800 | buf += dv_write_pack(DV_VIDEO_SOURCE, c, buf); | |
1174 | 73800 | buf += dv_write_pack(DV_VIDEO_CONTROL, c, buf); | |
1175 | 73800 | buf += 7 * 5; | |
1176 | 73800 | buf += dv_write_pack(DV_VIDEO_SOURCE, c, buf); | |
1177 | 73800 | buf += dv_write_pack(DV_VIDEO_CONTROL, c, buf); | |
1178 | 73800 | buf += 4 * 5 + 2; /* unused bytes */ | |
1179 | } | ||
1180 | |||
1181 | /* DV Audio/Video: 135 Video DIFs + 9 Audio DIFs */ | ||
1182 |
2/2✓ Branch 0 taken 3321000 times.
✓ Branch 1 taken 24600 times.
|
3345600 | for (j = 0; j < 135; j++) { |
1183 |
2/2✓ Branch 0 taken 221400 times.
✓ Branch 1 taken 3099600 times.
|
3321000 | if (j % 15 == 0) { |
1184 | 221400 | memset(buf, 0xff, 80); | |
1185 | 221400 | buf += dv_write_dif_id(DV_SECT_AUDIO, chan+chan_offset, i, j/15, buf); | |
1186 | 221400 | buf += 77; /* audio control & shuffled PCM audio */ | |
1187 | } | ||
1188 | 3321000 | buf += dv_write_dif_id(DV_SECT_VIDEO, chan+chan_offset, i, j, buf); | |
1189 | 3321000 | buf += 77; /* 1 video macroblock: 1 bytes control | |
1190 | * 4 * 14 bytes Y 8x8 data | ||
1191 | * 10 bytes Cr 8x8 data | ||
1192 | * 10 bytes Cb 8x8 data */ | ||
1193 | } | ||
1194 | } | ||
1195 | } | ||
1196 | 1005 | } | |
1197 | |||
1198 | 1005 | static int dvvideo_encode_frame(AVCodecContext *c, AVPacket *pkt, | |
1199 | const AVFrame *frame, int *got_packet) | ||
1200 | { | ||
1201 | 1005 | DVEncContext *s = c->priv_data; | |
1202 | int ret; | ||
1203 | |||
1204 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1005 times.
|
1005 | if ((ret = ff_get_encode_buffer(c, pkt, s->sys->frame_size, 0)) < 0) |
1205 | ✗ | return ret; | |
1206 | /* Fixme: Only zero the part that is not overwritten later. */ | ||
1207 | 1005 | memset(pkt->data, 0, pkt->size); | |
1208 | |||
1209 | 1005 | c->pix_fmt = s->sys->pix_fmt; | |
1210 | 1005 | s->frame = frame; | |
1211 | 1005 | s->buf = pkt->data; | |
1212 | |||
1213 | 1005 | dv_format_frame(s, pkt->data); | |
1214 | |||
1215 | 1005 | c->execute(c, dv_encode_video_segment, s->work_chunks, NULL, | |
1216 | dv_work_pool_size(s->sys), sizeof(DVwork_chunk)); | ||
1217 | |||
1218 | 1005 | emms_c(); | |
1219 | |||
1220 | 1005 | *got_packet = 1; | |
1221 | |||
1222 | 1005 | return 0; | |
1223 | } | ||
1224 | |||
1225 | #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM | ||
1226 | #define OFFSET(x) offsetof(DVEncContext, x) | ||
1227 | static const AVOption dv_options[] = { | ||
1228 | { "quant_deadzone", "Quantizer dead zone", OFFSET(quant_deadzone), AV_OPT_TYPE_INT, { .i64 = 7 }, 0, 1024, VE }, | ||
1229 | { NULL }, | ||
1230 | }; | ||
1231 | |||
1232 | static const AVClass dvvideo_encode_class = { | ||
1233 | .class_name = "dvvideo encoder", | ||
1234 | .item_name = av_default_item_name, | ||
1235 | .option = dv_options, | ||
1236 | .version = LIBAVUTIL_VERSION_INT, | ||
1237 | }; | ||
1238 | |||
1239 | const FFCodec ff_dvvideo_encoder = { | ||
1240 | .p.name = "dvvideo", | ||
1241 | CODEC_LONG_NAME("DV (Digital Video)"), | ||
1242 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
1243 | .p.id = AV_CODEC_ID_DVVIDEO, | ||
1244 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS | | ||
1245 | AV_CODEC_CAP_SLICE_THREADS | | ||
1246 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, | ||
1247 | .priv_data_size = sizeof(DVEncContext), | ||
1248 | .init = dvvideo_encode_init, | ||
1249 | FF_CODEC_ENCODE_CB(dvvideo_encode_frame), | ||
1250 | .p.pix_fmts = (const enum AVPixelFormat[]) { | ||
1251 | AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV422P, | ||
1252 | AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE | ||
1253 | }, | ||
1254 | .color_ranges = AVCOL_RANGE_MPEG, | ||
1255 | .p.priv_class = &dvvideo_encode_class, | ||
1256 | }; | ||
1257 |