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