Mercurial > libavcodec.hg
annotate dv.c @ 1414:5ee7bd7ee76d libavcodec
memmove fixes (Jon Burgess)
author | bellard |
---|---|
date | Sun, 24 Aug 2003 22:01:33 +0000 |
parents | f5318caa93f4 |
children | 8edad1e372d1 |
rev | line source |
---|---|
723 | 1 /* |
2 * DV decoder | |
3 * Copyright (c) 2002 Fabrice Bellard. | |
4 * | |
5 * This library is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2 of the License, or (at your option) any later version. | |
9 * | |
10 * This library is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Lesser General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Lesser General Public | |
16 * License along with this library; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 */ | |
1106 | 19 |
20 /** | |
21 * @file dv.c | |
22 * DV decoder. | |
23 */ | |
723 | 24 #include "avcodec.h" |
25 #include "dsputil.h" | |
26 #include "mpegvideo.h" | |
27 #include "simple_idct.h" | |
28 | |
29 #define NTSC_FRAME_SIZE 120000 | |
30 #define PAL_FRAME_SIZE 144000 | |
31 | |
32 #define TEX_VLC_BITS 9 | |
33 | |
34 typedef struct DVVideoDecodeContext { | |
35 AVCodecContext *avctx; | |
36 GetBitContext gb; | |
37 VLC *vlc; | |
38 int sampling_411; /* 0 = 420, 1 = 411 */ | |
39 int width, height; | |
1064 | 40 uint8_t *current_picture[3]; /* picture structure */ |
925 | 41 AVFrame picture; |
723 | 42 int linesize[3]; |
43 DCTELEM block[5*6][64] __align8; | |
1064 | 44 uint8_t dv_zigzag[2][64]; |
45 uint8_t idct_permutation[64]; | |
723 | 46 /* XXX: move it to static storage ? */ |
1064 | 47 uint8_t dv_shift[2][22][64]; |
48 void (*idct_put[2])(uint8_t *dest, int line_size, DCTELEM *block); | |
723 | 49 } DVVideoDecodeContext; |
50 | |
51 #include "dvdata.h" | |
52 | |
53 static VLC dv_vlc; | |
54 /* XXX: also include quantization */ | |
55 static RL_VLC_ELEM *dv_rl_vlc[1]; | |
56 | |
57 static void dv_build_unquantize_tables(DVVideoDecodeContext *s) | |
58 { | |
725 | 59 int i, q, j; |
723 | 60 |
61 /* NOTE: max left shift is 6 */ | |
725 | 62 for(q = 0; q < 22; q++) { |
63 /* 88 unquant */ | |
64 for(i = 1; i < 64; i++) { | |
65 /* 88 table */ | |
66 j = s->idct_permutation[i]; | |
67 s->dv_shift[0][q][j] = | |
68 dv_quant_shifts[q][dv_88_areas[i]] + 1; | |
69 } | |
70 | |
71 /* 248 unquant */ | |
72 for(i = 1; i < 64; i++) { | |
73 /* 248 table */ | |
74 s->dv_shift[1][q][i] = | |
75 dv_quant_shifts[q][dv_248_areas[i]] + 1; | |
723 | 76 } |
77 } | |
78 } | |
79 | |
80 static int dvvideo_decode_init(AVCodecContext *avctx) | |
81 { | |
82 DVVideoDecodeContext *s = avctx->priv_data; | |
725 | 83 MpegEncContext s2; |
1070
6da5ae9ee199
more #ifdef CONFIG_ENCODERS patch by (Wolfgang Hesseler <qv at multimediaware dot com>) with modifications by me (s/WOLFGANG/CONFIG_ENCODERS/ and some other fixes)
michaelni
parents:
1064
diff
changeset
|
84 static int done=0; |
723 | 85 |
86 if (!done) { | |
87 int i; | |
88 | |
89 done = 1; | |
90 | |
91 /* NOTE: as a trick, we use the fact the no codes are unused | |
92 to accelerate the parsing of partial codes */ | |
93 init_vlc(&dv_vlc, TEX_VLC_BITS, NB_DV_VLC, | |
94 dv_vlc_len, 1, 1, dv_vlc_bits, 2, 2); | |
95 | |
96 dv_rl_vlc[0] = av_malloc(dv_vlc.table_size * sizeof(RL_VLC_ELEM)); | |
97 for(i = 0; i < dv_vlc.table_size; i++){ | |
98 int code= dv_vlc.table[i][0]; | |
99 int len = dv_vlc.table[i][1]; | |
100 int level, run; | |
101 | |
102 if(len<0){ //more bits needed | |
103 run= 0; | |
104 level= code; | |
105 } else if (code == (NB_DV_VLC - 1)) { | |
106 /* EOB */ | |
107 run = 0; | |
108 level = 256; | |
109 } else { | |
110 run= dv_vlc_run[code] + 1; | |
111 level= dv_vlc_level[code]; | |
112 } | |
113 dv_rl_vlc[0][i].len = len; | |
114 dv_rl_vlc[0][i].level = level; | |
115 dv_rl_vlc[0][i].run = run; | |
116 } | |
117 } | |
725 | 118 |
119 /* ugly way to get the idct & scantable */ | |
120 /* XXX: fix it */ | |
121 memset(&s2, 0, sizeof(MpegEncContext)); | |
122 s2.avctx = avctx; | |
1092 | 123 dsputil_init(&s2.dsp, avctx); |
726
a91203b34e71
moved dct init out from mpv_common_init to dct_common_init (for less-uglier way for dv)
al3x
parents:
725
diff
changeset
|
124 if (DCT_common_init(&s2) < 0) |
725 | 125 return -1; |
126 | |
1092 | 127 s->idct_put[0] = s2.dsp.idct_put; |
128 memcpy(s->idct_permutation, s2.dsp.idct_permutation, 64); | |
725 | 129 memcpy(s->dv_zigzag[0], s2.intra_scantable.permutated, 64); |
130 | |
131 /* XXX: use MMX also for idct248 */ | |
132 s->idct_put[1] = simple_idct248_put; | |
133 memcpy(s->dv_zigzag[1], dv_248_zigzag, 64); | |
134 | |
723 | 135 /* XXX: do it only for constant case */ |
136 dv_build_unquantize_tables(s); | |
903 | 137 |
723 | 138 return 0; |
139 } | |
140 | |
141 //#define VLC_DEBUG | |
142 | |
725 | 143 typedef struct BlockInfo { |
1064 | 144 const uint8_t *shift_table; |
145 const uint8_t *scan_table; | |
146 uint8_t pos; /* position in block */ | |
147 uint8_t eob_reached; /* true if EOB has been reached */ | |
148 uint8_t dct_mode; | |
149 uint8_t partial_bit_count; | |
150 uint16_t partial_bit_buffer; | |
725 | 151 int shift_offset; |
152 } BlockInfo; | |
723 | 153 |
154 /* block size in bits */ | |
1064 | 155 static const uint16_t block_sizes[6] = { |
723 | 156 112, 112, 112, 112, 80, 80 |
157 }; | |
158 | |
159 #ifndef ALT_BITSTREAM_READER | |
1256
b8e1c17b8d7d
some libmpeg2 style bitstream reader fixes (no dv doesnt yet work with it)
michaelni
parents:
1228
diff
changeset
|
160 #warning only works with ALT_BITSTREAM_READER |
723 | 161 #endif |
162 | |
163 /* decode ac coefs */ | |
164 static void dv_decode_ac(DVVideoDecodeContext *s, | |
1008 | 165 BlockInfo *mb, DCTELEM *block, int last_index) |
723 | 166 { |
167 int last_re_index; | |
725 | 168 int shift_offset = mb->shift_offset; |
1064 | 169 const uint8_t *scan_table = mb->scan_table; |
170 const uint8_t *shift_table = mb->shift_table; | |
725 | 171 int pos = mb->pos; |
723 | 172 int level, pos1, sign, run; |
173 int partial_bit_count; | |
1256
b8e1c17b8d7d
some libmpeg2 style bitstream reader fixes (no dv doesnt yet work with it)
michaelni
parents:
1228
diff
changeset
|
174 #ifndef ALT_BITSTREAM_READER //FIXME |
b8e1c17b8d7d
some libmpeg2 style bitstream reader fixes (no dv doesnt yet work with it)
michaelni
parents:
1228
diff
changeset
|
175 int re_index=0; |
b8e1c17b8d7d
some libmpeg2 style bitstream reader fixes (no dv doesnt yet work with it)
michaelni
parents:
1228
diff
changeset
|
176 int re1_index=0; |
b8e1c17b8d7d
some libmpeg2 style bitstream reader fixes (no dv doesnt yet work with it)
michaelni
parents:
1228
diff
changeset
|
177 #endif |
723 | 178 OPEN_READER(re, &s->gb); |
179 | |
180 #ifdef VLC_DEBUG | |
725 | 181 printf("start\n"); |
723 | 182 #endif |
183 | |
184 /* if we must parse a partial vlc, we do it here */ | |
725 | 185 partial_bit_count = mb->partial_bit_count; |
723 | 186 if (partial_bit_count > 0) { |
1064 | 187 uint8_t buf[4]; |
188 uint32_t v; | |
723 | 189 int l, l1; |
190 GetBitContext gb1; | |
191 | |
192 /* build the dummy bit buffer */ | |
193 l = 16 - partial_bit_count; | |
194 UPDATE_CACHE(re, &s->gb); | |
195 #ifdef VLC_DEBUG | |
196 printf("show=%04x\n", SHOW_UBITS(re, &s->gb, 16)); | |
197 #endif | |
725 | 198 v = (mb->partial_bit_buffer << l) | SHOW_UBITS(re, &s->gb, l); |
723 | 199 buf[0] = v >> 8; |
200 buf[1] = v; | |
201 #ifdef VLC_DEBUG | |
202 printf("v=%04x cnt=%d %04x\n", | |
725 | 203 v, partial_bit_count, (mb->partial_bit_buffer << l)); |
723 | 204 #endif |
205 /* try to read the codeword */ | |
1025
1f9afd8b9131
GetBitContext.size is allways multiplied by 8 -> use size_in_bits to avoid useless *8 in a few inner loops
michaelni
parents:
1008
diff
changeset
|
206 init_get_bits(&gb1, buf, 4*8); |
723 | 207 { |
208 OPEN_READER(re1, &gb1); | |
209 UPDATE_CACHE(re1, &gb1); | |
210 GET_RL_VLC(level, run, re1, &gb1, dv_rl_vlc[0], | |
211 TEX_VLC_BITS, 2); | |
212 l = re1_index; | |
213 CLOSE_READER(re1, &gb1); | |
214 } | |
215 #ifdef VLC_DEBUG | |
216 printf("****run=%d level=%d size=%d\n", run, level, l); | |
217 #endif | |
218 /* compute codeword length */ | |
219 l1 = (level != 256 && level != 0); | |
220 /* if too long, we cannot parse */ | |
221 l -= partial_bit_count; | |
222 if ((re_index + l + l1) > last_index) | |
223 return; | |
224 /* skip read bits */ | |
225 last_re_index = 0; /* avoid warning */ | |
226 re_index += l; | |
725 | 227 /* by definition, if we can read the vlc, all partial bits |
723 | 228 will be read (otherwise we could have read the vlc before) */ |
725 | 229 mb->partial_bit_count = 0; |
723 | 230 UPDATE_CACHE(re, &s->gb); |
231 goto handle_vlc; | |
232 } | |
233 | |
234 /* get the AC coefficients until last_index is reached */ | |
235 for(;;) { | |
236 UPDATE_CACHE(re, &s->gb); | |
237 #ifdef VLC_DEBUG | |
238 printf("%2d: bits=%04x index=%d\n", | |
239 pos, SHOW_UBITS(re, &s->gb, 16), re_index); | |
240 #endif | |
241 last_re_index = re_index; | |
242 GET_RL_VLC(level, run, re, &s->gb, dv_rl_vlc[0], | |
243 TEX_VLC_BITS, 2); | |
244 handle_vlc: | |
245 #ifdef VLC_DEBUG | |
246 printf("run=%d level=%d\n", run, level); | |
247 #endif | |
248 if (level == 256) { | |
249 if (re_index > last_index) { | |
250 cannot_read: | |
251 /* put position before read code */ | |
252 re_index = last_re_index; | |
725 | 253 mb->eob_reached = 0; |
723 | 254 break; |
255 } | |
256 /* EOB */ | |
725 | 257 mb->eob_reached = 1; |
723 | 258 break; |
259 } else if (level != 0) { | |
260 if ((re_index + 1) > last_index) | |
261 goto cannot_read; | |
262 sign = SHOW_SBITS(re, &s->gb, 1); | |
263 level = (level ^ sign) - sign; | |
264 LAST_SKIP_BITS(re, &s->gb, 1); | |
265 pos += run; | |
266 /* error */ | |
267 if (pos >= 64) { | |
268 goto read_error; | |
269 } | |
270 pos1 = scan_table[pos]; | |
725 | 271 level = level << (shift_table[pos1] + shift_offset); |
723 | 272 block[pos1] = level; |
273 // printf("run=%d level=%d shift=%d\n", run, level, shift_table[pos1]); | |
274 } else { | |
275 if (re_index > last_index) | |
276 goto cannot_read; | |
277 /* level is zero: means run without coding. No | |
278 sign is coded */ | |
279 pos += run; | |
280 /* error */ | |
281 if (pos >= 64) { | |
282 read_error: | |
283 #if defined(VLC_DEBUG) || 1 | |
1222
adcc6f345202
making it nicer to the client who doesn't expect errors messages in stdout
romansh
parents:
1221
diff
changeset
|
284 fprintf(stderr, "error pos=%d\n", pos); |
723 | 285 #endif |
286 /* for errors, we consider the eob is reached */ | |
725 | 287 mb->eob_reached = 1; |
723 | 288 break; |
289 } | |
290 } | |
291 } | |
292 CLOSE_READER(re, &s->gb); | |
725 | 293 mb->pos = pos; |
723 | 294 } |
295 | |
296 static inline void bit_copy(PutBitContext *pb, GetBitContext *gb, int bits_left) | |
297 { | |
298 while (bits_left >= 16) { | |
299 put_bits(pb, 16, get_bits(gb, 16)); | |
300 bits_left -= 16; | |
301 } | |
302 if (bits_left > 0) { | |
303 put_bits(pb, bits_left, get_bits(gb, bits_left)); | |
304 } | |
305 } | |
306 | |
307 /* mb_x and mb_y are in units of 8 pixels */ | |
308 static inline void dv_decode_video_segment(DVVideoDecodeContext *s, | |
1064 | 309 uint8_t *buf_ptr1, |
310 const uint16_t *mb_pos_ptr) | |
723 | 311 { |
312 int quant, dc, dct_mode, class1, j; | |
313 int mb_index, mb_x, mb_y, v, last_index; | |
314 DCTELEM *block, *block1; | |
315 int c_offset, bits_left; | |
1064 | 316 uint8_t *y_ptr; |
725 | 317 BlockInfo mb_data[5 * 6], *mb, *mb1; |
1064 | 318 void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block); |
319 uint8_t *buf_ptr; | |
723 | 320 PutBitContext pb, vs_pb; |
1064 | 321 uint8_t mb_bit_buffer[80 + 4]; /* allow some slack */ |
725 | 322 int mb_bit_count; |
1064 | 323 uint8_t vs_bit_buffer[5 * 80 + 4]; /* allow some slack */ |
723 | 324 int vs_bit_count; |
325 | |
326 memset(s->block, 0, sizeof(s->block)); | |
327 | |
328 /* pass 1 : read DC and AC coefficients in blocks */ | |
329 buf_ptr = buf_ptr1; | |
330 block1 = &s->block[0][0]; | |
725 | 331 mb1 = mb_data; |
723 | 332 init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80, NULL, NULL); |
333 vs_bit_count = 0; | |
334 for(mb_index = 0; mb_index < 5; mb_index++) { | |
335 /* skip header */ | |
336 quant = buf_ptr[3] & 0x0f; | |
337 buf_ptr += 4; | |
725 | 338 init_put_bits(&pb, mb_bit_buffer, 80, NULL, NULL); |
339 mb_bit_count = 0; | |
340 mb = mb1; | |
723 | 341 block = block1; |
342 for(j = 0;j < 6; j++) { | |
343 /* NOTE: size is not important here */ | |
1025
1f9afd8b9131
GetBitContext.size is allways multiplied by 8 -> use size_in_bits to avoid useless *8 in a few inner loops
michaelni
parents:
1008
diff
changeset
|
344 init_get_bits(&s->gb, buf_ptr, 14*8); |
723 | 345 |
346 /* get the dc */ | |
347 dc = get_bits(&s->gb, 9); | |
348 dc = (dc << (32 - 9)) >> (32 - 9); | |
349 dct_mode = get_bits1(&s->gb); | |
725 | 350 mb->dct_mode = dct_mode; |
351 mb->scan_table = s->dv_zigzag[dct_mode]; | |
723 | 352 class1 = get_bits(&s->gb, 2); |
725 | 353 mb->shift_offset = (class1 == 3); |
354 mb->shift_table = s->dv_shift[dct_mode] | |
723 | 355 [quant + dv_quant_offset[class1]]; |
356 dc = dc << 2; | |
357 /* convert to unsigned because 128 is not added in the | |
358 standard IDCT */ | |
359 dc += 1024; | |
360 block[0] = dc; | |
361 last_index = block_sizes[j]; | |
362 buf_ptr += last_index >> 3; | |
725 | 363 mb->pos = 0; |
364 mb->partial_bit_count = 0; | |
723 | 365 |
725 | 366 dv_decode_ac(s, mb, block, last_index); |
723 | 367 |
368 /* write the remaining bits in a new buffer only if the | |
369 block is finished */ | |
1256
b8e1c17b8d7d
some libmpeg2 style bitstream reader fixes (no dv doesnt yet work with it)
michaelni
parents:
1228
diff
changeset
|
370 bits_left = last_index - get_bits_count(&s->gb); |
725 | 371 if (mb->eob_reached) { |
372 mb->partial_bit_count = 0; | |
373 mb_bit_count += bits_left; | |
723 | 374 bit_copy(&pb, &s->gb, bits_left); |
375 } else { | |
376 /* should be < 16 bits otherwise a codeword could have | |
377 been parsed */ | |
725 | 378 mb->partial_bit_count = bits_left; |
379 mb->partial_bit_buffer = get_bits(&s->gb, bits_left); | |
723 | 380 } |
381 block += 64; | |
725 | 382 mb++; |
723 | 383 } |
384 | |
385 flush_put_bits(&pb); | |
386 | |
387 /* pass 2 : we can do it just after */ | |
388 #ifdef VLC_DEBUG | |
725 | 389 printf("***pass 2 size=%d\n", mb_bit_count); |
723 | 390 #endif |
391 block = block1; | |
725 | 392 mb = mb1; |
1025
1f9afd8b9131
GetBitContext.size is allways multiplied by 8 -> use size_in_bits to avoid useless *8 in a few inner loops
michaelni
parents:
1008
diff
changeset
|
393 init_get_bits(&s->gb, mb_bit_buffer, 80*8); |
723 | 394 for(j = 0;j < 6; j++) { |
1256
b8e1c17b8d7d
some libmpeg2 style bitstream reader fixes (no dv doesnt yet work with it)
michaelni
parents:
1228
diff
changeset
|
395 if (!mb->eob_reached && get_bits_count(&s->gb) < mb_bit_count) { |
725 | 396 dv_decode_ac(s, mb, block, mb_bit_count); |
723 | 397 /* if still not finished, no need to parse other blocks */ |
725 | 398 if (!mb->eob_reached) { |
723 | 399 /* we could not parse the current AC coefficient, |
400 so we add the remaining bytes */ | |
1256
b8e1c17b8d7d
some libmpeg2 style bitstream reader fixes (no dv doesnt yet work with it)
michaelni
parents:
1228
diff
changeset
|
401 bits_left = mb_bit_count - get_bits_count(&s->gb); |
723 | 402 if (bits_left > 0) { |
725 | 403 mb->partial_bit_count += bits_left; |
404 mb->partial_bit_buffer = | |
405 (mb->partial_bit_buffer << bits_left) | | |
723 | 406 get_bits(&s->gb, bits_left); |
407 } | |
408 goto next_mb; | |
409 } | |
410 } | |
411 block += 64; | |
725 | 412 mb++; |
723 | 413 } |
414 /* all blocks are finished, so the extra bytes can be used at | |
415 the video segment level */ | |
1256
b8e1c17b8d7d
some libmpeg2 style bitstream reader fixes (no dv doesnt yet work with it)
michaelni
parents:
1228
diff
changeset
|
416 bits_left = mb_bit_count - get_bits_count(&s->gb); |
723 | 417 vs_bit_count += bits_left; |
418 bit_copy(&vs_pb, &s->gb, bits_left); | |
419 next_mb: | |
725 | 420 mb1 += 6; |
723 | 421 block1 += 6 * 64; |
422 } | |
423 | |
424 /* we need a pass other the whole video segment */ | |
425 flush_put_bits(&vs_pb); | |
426 | |
427 #ifdef VLC_DEBUG | |
428 printf("***pass 3 size=%d\n", vs_bit_count); | |
429 #endif | |
430 block = &s->block[0][0]; | |
431 mb = mb_data; | |
1025
1f9afd8b9131
GetBitContext.size is allways multiplied by 8 -> use size_in_bits to avoid useless *8 in a few inner loops
michaelni
parents:
1008
diff
changeset
|
432 init_get_bits(&s->gb, vs_bit_buffer, 5 * 80*8); |
723 | 433 for(mb_index = 0; mb_index < 5; mb_index++) { |
434 for(j = 0;j < 6; j++) { | |
725 | 435 if (!mb->eob_reached) { |
723 | 436 #ifdef VLC_DEBUG |
437 printf("start %d:%d\n", mb_index, j); | |
438 #endif | |
725 | 439 dv_decode_ac(s, mb, block, vs_bit_count); |
723 | 440 } |
441 block += 64; | |
725 | 442 mb++; |
723 | 443 } |
444 } | |
445 | |
446 /* compute idct and place blocks */ | |
447 block = &s->block[0][0]; | |
448 mb = mb_data; | |
449 for(mb_index = 0; mb_index < 5; mb_index++) { | |
450 v = *mb_pos_ptr++; | |
451 mb_x = v & 0xff; | |
452 mb_y = v >> 8; | |
453 y_ptr = s->current_picture[0] + (mb_y * s->linesize[0] * 8) + (mb_x * 8); | |
454 if (s->sampling_411) | |
455 c_offset = (mb_y * s->linesize[1] * 8) + ((mb_x >> 2) * 8); | |
456 else | |
457 c_offset = ((mb_y >> 1) * s->linesize[1] * 8) + ((mb_x >> 1) * 8); | |
458 for(j = 0;j < 6; j++) { | |
725 | 459 idct_put = s->idct_put[mb->dct_mode]; |
723 | 460 if (j < 4) { |
737 | 461 if (s->sampling_411 && mb_x < (704 / 8)) { |
462 /* NOTE: at end of line, the macroblock is handled as 420 */ | |
723 | 463 idct_put(y_ptr + (j * 8), s->linesize[0], block); |
464 } else { | |
465 idct_put(y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->linesize[0]), | |
466 s->linesize[0], block); | |
467 } | |
468 } else { | |
737 | 469 if (s->sampling_411 && mb_x >= (704 / 8)) { |
470 uint8_t pixels[64], *c_ptr, *c_ptr1, *ptr; | |
471 int y, linesize; | |
472 /* NOTE: at end of line, the macroblock is handled as 420 */ | |
473 idct_put(pixels, 8, block); | |
474 linesize = s->linesize[6 - j]; | |
475 c_ptr = s->current_picture[6 - j] + c_offset; | |
476 ptr = pixels; | |
477 for(y = 0;y < 8; y++) { | |
478 /* convert to 411P */ | |
1270
f5318caa93f4
seems i guessed correctly (last 411 chroma block isnt scaled but cut and reordered)
michaelni
parents:
1256
diff
changeset
|
479 c_ptr1 = c_ptr + 8*linesize; |
f5318caa93f4
seems i guessed correctly (last 411 chroma block isnt scaled but cut and reordered)
michaelni
parents:
1256
diff
changeset
|
480 c_ptr[0]= ptr[0]; c_ptr1[0]= ptr[4]; |
f5318caa93f4
seems i guessed correctly (last 411 chroma block isnt scaled but cut and reordered)
michaelni
parents:
1256
diff
changeset
|
481 c_ptr[1]= ptr[1]; c_ptr1[1]= ptr[5]; |
f5318caa93f4
seems i guessed correctly (last 411 chroma block isnt scaled but cut and reordered)
michaelni
parents:
1256
diff
changeset
|
482 c_ptr[2]= ptr[2]; c_ptr1[2]= ptr[6]; |
f5318caa93f4
seems i guessed correctly (last 411 chroma block isnt scaled but cut and reordered)
michaelni
parents:
1256
diff
changeset
|
483 c_ptr[3]= ptr[3]; c_ptr1[3]= ptr[7]; |
f5318caa93f4
seems i guessed correctly (last 411 chroma block isnt scaled but cut and reordered)
michaelni
parents:
1256
diff
changeset
|
484 c_ptr += linesize; |
737 | 485 ptr += 8; |
486 } | |
487 } else { | |
488 /* don't ask me why they inverted Cb and Cr ! */ | |
489 idct_put(s->current_picture[6 - j] + c_offset, | |
490 s->linesize[6 - j], block); | |
491 } | |
723 | 492 } |
493 block += 64; | |
725 | 494 mb++; |
723 | 495 } |
496 } | |
497 } | |
498 | |
499 | |
500 /* NOTE: exactly one frame must be given (120000 bytes for NTSC, | |
501 144000 bytes for PAL) */ | |
502 static int dvvideo_decode_frame(AVCodecContext *avctx, | |
503 void *data, int *data_size, | |
1064 | 504 uint8_t *buf, int buf_size) |
723 | 505 { |
506 DVVideoDecodeContext *s = avctx->priv_data; | |
862 | 507 int sct, dsf, apt, ds, nb_dif_segs, vs, width, height, i, packet_size; |
1064 | 508 uint8_t *buf_ptr; |
509 const uint16_t *mb_pos_ptr; | |
723 | 510 |
511 /* parse id */ | |
1025
1f9afd8b9131
GetBitContext.size is allways multiplied by 8 -> use size_in_bits to avoid useless *8 in a few inner loops
michaelni
parents:
1008
diff
changeset
|
512 init_get_bits(&s->gb, buf, buf_size*8); |
723 | 513 sct = get_bits(&s->gb, 3); |
514 if (sct != 0) | |
515 return -1; | |
516 skip_bits(&s->gb, 5); | |
517 get_bits(&s->gb, 4); /* dsn (sequence number */ | |
518 get_bits(&s->gb, 1); /* fsc (channel number) */ | |
519 skip_bits(&s->gb, 3); | |
520 get_bits(&s->gb, 8); /* dbn (diff block number 0-134) */ | |
521 | |
522 dsf = get_bits(&s->gb, 1); /* 0 = NTSC 1 = PAL */ | |
523 if (get_bits(&s->gb, 1) != 0) | |
524 return -1; | |
525 skip_bits(&s->gb, 11); | |
526 apt = get_bits(&s->gb, 3); /* apt */ | |
527 | |
528 get_bits(&s->gb, 1); /* tf1 */ | |
529 skip_bits(&s->gb, 4); | |
530 get_bits(&s->gb, 3); /* ap1 */ | |
531 | |
532 get_bits(&s->gb, 1); /* tf2 */ | |
533 skip_bits(&s->gb, 4); | |
534 get_bits(&s->gb, 3); /* ap2 */ | |
535 | |
536 get_bits(&s->gb, 1); /* tf3 */ | |
537 skip_bits(&s->gb, 4); | |
538 get_bits(&s->gb, 3); /* ap3 */ | |
539 | |
540 /* init size */ | |
541 width = 720; | |
542 if (dsf) { | |
1126
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
543 avctx->frame_rate = 25; |
1221 | 544 avctx->frame_rate_base = 1; |
738 | 545 packet_size = PAL_FRAME_SIZE; |
723 | 546 height = 576; |
547 nb_dif_segs = 12; | |
548 } else { | |
1221 | 549 avctx->frame_rate = 30000; |
550 avctx->frame_rate_base = 1001; | |
738 | 551 packet_size = NTSC_FRAME_SIZE; |
723 | 552 height = 480; |
553 nb_dif_segs = 10; | |
554 } | |
738 | 555 /* NOTE: we only accept several full frames */ |
556 if (buf_size < packet_size) | |
557 return -1; | |
558 | |
1087
7f10d38721ed
support for PAL 4:1:1 SMPTE 314M DV streams patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
1070
diff
changeset
|
559 /* NTSC[dsf == 0] is always 720x480, 4:1:1 |
7f10d38721ed
support for PAL 4:1:1 SMPTE 314M DV streams patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
1070
diff
changeset
|
560 * PAL[dsf == 1] is always 720x576, 4:2:0 for IEC 68134[apt == 0] |
7f10d38721ed
support for PAL 4:1:1 SMPTE 314M DV streams patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
1070
diff
changeset
|
561 * but for the SMPTE 314M[apt == 1] it is 720x576, 4:1:1 |
7f10d38721ed
support for PAL 4:1:1 SMPTE 314M DV streams patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
1070
diff
changeset
|
562 */ |
7f10d38721ed
support for PAL 4:1:1 SMPTE 314M DV streams patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
1070
diff
changeset
|
563 s->sampling_411 = !dsf || apt; |
848
c075abb7fb3c
10l - set pixelfmt (colorspace) _before_ calling get_buffer() callback
arpi_esp
parents:
835
diff
changeset
|
564 if (s->sampling_411) { |
1087
7f10d38721ed
support for PAL 4:1:1 SMPTE 314M DV streams patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
1070
diff
changeset
|
565 mb_pos_ptr = dsf ? dv_place_411P : dv_place_411; |
848
c075abb7fb3c
10l - set pixelfmt (colorspace) _before_ calling get_buffer() callback
arpi_esp
parents:
835
diff
changeset
|
566 avctx->pix_fmt = PIX_FMT_YUV411P; |
c075abb7fb3c
10l - set pixelfmt (colorspace) _before_ calling get_buffer() callback
arpi_esp
parents:
835
diff
changeset
|
567 } else { |
733 | 568 mb_pos_ptr = dv_place_420; |
848
c075abb7fb3c
10l - set pixelfmt (colorspace) _before_ calling get_buffer() callback
arpi_esp
parents:
835
diff
changeset
|
569 avctx->pix_fmt = PIX_FMT_YUV420P; |
c075abb7fb3c
10l - set pixelfmt (colorspace) _before_ calling get_buffer() callback
arpi_esp
parents:
835
diff
changeset
|
570 } |
c075abb7fb3c
10l - set pixelfmt (colorspace) _before_ calling get_buffer() callback
arpi_esp
parents:
835
diff
changeset
|
571 |
c075abb7fb3c
10l - set pixelfmt (colorspace) _before_ calling get_buffer() callback
arpi_esp
parents:
835
diff
changeset
|
572 avctx->width = width; |
c075abb7fb3c
10l - set pixelfmt (colorspace) _before_ calling get_buffer() callback
arpi_esp
parents:
835
diff
changeset
|
573 avctx->height = height; |
1167
35b80080b2db
support for DV aspect ratio and erroneous audio patch by (Dan Dennedy (dan at dennedy dot org) and Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
1156
diff
changeset
|
574 |
35b80080b2db
support for DV aspect ratio and erroneous audio patch by (Dan Dennedy (dan at dennedy dot org) and Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
1156
diff
changeset
|
575 /* Once again, this is pretty complicated by the fact that the same |
35b80080b2db
support for DV aspect ratio and erroneous audio patch by (Dan Dennedy (dan at dennedy dot org) and Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
1156
diff
changeset
|
576 * field is used differently by IEC 68134[apt == 0] and |
35b80080b2db
support for DV aspect ratio and erroneous audio patch by (Dan Dennedy (dan at dennedy dot org) and Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
1156
diff
changeset
|
577 * SMPTE 314M[apt == 1]. |
35b80080b2db
support for DV aspect ratio and erroneous audio patch by (Dan Dennedy (dan at dennedy dot org) and Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
1156
diff
changeset
|
578 */ |
35b80080b2db
support for DV aspect ratio and erroneous audio patch by (Dan Dennedy (dan at dennedy dot org) and Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
1156
diff
changeset
|
579 if (buf[VAUX_TC61_OFFSET] == 0x61 && |
35b80080b2db
support for DV aspect ratio and erroneous audio patch by (Dan Dennedy (dan at dennedy dot org) and Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
1156
diff
changeset
|
580 ((apt == 0 && (buf[VAUX_TC61_OFFSET + 2] & 0x07) == 0x07) || |
35b80080b2db
support for DV aspect ratio and erroneous audio patch by (Dan Dennedy (dan at dennedy dot org) and Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
1156
diff
changeset
|
581 (apt == 1 && (buf[VAUX_TC61_OFFSET + 2] & 0x07) == 0x02))) |
35b80080b2db
support for DV aspect ratio and erroneous audio patch by (Dan Dennedy (dan at dennedy dot org) and Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
1156
diff
changeset
|
582 avctx->aspect_ratio = 16.0 / 9.0; |
35b80080b2db
support for DV aspect ratio and erroneous audio patch by (Dan Dennedy (dan at dennedy dot org) and Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
1156
diff
changeset
|
583 else |
35b80080b2db
support for DV aspect ratio and erroneous audio patch by (Dan Dennedy (dan at dennedy dot org) and Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
1156
diff
changeset
|
584 avctx->aspect_ratio = 4.0 / 3.0; |
733 | 585 |
1228 | 586 if(s->picture.data[0]) |
587 avctx->release_buffer(avctx, &s->picture); | |
588 | |
903 | 589 s->picture.reference= 0; |
590 if(avctx->get_buffer(avctx, &s->picture) < 0) { | |
591 fprintf(stderr, "get_buffer() failed\n"); | |
592 return -1; | |
835 | 593 } |
594 | |
903 | 595 for(i=0;i<3;i++) { |
596 s->current_picture[i] = s->picture.data[i]; | |
597 s->linesize[i] = s->picture.linesize[i]; | |
598 if (!s->current_picture[i]) | |
599 return -1; | |
723 | 600 } |
903 | 601 s->width = width; |
602 s->height = height; | |
723 | 603 |
604 /* for each DIF segment */ | |
605 buf_ptr = buf; | |
606 for (ds = 0; ds < nb_dif_segs; ds++) { | |
607 buf_ptr += 6 * 80; /* skip DIF segment header */ | |
608 | |
609 for(vs = 0; vs < 27; vs++) { | |
610 if ((vs % 3) == 0) { | |
611 /* skip audio block */ | |
612 buf_ptr += 80; | |
613 } | |
614 dv_decode_video_segment(s, buf_ptr, mb_pos_ptr); | |
615 buf_ptr += 5 * 80; | |
616 mb_pos_ptr += 5; | |
617 } | |
618 } | |
619 | |
734
2d6b3e3d6c6f
10l - MMX/FPU state was not restored, causing nonsense fpu behaviour in caller (mplayer)
arpi_esp
parents:
733
diff
changeset
|
620 emms_c(); |
2d6b3e3d6c6f
10l - MMX/FPU state was not restored, causing nonsense fpu behaviour in caller (mplayer)
arpi_esp
parents:
733
diff
changeset
|
621 |
723 | 622 /* return image */ |
925 | 623 *data_size = sizeof(AVFrame); |
624 *(AVFrame*)data= s->picture; | |
903 | 625 |
738 | 626 return packet_size; |
723 | 627 } |
628 | |
629 static int dvvideo_decode_end(AVCodecContext *avctx) | |
630 { | |
631 DVVideoDecodeContext *s = avctx->priv_data; | |
1214 | 632 |
633 avcodec_default_free_buffers(avctx); | |
723 | 634 |
635 return 0; | |
636 } | |
637 | |
638 AVCodec dvvideo_decoder = { | |
639 "dvvideo", | |
640 CODEC_TYPE_VIDEO, | |
641 CODEC_ID_DVVIDEO, | |
642 sizeof(DVVideoDecodeContext), | |
643 dvvideo_decode_init, | |
644 NULL, | |
645 dvvideo_decode_end, | |
646 dvvideo_decode_frame, | |
835 | 647 CODEC_CAP_DR1, |
723 | 648 NULL |
649 }; | |
650 | |
651 typedef struct DVAudioDecodeContext { | |
652 AVCodecContext *avctx; | |
653 GetBitContext gb; | |
654 } DVAudioDecodeContext; | |
655 | |
656 static int dvaudio_decode_init(AVCodecContext *avctx) | |
657 { | |
658 // DVAudioDecodeContext *s = avctx->priv_data; | |
659 return 0; | |
660 } | |
661 | |
1064 | 662 static uint16_t dv_audio_12to16(uint16_t sample) |
1036 | 663 { |
1064 | 664 uint16_t shift, result; |
1036 | 665 |
666 sample = (sample < 0x800) ? sample : sample | 0xf000; | |
667 shift = (sample & 0xf00) >> 8; | |
668 | |
669 if (shift < 0x2 || shift > 0xd) { | |
670 result = sample; | |
671 } else if (shift < 0x8) { | |
672 shift--; | |
673 result = (sample - (256 * shift)) << shift; | |
674 } else { | |
675 shift = 0xe - shift; | |
676 result = ((sample + ((256 * shift) + 1)) << shift) - 1; | |
677 } | |
678 | |
679 return result; | |
680 } | |
681 | |
723 | 682 /* NOTE: exactly one frame must be given (120000 bytes for NTSC, |
1036 | 683 144000 bytes for PAL) |
684 | |
685 There's a couple of assumptions being made here: | |
1167
35b80080b2db
support for DV aspect ratio and erroneous audio patch by (Dan Dennedy (dan at dennedy dot org) and Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
1156
diff
changeset
|
686 1. By default we silence erroneous (0x8000/16bit 0x800/12bit) |
35b80080b2db
support for DV aspect ratio and erroneous audio patch by (Dan Dennedy (dan at dennedy dot org) and Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
1156
diff
changeset
|
687 audio samples. We can pass them upwards when ffmpeg will be ready |
35b80080b2db
support for DV aspect ratio and erroneous audio patch by (Dan Dennedy (dan at dennedy dot org) and Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
1156
diff
changeset
|
688 to deal with them. |
1036 | 689 2. We don't do software emphasis. |
1167
35b80080b2db
support for DV aspect ratio and erroneous audio patch by (Dan Dennedy (dan at dennedy dot org) and Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
1156
diff
changeset
|
690 3. Audio is always returned as 16bit linear samples: 12bit |
1036 | 691 nonlinear samples are converted into 16bit linear ones. |
692 */ | |
723 | 693 static int dvaudio_decode_frame(AVCodecContext *avctx, |
694 void *data, int *data_size, | |
1064 | 695 uint8_t *buf, int buf_size) |
723 | 696 { |
1036 | 697 DVVideoDecodeContext *s = avctx->priv_data; |
1064 | 698 const uint16_t (*unshuffle)[9]; |
1036 | 699 int smpls, freq, quant, sys, stride, difseg, ad, dp, nb_dif_segs, i; |
1064 | 700 uint16_t lc, rc; |
701 uint8_t *buf_ptr; | |
1036 | 702 |
703 /* parse id */ | |
1167
35b80080b2db
support for DV aspect ratio and erroneous audio patch by (Dan Dennedy (dan at dennedy dot org) and Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
1156
diff
changeset
|
704 init_get_bits(&s->gb, &buf[AAUX_AS_OFFSET], 5*8); |
1036 | 705 i = get_bits(&s->gb, 8); |
706 if (i != 0x50) { /* No audio ? */ | |
707 *data_size = 0; | |
708 return buf_size; | |
709 } | |
710 | |
711 get_bits(&s->gb, 1); /* 0 - locked audio, 1 - unlocked audio */ | |
712 skip_bits(&s->gb, 1); | |
713 smpls = get_bits(&s->gb, 6); /* samples in this frame - min. samples */ | |
714 | |
715 skip_bits(&s->gb, 8); | |
716 | |
717 skip_bits(&s->gb, 2); | |
718 sys = get_bits(&s->gb, 1); /* 0 - 60 fields, 1 = 50 fields */ | |
719 skip_bits(&s->gb, 5); | |
720 | |
721 get_bits(&s->gb, 1); /* 0 - emphasis on, 1 - emphasis off */ | |
722 get_bits(&s->gb, 1); /* 0 - reserved, 1 - emphasis time constant 50/15us */ | |
723 freq = get_bits(&s->gb, 3); /* 0 - 48KHz, 1 - 44,1kHz, 2 - 32 kHz */ | |
724 quant = get_bits(&s->gb, 3); /* 0 - 16bit linear, 1 - 12bit nonlinear */ | |
725 | |
726 if (quant > 1) | |
727 return -1; /* Unsupported quantization */ | |
728 | |
729 avctx->sample_rate = dv_audio_frequency[freq]; | |
1156
48efa413ac81
AVI type 1 support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
1126
diff
changeset
|
730 avctx->channels = 2; |
1221 | 731 avctx->bit_rate = avctx->channels * avctx->sample_rate * 16; |
1036 | 732 // What about: |
733 // avctx->frame_size = | |
734 | |
735 *data_size = (dv_audio_min_samples[sys][freq] + smpls) * | |
736 avctx->channels * 2; | |
737 | |
738 if (sys) { | |
739 nb_dif_segs = 12; | |
740 stride = 108; | |
741 unshuffle = dv_place_audio50; | |
742 } else { | |
743 nb_dif_segs = 10; | |
744 stride = 90; | |
745 unshuffle = dv_place_audio60; | |
746 } | |
747 | |
748 /* for each DIF segment */ | |
749 buf_ptr = buf; | |
750 for (difseg = 0; difseg < nb_dif_segs; difseg++) { | |
751 buf_ptr += 6 * 80; /* skip DIF segment header */ | |
752 for (ad = 0; ad < 9; ad++) { | |
753 | |
754 for (dp = 8; dp < 80; dp+=2) { | |
755 if (quant == 0) { /* 16bit quantization */ | |
756 i = unshuffle[difseg][ad] + (dp - 8)/2 * stride; | |
757 ((short *)data)[i] = (buf_ptr[dp] << 8) | buf_ptr[dp+1]; | |
1167
35b80080b2db
support for DV aspect ratio and erroneous audio patch by (Dan Dennedy (dan at dennedy dot org) and Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
1156
diff
changeset
|
758 if (((unsigned short *)data)[i] == 0x8000) |
35b80080b2db
support for DV aspect ratio and erroneous audio patch by (Dan Dennedy (dan at dennedy dot org) and Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
1156
diff
changeset
|
759 ((short *)data)[i] = 0; |
1036 | 760 } else { /* 12bit quantization */ |
761 if (difseg >= nb_dif_segs/2) | |
762 goto out; /* We're not doing 4ch at this time */ | |
763 | |
1064 | 764 lc = ((uint16_t)buf_ptr[dp] << 4) | |
765 ((uint16_t)buf_ptr[dp+2] >> 4); | |
766 rc = ((uint16_t)buf_ptr[dp+1] << 4) | | |
767 ((uint16_t)buf_ptr[dp+2] & 0x0f); | |
1167
35b80080b2db
support for DV aspect ratio and erroneous audio patch by (Dan Dennedy (dan at dennedy dot org) and Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
1156
diff
changeset
|
768 lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc)); |
35b80080b2db
support for DV aspect ratio and erroneous audio patch by (Dan Dennedy (dan at dennedy dot org) and Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
1156
diff
changeset
|
769 rc = (rc == 0x800 ? 0 : dv_audio_12to16(rc)); |
1036 | 770 |
771 i = unshuffle[difseg][ad] + (dp - 8)/3 * stride; | |
772 ((short *)data)[i] = lc; | |
773 i = unshuffle[difseg+nb_dif_segs/2][ad] + (dp - 8)/3 * stride; | |
774 ((short *)data)[i] = rc; | |
775 ++dp; | |
776 } | |
777 } | |
778 | |
779 buf_ptr += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */ | |
780 } | |
781 } | |
782 | |
783 out: | |
723 | 784 return buf_size; |
785 } | |
786 | |
787 static int dvaudio_decode_end(AVCodecContext *avctx) | |
788 { | |
789 // DVAudioDecodeContext *s = avctx->priv_data; | |
790 return 0; | |
791 } | |
792 | |
793 AVCodec dvaudio_decoder = { | |
794 "dvaudio", | |
795 CODEC_TYPE_AUDIO, | |
796 CODEC_ID_DVAUDIO, | |
797 sizeof(DVAudioDecodeContext), | |
798 dvaudio_decode_init, | |
799 NULL, | |
800 dvaudio_decode_end, | |
801 dvaudio_decode_frame, | |
802 0, | |
803 NULL | |
804 }; |