Mercurial > libavcodec.hg
annotate dv.c @ 1234:fc2a7eefa9cc libavcodec
svq3 decoder by anonymous
| author | michaelni |
|---|---|
| date | Fri, 09 May 2003 22:16:14 +0000 |
| parents | d63e0185a90f |
| children | b8e1c17b8d7d |
| 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 | |
| 160 #error only works with ALT_BITSTREAM_READER | |
| 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; | |
| 174 | |
| 175 OPEN_READER(re, &s->gb); | |
| 176 | |
| 177 #ifdef VLC_DEBUG | |
| 725 | 178 printf("start\n"); |
| 723 | 179 #endif |
| 180 | |
| 181 /* if we must parse a partial vlc, we do it here */ | |
| 725 | 182 partial_bit_count = mb->partial_bit_count; |
| 723 | 183 if (partial_bit_count > 0) { |
| 1064 | 184 uint8_t buf[4]; |
| 185 uint32_t v; | |
| 723 | 186 int l, l1; |
| 187 GetBitContext gb1; | |
| 188 | |
| 189 /* build the dummy bit buffer */ | |
| 190 l = 16 - partial_bit_count; | |
| 191 UPDATE_CACHE(re, &s->gb); | |
| 192 #ifdef VLC_DEBUG | |
| 193 printf("show=%04x\n", SHOW_UBITS(re, &s->gb, 16)); | |
| 194 #endif | |
| 725 | 195 v = (mb->partial_bit_buffer << l) | SHOW_UBITS(re, &s->gb, l); |
| 723 | 196 buf[0] = v >> 8; |
| 197 buf[1] = v; | |
| 198 #ifdef VLC_DEBUG | |
| 199 printf("v=%04x cnt=%d %04x\n", | |
| 725 | 200 v, partial_bit_count, (mb->partial_bit_buffer << l)); |
| 723 | 201 #endif |
| 202 /* 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
|
203 init_get_bits(&gb1, buf, 4*8); |
| 723 | 204 { |
| 205 OPEN_READER(re1, &gb1); | |
| 206 UPDATE_CACHE(re1, &gb1); | |
| 207 GET_RL_VLC(level, run, re1, &gb1, dv_rl_vlc[0], | |
| 208 TEX_VLC_BITS, 2); | |
| 209 l = re1_index; | |
| 210 CLOSE_READER(re1, &gb1); | |
| 211 } | |
| 212 #ifdef VLC_DEBUG | |
| 213 printf("****run=%d level=%d size=%d\n", run, level, l); | |
| 214 #endif | |
| 215 /* compute codeword length */ | |
| 216 l1 = (level != 256 && level != 0); | |
| 217 /* if too long, we cannot parse */ | |
| 218 l -= partial_bit_count; | |
| 219 if ((re_index + l + l1) > last_index) | |
| 220 return; | |
| 221 /* skip read bits */ | |
| 222 last_re_index = 0; /* avoid warning */ | |
| 223 re_index += l; | |
| 725 | 224 /* by definition, if we can read the vlc, all partial bits |
| 723 | 225 will be read (otherwise we could have read the vlc before) */ |
| 725 | 226 mb->partial_bit_count = 0; |
| 723 | 227 UPDATE_CACHE(re, &s->gb); |
| 228 goto handle_vlc; | |
| 229 } | |
| 230 | |
| 231 /* get the AC coefficients until last_index is reached */ | |
| 232 for(;;) { | |
| 233 UPDATE_CACHE(re, &s->gb); | |
| 234 #ifdef VLC_DEBUG | |
| 235 printf("%2d: bits=%04x index=%d\n", | |
| 236 pos, SHOW_UBITS(re, &s->gb, 16), re_index); | |
| 237 #endif | |
| 238 last_re_index = re_index; | |
| 239 GET_RL_VLC(level, run, re, &s->gb, dv_rl_vlc[0], | |
| 240 TEX_VLC_BITS, 2); | |
| 241 handle_vlc: | |
| 242 #ifdef VLC_DEBUG | |
| 243 printf("run=%d level=%d\n", run, level); | |
| 244 #endif | |
| 245 if (level == 256) { | |
| 246 if (re_index > last_index) { | |
| 247 cannot_read: | |
| 248 /* put position before read code */ | |
| 249 re_index = last_re_index; | |
| 725 | 250 mb->eob_reached = 0; |
| 723 | 251 break; |
| 252 } | |
| 253 /* EOB */ | |
| 725 | 254 mb->eob_reached = 1; |
| 723 | 255 break; |
| 256 } else if (level != 0) { | |
| 257 if ((re_index + 1) > last_index) | |
| 258 goto cannot_read; | |
| 259 sign = SHOW_SBITS(re, &s->gb, 1); | |
| 260 level = (level ^ sign) - sign; | |
| 261 LAST_SKIP_BITS(re, &s->gb, 1); | |
| 262 pos += run; | |
| 263 /* error */ | |
| 264 if (pos >= 64) { | |
| 265 goto read_error; | |
| 266 } | |
| 267 pos1 = scan_table[pos]; | |
| 725 | 268 level = level << (shift_table[pos1] + shift_offset); |
| 723 | 269 block[pos1] = level; |
| 270 // printf("run=%d level=%d shift=%d\n", run, level, shift_table[pos1]); | |
| 271 } else { | |
| 272 if (re_index > last_index) | |
| 273 goto cannot_read; | |
| 274 /* level is zero: means run without coding. No | |
| 275 sign is coded */ | |
| 276 pos += run; | |
| 277 /* error */ | |
| 278 if (pos >= 64) { | |
| 279 read_error: | |
| 280 #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
|
281 fprintf(stderr, "error pos=%d\n", pos); |
| 723 | 282 #endif |
| 283 /* for errors, we consider the eob is reached */ | |
| 725 | 284 mb->eob_reached = 1; |
| 723 | 285 break; |
| 286 } | |
| 287 } | |
| 288 } | |
| 289 CLOSE_READER(re, &s->gb); | |
| 725 | 290 mb->pos = pos; |
| 723 | 291 } |
| 292 | |
| 293 static inline void bit_copy(PutBitContext *pb, GetBitContext *gb, int bits_left) | |
| 294 { | |
| 295 while (bits_left >= 16) { | |
| 296 put_bits(pb, 16, get_bits(gb, 16)); | |
| 297 bits_left -= 16; | |
| 298 } | |
| 299 if (bits_left > 0) { | |
| 300 put_bits(pb, bits_left, get_bits(gb, bits_left)); | |
| 301 } | |
| 302 } | |
| 303 | |
| 304 /* mb_x and mb_y are in units of 8 pixels */ | |
| 305 static inline void dv_decode_video_segment(DVVideoDecodeContext *s, | |
| 1064 | 306 uint8_t *buf_ptr1, |
| 307 const uint16_t *mb_pos_ptr) | |
| 723 | 308 { |
| 309 int quant, dc, dct_mode, class1, j; | |
| 310 int mb_index, mb_x, mb_y, v, last_index; | |
| 311 DCTELEM *block, *block1; | |
| 312 int c_offset, bits_left; | |
| 1064 | 313 uint8_t *y_ptr; |
| 725 | 314 BlockInfo mb_data[5 * 6], *mb, *mb1; |
| 1064 | 315 void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block); |
| 316 uint8_t *buf_ptr; | |
| 723 | 317 PutBitContext pb, vs_pb; |
| 1064 | 318 uint8_t mb_bit_buffer[80 + 4]; /* allow some slack */ |
| 725 | 319 int mb_bit_count; |
| 1064 | 320 uint8_t vs_bit_buffer[5 * 80 + 4]; /* allow some slack */ |
| 723 | 321 int vs_bit_count; |
| 322 | |
| 323 memset(s->block, 0, sizeof(s->block)); | |
| 324 | |
| 325 /* pass 1 : read DC and AC coefficients in blocks */ | |
| 326 buf_ptr = buf_ptr1; | |
| 327 block1 = &s->block[0][0]; | |
| 725 | 328 mb1 = mb_data; |
| 723 | 329 init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80, NULL, NULL); |
| 330 vs_bit_count = 0; | |
| 331 for(mb_index = 0; mb_index < 5; mb_index++) { | |
| 332 /* skip header */ | |
| 333 quant = buf_ptr[3] & 0x0f; | |
| 334 buf_ptr += 4; | |
| 725 | 335 init_put_bits(&pb, mb_bit_buffer, 80, NULL, NULL); |
| 336 mb_bit_count = 0; | |
| 337 mb = mb1; | |
| 723 | 338 block = block1; |
| 339 for(j = 0;j < 6; j++) { | |
| 340 /* 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
|
341 init_get_bits(&s->gb, buf_ptr, 14*8); |
| 723 | 342 |
| 343 /* get the dc */ | |
| 344 dc = get_bits(&s->gb, 9); | |
| 345 dc = (dc << (32 - 9)) >> (32 - 9); | |
| 346 dct_mode = get_bits1(&s->gb); | |
| 725 | 347 mb->dct_mode = dct_mode; |
| 348 mb->scan_table = s->dv_zigzag[dct_mode]; | |
| 723 | 349 class1 = get_bits(&s->gb, 2); |
| 725 | 350 mb->shift_offset = (class1 == 3); |
| 351 mb->shift_table = s->dv_shift[dct_mode] | |
| 723 | 352 [quant + dv_quant_offset[class1]]; |
| 353 dc = dc << 2; | |
| 354 /* convert to unsigned because 128 is not added in the | |
| 355 standard IDCT */ | |
| 356 dc += 1024; | |
| 357 block[0] = dc; | |
| 358 last_index = block_sizes[j]; | |
| 359 buf_ptr += last_index >> 3; | |
| 725 | 360 mb->pos = 0; |
| 361 mb->partial_bit_count = 0; | |
| 723 | 362 |
| 725 | 363 dv_decode_ac(s, mb, block, last_index); |
| 723 | 364 |
| 365 /* write the remaining bits in a new buffer only if the | |
| 366 block is finished */ | |
| 367 bits_left = last_index - s->gb.index; | |
| 725 | 368 if (mb->eob_reached) { |
| 369 mb->partial_bit_count = 0; | |
| 370 mb_bit_count += bits_left; | |
| 723 | 371 bit_copy(&pb, &s->gb, bits_left); |
| 372 } else { | |
| 373 /* should be < 16 bits otherwise a codeword could have | |
| 374 been parsed */ | |
| 725 | 375 mb->partial_bit_count = bits_left; |
| 376 mb->partial_bit_buffer = get_bits(&s->gb, bits_left); | |
| 723 | 377 } |
| 378 block += 64; | |
| 725 | 379 mb++; |
| 723 | 380 } |
| 381 | |
| 382 flush_put_bits(&pb); | |
| 383 | |
| 384 /* pass 2 : we can do it just after */ | |
| 385 #ifdef VLC_DEBUG | |
| 725 | 386 printf("***pass 2 size=%d\n", mb_bit_count); |
| 723 | 387 #endif |
| 388 block = block1; | |
| 725 | 389 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
|
390 init_get_bits(&s->gb, mb_bit_buffer, 80*8); |
| 723 | 391 for(j = 0;j < 6; j++) { |
| 725 | 392 if (!mb->eob_reached && s->gb.index < mb_bit_count) { |
| 393 dv_decode_ac(s, mb, block, mb_bit_count); | |
| 723 | 394 /* if still not finished, no need to parse other blocks */ |
| 725 | 395 if (!mb->eob_reached) { |
| 723 | 396 /* we could not parse the current AC coefficient, |
| 397 so we add the remaining bytes */ | |
| 725 | 398 bits_left = mb_bit_count - s->gb.index; |
| 723 | 399 if (bits_left > 0) { |
| 725 | 400 mb->partial_bit_count += bits_left; |
| 401 mb->partial_bit_buffer = | |
| 402 (mb->partial_bit_buffer << bits_left) | | |
| 723 | 403 get_bits(&s->gb, bits_left); |
| 404 } | |
| 405 goto next_mb; | |
| 406 } | |
| 407 } | |
| 408 block += 64; | |
| 725 | 409 mb++; |
| 723 | 410 } |
| 411 /* all blocks are finished, so the extra bytes can be used at | |
| 412 the video segment level */ | |
| 725 | 413 bits_left = mb_bit_count - s->gb.index; |
| 723 | 414 vs_bit_count += bits_left; |
| 415 bit_copy(&vs_pb, &s->gb, bits_left); | |
| 416 next_mb: | |
| 725 | 417 mb1 += 6; |
| 723 | 418 block1 += 6 * 64; |
| 419 } | |
| 420 | |
| 421 /* we need a pass other the whole video segment */ | |
| 422 flush_put_bits(&vs_pb); | |
| 423 | |
| 424 #ifdef VLC_DEBUG | |
| 425 printf("***pass 3 size=%d\n", vs_bit_count); | |
| 426 #endif | |
| 427 block = &s->block[0][0]; | |
| 428 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
|
429 init_get_bits(&s->gb, vs_bit_buffer, 5 * 80*8); |
| 723 | 430 for(mb_index = 0; mb_index < 5; mb_index++) { |
| 431 for(j = 0;j < 6; j++) { | |
| 725 | 432 if (!mb->eob_reached) { |
| 723 | 433 #ifdef VLC_DEBUG |
| 434 printf("start %d:%d\n", mb_index, j); | |
| 435 #endif | |
| 725 | 436 dv_decode_ac(s, mb, block, vs_bit_count); |
| 723 | 437 } |
| 438 block += 64; | |
| 725 | 439 mb++; |
| 723 | 440 } |
| 441 } | |
| 442 | |
| 443 /* compute idct and place blocks */ | |
| 444 block = &s->block[0][0]; | |
| 445 mb = mb_data; | |
| 446 for(mb_index = 0; mb_index < 5; mb_index++) { | |
| 447 v = *mb_pos_ptr++; | |
| 448 mb_x = v & 0xff; | |
| 449 mb_y = v >> 8; | |
| 450 y_ptr = s->current_picture[0] + (mb_y * s->linesize[0] * 8) + (mb_x * 8); | |
| 451 if (s->sampling_411) | |
| 452 c_offset = (mb_y * s->linesize[1] * 8) + ((mb_x >> 2) * 8); | |
| 453 else | |
| 454 c_offset = ((mb_y >> 1) * s->linesize[1] * 8) + ((mb_x >> 1) * 8); | |
| 455 for(j = 0;j < 6; j++) { | |
| 725 | 456 idct_put = s->idct_put[mb->dct_mode]; |
| 723 | 457 if (j < 4) { |
| 737 | 458 if (s->sampling_411 && mb_x < (704 / 8)) { |
| 459 /* NOTE: at end of line, the macroblock is handled as 420 */ | |
| 723 | 460 idct_put(y_ptr + (j * 8), s->linesize[0], block); |
| 461 } else { | |
| 462 idct_put(y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->linesize[0]), | |
| 463 s->linesize[0], block); | |
| 464 } | |
| 465 } else { | |
| 737 | 466 if (s->sampling_411 && mb_x >= (704 / 8)) { |
| 467 uint8_t pixels[64], *c_ptr, *c_ptr1, *ptr; | |
| 468 int y, linesize; | |
| 469 /* NOTE: at end of line, the macroblock is handled as 420 */ | |
| 470 idct_put(pixels, 8, block); | |
| 471 linesize = s->linesize[6 - j]; | |
| 472 c_ptr = s->current_picture[6 - j] + c_offset; | |
| 473 ptr = pixels; | |
| 474 for(y = 0;y < 8; y++) { | |
| 475 /* convert to 411P */ | |
| 476 c_ptr1 = c_ptr + linesize; | |
| 477 c_ptr1[0] = c_ptr[0] = (ptr[0] + ptr[1]) >> 1; | |
| 478 c_ptr1[1] = c_ptr[1] = (ptr[2] + ptr[3]) >> 1; | |
| 479 c_ptr1[2] = c_ptr[2] = (ptr[4] + ptr[5]) >> 1; | |
| 480 c_ptr1[3] = c_ptr[3] = (ptr[6] + ptr[7]) >> 1; | |
| 481 c_ptr += linesize * 2; | |
| 482 ptr += 8; | |
| 483 } | |
| 484 } else { | |
| 485 /* don't ask me why they inverted Cb and Cr ! */ | |
| 486 idct_put(s->current_picture[6 - j] + c_offset, | |
| 487 s->linesize[6 - j], block); | |
| 488 } | |
| 723 | 489 } |
| 490 block += 64; | |
| 725 | 491 mb++; |
| 723 | 492 } |
| 493 } | |
| 494 } | |
| 495 | |
| 496 | |
| 497 /* NOTE: exactly one frame must be given (120000 bytes for NTSC, | |
| 498 144000 bytes for PAL) */ | |
| 499 static int dvvideo_decode_frame(AVCodecContext *avctx, | |
| 500 void *data, int *data_size, | |
| 1064 | 501 uint8_t *buf, int buf_size) |
| 723 | 502 { |
| 503 DVVideoDecodeContext *s = avctx->priv_data; | |
| 862 | 504 int sct, dsf, apt, ds, nb_dif_segs, vs, width, height, i, packet_size; |
| 1064 | 505 uint8_t *buf_ptr; |
| 506 const uint16_t *mb_pos_ptr; | |
| 723 | 507 |
| 508 /* 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
|
509 init_get_bits(&s->gb, buf, buf_size*8); |
| 723 | 510 sct = get_bits(&s->gb, 3); |
| 511 if (sct != 0) | |
| 512 return -1; | |
| 513 skip_bits(&s->gb, 5); | |
| 514 get_bits(&s->gb, 4); /* dsn (sequence number */ | |
| 515 get_bits(&s->gb, 1); /* fsc (channel number) */ | |
| 516 skip_bits(&s->gb, 3); | |
| 517 get_bits(&s->gb, 8); /* dbn (diff block number 0-134) */ | |
| 518 | |
| 519 dsf = get_bits(&s->gb, 1); /* 0 = NTSC 1 = PAL */ | |
| 520 if (get_bits(&s->gb, 1) != 0) | |
| 521 return -1; | |
| 522 skip_bits(&s->gb, 11); | |
| 523 apt = get_bits(&s->gb, 3); /* apt */ | |
| 524 | |
| 525 get_bits(&s->gb, 1); /* tf1 */ | |
| 526 skip_bits(&s->gb, 4); | |
| 527 get_bits(&s->gb, 3); /* ap1 */ | |
| 528 | |
| 529 get_bits(&s->gb, 1); /* tf2 */ | |
| 530 skip_bits(&s->gb, 4); | |
| 531 get_bits(&s->gb, 3); /* ap2 */ | |
| 532 | |
| 533 get_bits(&s->gb, 1); /* tf3 */ | |
| 534 skip_bits(&s->gb, 4); | |
| 535 get_bits(&s->gb, 3); /* ap3 */ | |
| 536 | |
| 537 /* init size */ | |
| 538 width = 720; | |
| 539 if (dsf) { | |
|
1126
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
540 avctx->frame_rate = 25; |
| 1221 | 541 avctx->frame_rate_base = 1; |
| 738 | 542 packet_size = PAL_FRAME_SIZE; |
| 723 | 543 height = 576; |
| 544 nb_dif_segs = 12; | |
| 545 } else { | |
| 1221 | 546 avctx->frame_rate = 30000; |
| 547 avctx->frame_rate_base = 1001; | |
| 738 | 548 packet_size = NTSC_FRAME_SIZE; |
| 723 | 549 height = 480; |
| 550 nb_dif_segs = 10; | |
| 551 } | |
| 738 | 552 /* NOTE: we only accept several full frames */ |
| 553 if (buf_size < packet_size) | |
| 554 return -1; | |
| 555 | |
|
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
|
556 /* 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
|
557 * 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
|
558 * 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
|
559 */ |
|
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 s->sampling_411 = !dsf || apt; |
|
848
c075abb7fb3c
10l - set pixelfmt (colorspace) _before_ calling get_buffer() callback
arpi_esp
parents:
835
diff
changeset
|
561 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
|
562 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
|
563 avctx->pix_fmt = PIX_FMT_YUV411P; |
|
c075abb7fb3c
10l - set pixelfmt (colorspace) _before_ calling get_buffer() callback
arpi_esp
parents:
835
diff
changeset
|
564 } else { |
| 733 | 565 mb_pos_ptr = dv_place_420; |
|
848
c075abb7fb3c
10l - set pixelfmt (colorspace) _before_ calling get_buffer() callback
arpi_esp
parents:
835
diff
changeset
|
566 avctx->pix_fmt = PIX_FMT_YUV420P; |
|
c075abb7fb3c
10l - set pixelfmt (colorspace) _before_ calling get_buffer() callback
arpi_esp
parents:
835
diff
changeset
|
567 } |
|
c075abb7fb3c
10l - set pixelfmt (colorspace) _before_ calling get_buffer() callback
arpi_esp
parents:
835
diff
changeset
|
568 |
|
c075abb7fb3c
10l - set pixelfmt (colorspace) _before_ calling get_buffer() callback
arpi_esp
parents:
835
diff
changeset
|
569 avctx->width = width; |
|
c075abb7fb3c
10l - set pixelfmt (colorspace) _before_ calling get_buffer() callback
arpi_esp
parents:
835
diff
changeset
|
570 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
|
571 |
|
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
|
572 /* 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
|
573 * 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
|
574 * 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
|
575 */ |
|
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 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
|
577 ((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
|
578 (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
|
579 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
|
580 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
|
581 avctx->aspect_ratio = 4.0 / 3.0; |
| 733 | 582 |
| 1228 | 583 if(s->picture.data[0]) |
| 584 avctx->release_buffer(avctx, &s->picture); | |
| 585 | |
| 903 | 586 s->picture.reference= 0; |
| 587 if(avctx->get_buffer(avctx, &s->picture) < 0) { | |
| 588 fprintf(stderr, "get_buffer() failed\n"); | |
| 589 return -1; | |
| 835 | 590 } |
| 591 | |
| 903 | 592 for(i=0;i<3;i++) { |
| 593 s->current_picture[i] = s->picture.data[i]; | |
| 594 s->linesize[i] = s->picture.linesize[i]; | |
| 595 if (!s->current_picture[i]) | |
| 596 return -1; | |
| 723 | 597 } |
| 903 | 598 s->width = width; |
| 599 s->height = height; | |
| 723 | 600 |
| 601 /* for each DIF segment */ | |
| 602 buf_ptr = buf; | |
| 603 for (ds = 0; ds < nb_dif_segs; ds++) { | |
| 604 buf_ptr += 6 * 80; /* skip DIF segment header */ | |
| 605 | |
| 606 for(vs = 0; vs < 27; vs++) { | |
| 607 if ((vs % 3) == 0) { | |
| 608 /* skip audio block */ | |
| 609 buf_ptr += 80; | |
| 610 } | |
| 611 dv_decode_video_segment(s, buf_ptr, mb_pos_ptr); | |
| 612 buf_ptr += 5 * 80; | |
| 613 mb_pos_ptr += 5; | |
| 614 } | |
| 615 } | |
| 616 | |
|
734
2d6b3e3d6c6f
10l - MMX/FPU state was not restored, causing nonsense fpu behaviour in caller (mplayer)
arpi_esp
parents:
733
diff
changeset
|
617 emms_c(); |
|
2d6b3e3d6c6f
10l - MMX/FPU state was not restored, causing nonsense fpu behaviour in caller (mplayer)
arpi_esp
parents:
733
diff
changeset
|
618 |
| 723 | 619 /* return image */ |
| 925 | 620 *data_size = sizeof(AVFrame); |
| 621 *(AVFrame*)data= s->picture; | |
| 903 | 622 |
| 738 | 623 return packet_size; |
| 723 | 624 } |
| 625 | |
| 626 static int dvvideo_decode_end(AVCodecContext *avctx) | |
| 627 { | |
| 628 DVVideoDecodeContext *s = avctx->priv_data; | |
| 1214 | 629 |
| 630 avcodec_default_free_buffers(avctx); | |
| 723 | 631 |
| 632 return 0; | |
| 633 } | |
| 634 | |
| 635 AVCodec dvvideo_decoder = { | |
| 636 "dvvideo", | |
| 637 CODEC_TYPE_VIDEO, | |
| 638 CODEC_ID_DVVIDEO, | |
| 639 sizeof(DVVideoDecodeContext), | |
| 640 dvvideo_decode_init, | |
| 641 NULL, | |
| 642 dvvideo_decode_end, | |
| 643 dvvideo_decode_frame, | |
| 835 | 644 CODEC_CAP_DR1, |
| 723 | 645 NULL |
| 646 }; | |
| 647 | |
| 648 typedef struct DVAudioDecodeContext { | |
| 649 AVCodecContext *avctx; | |
| 650 GetBitContext gb; | |
| 651 } DVAudioDecodeContext; | |
| 652 | |
| 653 static int dvaudio_decode_init(AVCodecContext *avctx) | |
| 654 { | |
| 655 // DVAudioDecodeContext *s = avctx->priv_data; | |
| 656 return 0; | |
| 657 } | |
| 658 | |
| 1064 | 659 static uint16_t dv_audio_12to16(uint16_t sample) |
| 1036 | 660 { |
| 1064 | 661 uint16_t shift, result; |
| 1036 | 662 |
| 663 sample = (sample < 0x800) ? sample : sample | 0xf000; | |
| 664 shift = (sample & 0xf00) >> 8; | |
| 665 | |
| 666 if (shift < 0x2 || shift > 0xd) { | |
| 667 result = sample; | |
| 668 } else if (shift < 0x8) { | |
| 669 shift--; | |
| 670 result = (sample - (256 * shift)) << shift; | |
| 671 } else { | |
| 672 shift = 0xe - shift; | |
| 673 result = ((sample + ((256 * shift) + 1)) << shift) - 1; | |
| 674 } | |
| 675 | |
| 676 return result; | |
| 677 } | |
| 678 | |
| 723 | 679 /* NOTE: exactly one frame must be given (120000 bytes for NTSC, |
| 1036 | 680 144000 bytes for PAL) |
| 681 | |
| 682 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
|
683 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
|
684 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
|
685 to deal with them. |
| 1036 | 686 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
|
687 3. Audio is always returned as 16bit linear samples: 12bit |
| 1036 | 688 nonlinear samples are converted into 16bit linear ones. |
| 689 */ | |
| 723 | 690 static int dvaudio_decode_frame(AVCodecContext *avctx, |
| 691 void *data, int *data_size, | |
| 1064 | 692 uint8_t *buf, int buf_size) |
| 723 | 693 { |
| 1036 | 694 DVVideoDecodeContext *s = avctx->priv_data; |
| 1064 | 695 const uint16_t (*unshuffle)[9]; |
| 1036 | 696 int smpls, freq, quant, sys, stride, difseg, ad, dp, nb_dif_segs, i; |
| 1064 | 697 uint16_t lc, rc; |
| 698 uint8_t *buf_ptr; | |
| 1036 | 699 |
| 700 /* 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
|
701 init_get_bits(&s->gb, &buf[AAUX_AS_OFFSET], 5*8); |
| 1036 | 702 i = get_bits(&s->gb, 8); |
| 703 if (i != 0x50) { /* No audio ? */ | |
| 704 *data_size = 0; | |
| 705 return buf_size; | |
| 706 } | |
| 707 | |
| 708 get_bits(&s->gb, 1); /* 0 - locked audio, 1 - unlocked audio */ | |
| 709 skip_bits(&s->gb, 1); | |
| 710 smpls = get_bits(&s->gb, 6); /* samples in this frame - min. samples */ | |
| 711 | |
| 712 skip_bits(&s->gb, 8); | |
| 713 | |
| 714 skip_bits(&s->gb, 2); | |
| 715 sys = get_bits(&s->gb, 1); /* 0 - 60 fields, 1 = 50 fields */ | |
| 716 skip_bits(&s->gb, 5); | |
| 717 | |
| 718 get_bits(&s->gb, 1); /* 0 - emphasis on, 1 - emphasis off */ | |
| 719 get_bits(&s->gb, 1); /* 0 - reserved, 1 - emphasis time constant 50/15us */ | |
| 720 freq = get_bits(&s->gb, 3); /* 0 - 48KHz, 1 - 44,1kHz, 2 - 32 kHz */ | |
| 721 quant = get_bits(&s->gb, 3); /* 0 - 16bit linear, 1 - 12bit nonlinear */ | |
| 722 | |
| 723 if (quant > 1) | |
| 724 return -1; /* Unsupported quantization */ | |
| 725 | |
| 726 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
|
727 avctx->channels = 2; |
| 1221 | 728 avctx->bit_rate = avctx->channels * avctx->sample_rate * 16; |
| 1036 | 729 // What about: |
| 730 // avctx->frame_size = | |
| 731 | |
| 732 *data_size = (dv_audio_min_samples[sys][freq] + smpls) * | |
| 733 avctx->channels * 2; | |
| 734 | |
| 735 if (sys) { | |
| 736 nb_dif_segs = 12; | |
| 737 stride = 108; | |
| 738 unshuffle = dv_place_audio50; | |
| 739 } else { | |
| 740 nb_dif_segs = 10; | |
| 741 stride = 90; | |
| 742 unshuffle = dv_place_audio60; | |
| 743 } | |
| 744 | |
| 745 /* for each DIF segment */ | |
| 746 buf_ptr = buf; | |
| 747 for (difseg = 0; difseg < nb_dif_segs; difseg++) { | |
| 748 buf_ptr += 6 * 80; /* skip DIF segment header */ | |
| 749 for (ad = 0; ad < 9; ad++) { | |
| 750 | |
| 751 for (dp = 8; dp < 80; dp+=2) { | |
| 752 if (quant == 0) { /* 16bit quantization */ | |
| 753 i = unshuffle[difseg][ad] + (dp - 8)/2 * stride; | |
| 754 ((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
|
755 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
|
756 ((short *)data)[i] = 0; |
| 1036 | 757 } else { /* 12bit quantization */ |
| 758 if (difseg >= nb_dif_segs/2) | |
| 759 goto out; /* We're not doing 4ch at this time */ | |
| 760 | |
| 1064 | 761 lc = ((uint16_t)buf_ptr[dp] << 4) | |
| 762 ((uint16_t)buf_ptr[dp+2] >> 4); | |
| 763 rc = ((uint16_t)buf_ptr[dp+1] << 4) | | |
| 764 ((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
|
765 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
|
766 rc = (rc == 0x800 ? 0 : dv_audio_12to16(rc)); |
| 1036 | 767 |
| 768 i = unshuffle[difseg][ad] + (dp - 8)/3 * stride; | |
| 769 ((short *)data)[i] = lc; | |
| 770 i = unshuffle[difseg+nb_dif_segs/2][ad] + (dp - 8)/3 * stride; | |
| 771 ((short *)data)[i] = rc; | |
| 772 ++dp; | |
| 773 } | |
| 774 } | |
| 775 | |
| 776 buf_ptr += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */ | |
| 777 } | |
| 778 } | |
| 779 | |
| 780 out: | |
| 723 | 781 return buf_size; |
| 782 } | |
| 783 | |
| 784 static int dvaudio_decode_end(AVCodecContext *avctx) | |
| 785 { | |
| 786 // DVAudioDecodeContext *s = avctx->priv_data; | |
| 787 return 0; | |
| 788 } | |
| 789 | |
| 790 AVCodec dvaudio_decoder = { | |
| 791 "dvaudio", | |
| 792 CODEC_TYPE_AUDIO, | |
| 793 CODEC_ID_DVAUDIO, | |
| 794 sizeof(DVAudioDecodeContext), | |
| 795 dvaudio_decode_init, | |
| 796 NULL, | |
| 797 dvaudio_decode_end, | |
| 798 dvaudio_decode_frame, | |
| 799 0, | |
| 800 NULL | |
| 801 }; |
