Mercurial > libavcodec.hg
annotate dv.c @ 853:eacc2dd8fd9d libavcodec
* using DSPContext - so each codec could use its local (sub)set of CPU extension
| author | kabi |
|---|---|
| date | Mon, 11 Nov 2002 09:40:17 +0000 |
| parents | c075abb7fb3c |
| children | 058194d7ade6 |
| 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; | |
| 35 UINT8 *current_picture[3]; /* picture structure */ | |
| 36 int linesize[3]; | |
| 37 DCTELEM block[5*6][64] __align8; | |
| 38 UINT8 dv_zigzag[2][64]; | |
| 725 | 39 UINT8 idct_permutation[64]; |
| 723 | 40 /* XXX: move it to static storage ? */ |
| 725 | 41 UINT8 dv_shift[2][22][64]; |
| 723 | 42 void (*idct_put[2])(UINT8 *dest, int line_size, DCTELEM *block); |
| 43 } DVVideoDecodeContext; | |
| 44 | |
| 45 #include "dvdata.h" | |
| 46 | |
| 47 static VLC dv_vlc; | |
| 48 /* XXX: also include quantization */ | |
| 49 static RL_VLC_ELEM *dv_rl_vlc[1]; | |
| 50 | |
| 51 static void dv_build_unquantize_tables(DVVideoDecodeContext *s) | |
| 52 { | |
| 725 | 53 int i, q, j; |
| 723 | 54 |
| 55 /* NOTE: max left shift is 6 */ | |
| 725 | 56 for(q = 0; q < 22; q++) { |
| 57 /* 88 unquant */ | |
| 58 for(i = 1; i < 64; i++) { | |
| 59 /* 88 table */ | |
| 60 j = s->idct_permutation[i]; | |
| 61 s->dv_shift[0][q][j] = | |
| 62 dv_quant_shifts[q][dv_88_areas[i]] + 1; | |
| 63 } | |
| 64 | |
| 65 /* 248 unquant */ | |
| 66 for(i = 1; i < 64; i++) { | |
| 67 /* 248 table */ | |
| 68 s->dv_shift[1][q][i] = | |
| 69 dv_quant_shifts[q][dv_248_areas[i]] + 1; | |
| 723 | 70 } |
| 71 } | |
| 72 } | |
| 73 | |
| 74 static int dvvideo_decode_init(AVCodecContext *avctx) | |
| 75 { | |
| 76 DVVideoDecodeContext *s = avctx->priv_data; | |
| 725 | 77 MpegEncContext s2; |
| 723 | 78 static int done; |
| 79 | |
| 80 if (!done) { | |
| 81 int i; | |
| 82 | |
| 83 done = 1; | |
| 84 | |
| 85 /* NOTE: as a trick, we use the fact the no codes are unused | |
| 86 to accelerate the parsing of partial codes */ | |
| 87 init_vlc(&dv_vlc, TEX_VLC_BITS, NB_DV_VLC, | |
| 88 dv_vlc_len, 1, 1, dv_vlc_bits, 2, 2); | |
| 89 | |
| 90 dv_rl_vlc[0] = av_malloc(dv_vlc.table_size * sizeof(RL_VLC_ELEM)); | |
| 91 for(i = 0; i < dv_vlc.table_size; i++){ | |
| 92 int code= dv_vlc.table[i][0]; | |
| 93 int len = dv_vlc.table[i][1]; | |
| 94 int level, run; | |
| 95 | |
| 96 if(len<0){ //more bits needed | |
| 97 run= 0; | |
| 98 level= code; | |
| 99 } else if (code == (NB_DV_VLC - 1)) { | |
| 100 /* EOB */ | |
| 101 run = 0; | |
| 102 level = 256; | |
| 103 } else { | |
| 104 run= dv_vlc_run[code] + 1; | |
| 105 level= dv_vlc_level[code]; | |
| 106 } | |
| 107 dv_rl_vlc[0][i].len = len; | |
| 108 dv_rl_vlc[0][i].level = level; | |
| 109 dv_rl_vlc[0][i].run = run; | |
| 110 } | |
| 111 } | |
| 725 | 112 |
| 113 /* ugly way to get the idct & scantable */ | |
| 114 /* XXX: fix it */ | |
| 115 memset(&s2, 0, sizeof(MpegEncContext)); | |
| 116 s2.avctx = avctx; | |
|
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
848
diff
changeset
|
117 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
|
118 if (DCT_common_init(&s2) < 0) |
| 725 | 119 return -1; |
| 120 | |
| 121 s->idct_put[0] = s2.idct_put; | |
| 122 memcpy(s->idct_permutation, s2.idct_permutation, 64); | |
| 123 memcpy(s->dv_zigzag[0], s2.intra_scantable.permutated, 64); | |
| 124 | |
| 125 /* XXX: use MMX also for idct248 */ | |
| 126 s->idct_put[1] = simple_idct248_put; | |
| 127 memcpy(s->dv_zigzag[1], dv_248_zigzag, 64); | |
| 128 | |
| 723 | 129 /* XXX: do it only for constant case */ |
| 130 dv_build_unquantize_tables(s); | |
| 131 | |
| 132 return 0; | |
| 133 } | |
| 134 | |
| 135 //#define VLC_DEBUG | |
| 136 | |
| 725 | 137 typedef struct BlockInfo { |
| 138 const UINT8 *shift_table; | |
| 139 const UINT8 *scan_table; | |
| 140 UINT8 pos; /* position in block */ | |
| 141 UINT8 eob_reached; /* true if EOB has been reached */ | |
| 142 UINT8 dct_mode; | |
| 143 UINT8 partial_bit_count; | |
| 144 UINT16 partial_bit_buffer; | |
| 145 int shift_offset; | |
| 146 } BlockInfo; | |
| 723 | 147 |
| 148 /* block size in bits */ | |
|
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
|
149 static const UINT16 block_sizes[6] = { |
| 723 | 150 112, 112, 112, 112, 80, 80 |
| 151 }; | |
| 152 | |
| 153 #ifndef ALT_BITSTREAM_READER | |
| 154 #error only works with ALT_BITSTREAM_READER | |
| 155 #endif | |
| 156 | |
| 157 /* decode ac coefs */ | |
| 158 static void dv_decode_ac(DVVideoDecodeContext *s, | |
| 725 | 159 BlockInfo *mb, INT16 *block, int last_index) |
| 723 | 160 { |
| 161 int last_re_index; | |
| 725 | 162 int shift_offset = mb->shift_offset; |
| 163 const UINT8 *scan_table = mb->scan_table; | |
| 164 const UINT8 *shift_table = mb->shift_table; | |
| 165 int pos = mb->pos; | |
| 723 | 166 int level, pos1, sign, run; |
| 167 int partial_bit_count; | |
| 168 | |
| 169 OPEN_READER(re, &s->gb); | |
| 170 | |
| 171 #ifdef VLC_DEBUG | |
| 725 | 172 printf("start\n"); |
| 723 | 173 #endif |
| 174 | |
| 175 /* if we must parse a partial vlc, we do it here */ | |
| 725 | 176 partial_bit_count = mb->partial_bit_count; |
| 723 | 177 if (partial_bit_count > 0) { |
| 178 UINT8 buf[4]; | |
| 179 UINT32 v; | |
| 180 int l, l1; | |
| 181 GetBitContext gb1; | |
| 182 | |
| 183 /* build the dummy bit buffer */ | |
| 184 l = 16 - partial_bit_count; | |
| 185 UPDATE_CACHE(re, &s->gb); | |
| 186 #ifdef VLC_DEBUG | |
| 187 printf("show=%04x\n", SHOW_UBITS(re, &s->gb, 16)); | |
| 188 #endif | |
| 725 | 189 v = (mb->partial_bit_buffer << l) | SHOW_UBITS(re, &s->gb, l); |
| 723 | 190 buf[0] = v >> 8; |
| 191 buf[1] = v; | |
| 192 #ifdef VLC_DEBUG | |
| 193 printf("v=%04x cnt=%d %04x\n", | |
| 725 | 194 v, partial_bit_count, (mb->partial_bit_buffer << l)); |
| 723 | 195 #endif |
| 196 /* try to read the codeword */ | |
| 197 init_get_bits(&gb1, buf, 4); | |
| 198 { | |
| 199 OPEN_READER(re1, &gb1); | |
| 200 UPDATE_CACHE(re1, &gb1); | |
| 201 GET_RL_VLC(level, run, re1, &gb1, dv_rl_vlc[0], | |
| 202 TEX_VLC_BITS, 2); | |
| 203 l = re1_index; | |
| 204 CLOSE_READER(re1, &gb1); | |
| 205 } | |
| 206 #ifdef VLC_DEBUG | |
| 207 printf("****run=%d level=%d size=%d\n", run, level, l); | |
| 208 #endif | |
| 209 /* compute codeword length */ | |
| 210 l1 = (level != 256 && level != 0); | |
| 211 /* if too long, we cannot parse */ | |
| 212 l -= partial_bit_count; | |
| 213 if ((re_index + l + l1) > last_index) | |
| 214 return; | |
| 215 /* skip read bits */ | |
| 216 last_re_index = 0; /* avoid warning */ | |
| 217 re_index += l; | |
| 725 | 218 /* by definition, if we can read the vlc, all partial bits |
| 723 | 219 will be read (otherwise we could have read the vlc before) */ |
| 725 | 220 mb->partial_bit_count = 0; |
| 723 | 221 UPDATE_CACHE(re, &s->gb); |
| 222 goto handle_vlc; | |
| 223 } | |
| 224 | |
| 225 /* get the AC coefficients until last_index is reached */ | |
| 226 for(;;) { | |
| 227 UPDATE_CACHE(re, &s->gb); | |
| 228 #ifdef VLC_DEBUG | |
| 229 printf("%2d: bits=%04x index=%d\n", | |
| 230 pos, SHOW_UBITS(re, &s->gb, 16), re_index); | |
| 231 #endif | |
| 232 last_re_index = re_index; | |
| 233 GET_RL_VLC(level, run, re, &s->gb, dv_rl_vlc[0], | |
| 234 TEX_VLC_BITS, 2); | |
| 235 handle_vlc: | |
| 236 #ifdef VLC_DEBUG | |
| 237 printf("run=%d level=%d\n", run, level); | |
| 238 #endif | |
| 239 if (level == 256) { | |
| 240 if (re_index > last_index) { | |
| 241 cannot_read: | |
| 242 /* put position before read code */ | |
| 243 re_index = last_re_index; | |
| 725 | 244 mb->eob_reached = 0; |
| 723 | 245 break; |
| 246 } | |
| 247 /* EOB */ | |
| 725 | 248 mb->eob_reached = 1; |
| 723 | 249 break; |
| 250 } else if (level != 0) { | |
| 251 if ((re_index + 1) > last_index) | |
| 252 goto cannot_read; | |
| 253 sign = SHOW_SBITS(re, &s->gb, 1); | |
| 254 level = (level ^ sign) - sign; | |
| 255 LAST_SKIP_BITS(re, &s->gb, 1); | |
| 256 pos += run; | |
| 257 /* error */ | |
| 258 if (pos >= 64) { | |
| 259 goto read_error; | |
| 260 } | |
| 261 pos1 = scan_table[pos]; | |
| 725 | 262 level = level << (shift_table[pos1] + shift_offset); |
| 723 | 263 block[pos1] = level; |
| 264 // printf("run=%d level=%d shift=%d\n", run, level, shift_table[pos1]); | |
| 265 } else { | |
| 266 if (re_index > last_index) | |
| 267 goto cannot_read; | |
| 268 /* level is zero: means run without coding. No | |
| 269 sign is coded */ | |
| 270 pos += run; | |
| 271 /* error */ | |
| 272 if (pos >= 64) { | |
| 273 read_error: | |
| 274 #if defined(VLC_DEBUG) || 1 | |
| 275 printf("error pos=%d\n", pos); | |
| 276 #endif | |
| 277 /* for errors, we consider the eob is reached */ | |
| 725 | 278 mb->eob_reached = 1; |
| 723 | 279 break; |
| 280 } | |
| 281 } | |
| 282 } | |
| 283 CLOSE_READER(re, &s->gb); | |
| 725 | 284 mb->pos = pos; |
| 723 | 285 } |
| 286 | |
| 287 static inline void bit_copy(PutBitContext *pb, GetBitContext *gb, int bits_left) | |
| 288 { | |
| 289 while (bits_left >= 16) { | |
| 290 put_bits(pb, 16, get_bits(gb, 16)); | |
| 291 bits_left -= 16; | |
| 292 } | |
| 293 if (bits_left > 0) { | |
| 294 put_bits(pb, bits_left, get_bits(gb, bits_left)); | |
| 295 } | |
| 296 } | |
| 297 | |
| 298 /* mb_x and mb_y are in units of 8 pixels */ | |
| 299 static inline void dv_decode_video_segment(DVVideoDecodeContext *s, | |
| 300 UINT8 *buf_ptr1, | |
| 301 const UINT16 *mb_pos_ptr) | |
| 302 { | |
| 303 int quant, dc, dct_mode, class1, j; | |
| 304 int mb_index, mb_x, mb_y, v, last_index; | |
| 305 DCTELEM *block, *block1; | |
| 306 int c_offset, bits_left; | |
| 307 UINT8 *y_ptr; | |
| 725 | 308 BlockInfo mb_data[5 * 6], *mb, *mb1; |
| 723 | 309 void (*idct_put)(UINT8 *dest, int line_size, DCTELEM *block); |
| 310 UINT8 *buf_ptr; | |
| 311 PutBitContext pb, vs_pb; | |
| 725 | 312 UINT8 mb_bit_buffer[80 + 4]; /* allow some slack */ |
| 313 int mb_bit_count; | |
| 314 UINT8 vs_bit_buffer[5 * 80 + 4]; /* allow some slack */ | |
| 723 | 315 int vs_bit_count; |
| 316 | |
| 317 memset(s->block, 0, sizeof(s->block)); | |
| 318 | |
| 319 /* pass 1 : read DC and AC coefficients in blocks */ | |
| 320 buf_ptr = buf_ptr1; | |
| 321 block1 = &s->block[0][0]; | |
| 725 | 322 mb1 = mb_data; |
| 723 | 323 init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80, NULL, NULL); |
| 324 vs_bit_count = 0; | |
| 325 for(mb_index = 0; mb_index < 5; mb_index++) { | |
| 326 /* skip header */ | |
| 327 quant = buf_ptr[3] & 0x0f; | |
| 328 buf_ptr += 4; | |
| 725 | 329 init_put_bits(&pb, mb_bit_buffer, 80, NULL, NULL); |
| 330 mb_bit_count = 0; | |
| 331 mb = mb1; | |
| 723 | 332 block = block1; |
| 333 for(j = 0;j < 6; j++) { | |
| 334 /* NOTE: size is not important here */ | |
| 335 init_get_bits(&s->gb, buf_ptr, 14); | |
| 336 | |
| 337 /* get the dc */ | |
| 338 dc = get_bits(&s->gb, 9); | |
| 339 dc = (dc << (32 - 9)) >> (32 - 9); | |
| 340 dct_mode = get_bits1(&s->gb); | |
| 725 | 341 mb->dct_mode = dct_mode; |
| 342 mb->scan_table = s->dv_zigzag[dct_mode]; | |
| 723 | 343 class1 = get_bits(&s->gb, 2); |
| 725 | 344 mb->shift_offset = (class1 == 3); |
| 345 mb->shift_table = s->dv_shift[dct_mode] | |
| 723 | 346 [quant + dv_quant_offset[class1]]; |
| 347 dc = dc << 2; | |
| 348 /* convert to unsigned because 128 is not added in the | |
| 349 standard IDCT */ | |
| 350 dc += 1024; | |
| 351 block[0] = dc; | |
| 352 last_index = block_sizes[j]; | |
| 353 buf_ptr += last_index >> 3; | |
| 725 | 354 mb->pos = 0; |
| 355 mb->partial_bit_count = 0; | |
| 723 | 356 |
| 725 | 357 dv_decode_ac(s, mb, block, last_index); |
| 723 | 358 |
| 359 /* write the remaining bits in a new buffer only if the | |
| 360 block is finished */ | |
| 361 bits_left = last_index - s->gb.index; | |
| 725 | 362 if (mb->eob_reached) { |
| 363 mb->partial_bit_count = 0; | |
| 364 mb_bit_count += bits_left; | |
| 723 | 365 bit_copy(&pb, &s->gb, bits_left); |
| 366 } else { | |
| 367 /* should be < 16 bits otherwise a codeword could have | |
| 368 been parsed */ | |
| 725 | 369 mb->partial_bit_count = bits_left; |
| 370 mb->partial_bit_buffer = get_bits(&s->gb, bits_left); | |
| 723 | 371 } |
| 372 block += 64; | |
| 725 | 373 mb++; |
| 723 | 374 } |
| 375 | |
| 376 flush_put_bits(&pb); | |
| 377 | |
| 378 /* pass 2 : we can do it just after */ | |
| 379 #ifdef VLC_DEBUG | |
| 725 | 380 printf("***pass 2 size=%d\n", mb_bit_count); |
| 723 | 381 #endif |
| 382 block = block1; | |
| 725 | 383 mb = mb1; |
| 384 init_get_bits(&s->gb, mb_bit_buffer, 80); | |
| 723 | 385 for(j = 0;j < 6; j++) { |
| 725 | 386 if (!mb->eob_reached && s->gb.index < mb_bit_count) { |
| 387 dv_decode_ac(s, mb, block, mb_bit_count); | |
| 723 | 388 /* if still not finished, no need to parse other blocks */ |
| 725 | 389 if (!mb->eob_reached) { |
| 723 | 390 /* we could not parse the current AC coefficient, |
| 391 so we add the remaining bytes */ | |
| 725 | 392 bits_left = mb_bit_count - s->gb.index; |
| 723 | 393 if (bits_left > 0) { |
| 725 | 394 mb->partial_bit_count += bits_left; |
| 395 mb->partial_bit_buffer = | |
| 396 (mb->partial_bit_buffer << bits_left) | | |
| 723 | 397 get_bits(&s->gb, bits_left); |
| 398 } | |
| 399 goto next_mb; | |
| 400 } | |
| 401 } | |
| 402 block += 64; | |
| 725 | 403 mb++; |
| 723 | 404 } |
| 405 /* all blocks are finished, so the extra bytes can be used at | |
| 406 the video segment level */ | |
| 725 | 407 bits_left = mb_bit_count - s->gb.index; |
| 723 | 408 vs_bit_count += bits_left; |
| 409 bit_copy(&vs_pb, &s->gb, bits_left); | |
| 410 next_mb: | |
| 725 | 411 mb1 += 6; |
| 723 | 412 block1 += 6 * 64; |
| 413 } | |
| 414 | |
| 415 /* we need a pass other the whole video segment */ | |
| 416 flush_put_bits(&vs_pb); | |
| 417 | |
| 418 #ifdef VLC_DEBUG | |
| 419 printf("***pass 3 size=%d\n", vs_bit_count); | |
| 420 #endif | |
| 421 block = &s->block[0][0]; | |
| 422 mb = mb_data; | |
| 423 init_get_bits(&s->gb, vs_bit_buffer, 5 * 80); | |
| 424 for(mb_index = 0; mb_index < 5; mb_index++) { | |
| 425 for(j = 0;j < 6; j++) { | |
| 725 | 426 if (!mb->eob_reached) { |
| 723 | 427 #ifdef VLC_DEBUG |
| 428 printf("start %d:%d\n", mb_index, j); | |
| 429 #endif | |
| 725 | 430 dv_decode_ac(s, mb, block, vs_bit_count); |
| 723 | 431 } |
| 432 block += 64; | |
| 725 | 433 mb++; |
| 723 | 434 } |
| 435 } | |
| 436 | |
| 437 /* compute idct and place blocks */ | |
| 438 block = &s->block[0][0]; | |
| 439 mb = mb_data; | |
| 440 for(mb_index = 0; mb_index < 5; mb_index++) { | |
| 441 v = *mb_pos_ptr++; | |
| 442 mb_x = v & 0xff; | |
| 443 mb_y = v >> 8; | |
| 444 y_ptr = s->current_picture[0] + (mb_y * s->linesize[0] * 8) + (mb_x * 8); | |
| 445 if (s->sampling_411) | |
| 446 c_offset = (mb_y * s->linesize[1] * 8) + ((mb_x >> 2) * 8); | |
| 447 else | |
| 448 c_offset = ((mb_y >> 1) * s->linesize[1] * 8) + ((mb_x >> 1) * 8); | |
| 449 for(j = 0;j < 6; j++) { | |
| 725 | 450 idct_put = s->idct_put[mb->dct_mode]; |
| 723 | 451 if (j < 4) { |
| 737 | 452 if (s->sampling_411 && mb_x < (704 / 8)) { |
| 453 /* NOTE: at end of line, the macroblock is handled as 420 */ | |
| 723 | 454 idct_put(y_ptr + (j * 8), s->linesize[0], block); |
| 455 } else { | |
| 456 idct_put(y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->linesize[0]), | |
| 457 s->linesize[0], block); | |
| 458 } | |
| 459 } else { | |
| 737 | 460 if (s->sampling_411 && mb_x >= (704 / 8)) { |
| 461 uint8_t pixels[64], *c_ptr, *c_ptr1, *ptr; | |
| 462 int y, linesize; | |
| 463 /* NOTE: at end of line, the macroblock is handled as 420 */ | |
| 464 idct_put(pixels, 8, block); | |
| 465 linesize = s->linesize[6 - j]; | |
| 466 c_ptr = s->current_picture[6 - j] + c_offset; | |
| 467 ptr = pixels; | |
| 468 for(y = 0;y < 8; y++) { | |
| 469 /* convert to 411P */ | |
| 470 c_ptr1 = c_ptr + linesize; | |
| 471 c_ptr1[0] = c_ptr[0] = (ptr[0] + ptr[1]) >> 1; | |
| 472 c_ptr1[1] = c_ptr[1] = (ptr[2] + ptr[3]) >> 1; | |
| 473 c_ptr1[2] = c_ptr[2] = (ptr[4] + ptr[5]) >> 1; | |
| 474 c_ptr1[3] = c_ptr[3] = (ptr[6] + ptr[7]) >> 1; | |
| 475 c_ptr += linesize * 2; | |
| 476 ptr += 8; | |
| 477 } | |
| 478 } else { | |
| 479 /* don't ask me why they inverted Cb and Cr ! */ | |
| 480 idct_put(s->current_picture[6 - j] + c_offset, | |
| 481 s->linesize[6 - j], block); | |
| 482 } | |
| 723 | 483 } |
| 484 block += 64; | |
| 725 | 485 mb++; |
| 723 | 486 } |
| 487 } | |
| 488 } | |
| 489 | |
| 490 | |
| 491 /* NOTE: exactly one frame must be given (120000 bytes for NTSC, | |
| 492 144000 bytes for PAL) */ | |
| 493 static int dvvideo_decode_frame(AVCodecContext *avctx, | |
| 494 void *data, int *data_size, | |
| 495 UINT8 *buf, int buf_size) | |
| 496 { | |
| 497 DVVideoDecodeContext *s = avctx->priv_data; | |
| 738 | 498 int sct, dsf, apt, ds, nb_dif_segs, vs, size, width, height, i, packet_size; |
| 723 | 499 UINT8 *buf_ptr; |
| 500 const UINT16 *mb_pos_ptr; | |
| 501 AVPicture *picture; | |
| 502 | |
| 503 /* parse id */ | |
| 504 init_get_bits(&s->gb, buf, buf_size); | |
| 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 | |
| 733 | 549 /* XXX: is it correct to assume that 420 is always used in PAL |
| 550 mode ? */ | |
| 551 s->sampling_411 = !dsf; | |
|
848
c075abb7fb3c
10l - set pixelfmt (colorspace) _before_ calling get_buffer() callback
arpi_esp
parents:
835
diff
changeset
|
552 if (s->sampling_411) { |
| 733 | 553 mb_pos_ptr = dv_place_411; |
|
848
c075abb7fb3c
10l - set pixelfmt (colorspace) _before_ calling get_buffer() callback
arpi_esp
parents:
835
diff
changeset
|
554 avctx->pix_fmt = PIX_FMT_YUV411P; |
|
c075abb7fb3c
10l - set pixelfmt (colorspace) _before_ calling get_buffer() callback
arpi_esp
parents:
835
diff
changeset
|
555 } else { |
| 733 | 556 mb_pos_ptr = dv_place_420; |
|
848
c075abb7fb3c
10l - set pixelfmt (colorspace) _before_ calling get_buffer() callback
arpi_esp
parents:
835
diff
changeset
|
557 avctx->pix_fmt = PIX_FMT_YUV420P; |
|
c075abb7fb3c
10l - set pixelfmt (colorspace) _before_ calling get_buffer() callback
arpi_esp
parents:
835
diff
changeset
|
558 } |
|
c075abb7fb3c
10l - set pixelfmt (colorspace) _before_ calling get_buffer() callback
arpi_esp
parents:
835
diff
changeset
|
559 |
|
c075abb7fb3c
10l - set pixelfmt (colorspace) _before_ calling get_buffer() callback
arpi_esp
parents:
835
diff
changeset
|
560 avctx->width = width; |
|
c075abb7fb3c
10l - set pixelfmt (colorspace) _before_ calling get_buffer() callback
arpi_esp
parents:
835
diff
changeset
|
561 avctx->height = height; |
| 733 | 562 |
| 835 | 563 if (avctx->flags & CODEC_FLAG_DR1 && avctx->get_buffer_callback) |
| 564 { | |
| 565 s->width = -1; | |
| 566 avctx->dr_buffer[0] = avctx->dr_buffer[1] = avctx->dr_buffer[2] = 0; | |
| 567 if(avctx->get_buffer_callback(avctx, width, height, I_TYPE) < 0){ | |
| 568 fprintf(stderr, "get_buffer() failed\n"); | |
| 569 return -1; | |
| 570 } | |
| 571 } | |
| 572 | |
| 723 | 573 /* (re)alloc picture if needed */ |
| 574 if (s->width != width || s->height != height) { | |
| 835 | 575 if (!(avctx->flags & CODEC_FLAG_DR1)) |
| 576 for(i=0;i<3;i++) { | |
| 577 if (avctx->dr_buffer[i] != s->current_picture[i]) | |
| 578 av_freep(&s->current_picture[i]); | |
| 579 avctx->dr_buffer[i] = 0; | |
| 580 } | |
| 581 | |
| 723 | 582 for(i=0;i<3;i++) { |
| 835 | 583 if (avctx->dr_buffer[i]) { |
| 584 s->current_picture[i] = avctx->dr_buffer[i]; | |
| 585 s->linesize[i] = (i == 0) ? avctx->dr_stride : avctx->dr_uvstride; | |
| 586 } else { | |
| 587 size = width * height; | |
| 588 s->linesize[i] = width; | |
| 589 if (i >= 1) { | |
| 590 size >>= 2; | |
| 591 s->linesize[i] >>= s->sampling_411 ? 2 : 1; | |
| 592 } | |
| 593 s->current_picture[i] = av_malloc(size); | |
| 594 } | |
| 723 | 595 if (!s->current_picture[i]) |
| 596 return -1; | |
| 597 } | |
| 598 s->width = width; | |
| 599 s->height = height; | |
| 600 } | |
| 601 | |
| 602 /* for each DIF segment */ | |
| 603 buf_ptr = buf; | |
| 604 for (ds = 0; ds < nb_dif_segs; ds++) { | |
| 605 buf_ptr += 6 * 80; /* skip DIF segment header */ | |
| 606 | |
| 607 for(vs = 0; vs < 27; vs++) { | |
| 608 if ((vs % 3) == 0) { | |
| 609 /* skip audio block */ | |
| 610 buf_ptr += 80; | |
| 611 } | |
| 612 dv_decode_video_segment(s, buf_ptr, mb_pos_ptr); | |
| 613 buf_ptr += 5 * 80; | |
| 614 mb_pos_ptr += 5; | |
| 615 } | |
| 616 } | |
| 617 | |
|
734
2d6b3e3d6c6f
10l - MMX/FPU state was not restored, causing nonsense fpu behaviour in caller (mplayer)
arpi_esp
parents:
733
diff
changeset
|
618 emms_c(); |
|
2d6b3e3d6c6f
10l - MMX/FPU state was not restored, causing nonsense fpu behaviour in caller (mplayer)
arpi_esp
parents:
733
diff
changeset
|
619 |
| 723 | 620 /* return image */ |
| 621 *data_size = sizeof(AVPicture); | |
| 622 picture = data; | |
| 623 for(i=0;i<3;i++) { | |
| 624 picture->data[i] = s->current_picture[i]; | |
| 625 picture->linesize[i] = s->linesize[i]; | |
| 626 } | |
| 738 | 627 return packet_size; |
| 723 | 628 } |
| 629 | |
| 630 static int dvvideo_decode_end(AVCodecContext *avctx) | |
| 631 { | |
| 632 DVVideoDecodeContext *s = avctx->priv_data; | |
| 633 int i; | |
| 634 | |
| 635 for(i=0;i<3;i++) | |
| 835 | 636 if (avctx->dr_buffer[i] != s->current_picture[i]) |
| 723 | 637 av_freep(&s->current_picture[i]); |
| 638 return 0; | |
| 639 } | |
| 640 | |
| 641 AVCodec dvvideo_decoder = { | |
| 642 "dvvideo", | |
| 643 CODEC_TYPE_VIDEO, | |
| 644 CODEC_ID_DVVIDEO, | |
| 645 sizeof(DVVideoDecodeContext), | |
| 646 dvvideo_decode_init, | |
| 647 NULL, | |
| 648 dvvideo_decode_end, | |
| 649 dvvideo_decode_frame, | |
| 835 | 650 CODEC_CAP_DR1, |
| 723 | 651 NULL |
| 652 }; | |
| 653 | |
| 654 typedef struct DVAudioDecodeContext { | |
| 655 AVCodecContext *avctx; | |
| 656 GetBitContext gb; | |
| 657 | |
| 658 } DVAudioDecodeContext; | |
| 659 | |
| 660 static int dvaudio_decode_init(AVCodecContext *avctx) | |
| 661 { | |
| 662 // DVAudioDecodeContext *s = avctx->priv_data; | |
| 663 return 0; | |
| 664 } | |
| 665 | |
| 666 /* NOTE: exactly one frame must be given (120000 bytes for NTSC, | |
| 667 144000 bytes for PAL) */ | |
| 668 static int dvaudio_decode_frame(AVCodecContext *avctx, | |
| 669 void *data, int *data_size, | |
| 670 UINT8 *buf, int buf_size) | |
| 671 { | |
| 672 // DVAudioDecodeContext *s = avctx->priv_data; | |
| 673 return buf_size; | |
| 674 } | |
| 675 | |
| 676 static int dvaudio_decode_end(AVCodecContext *avctx) | |
| 677 { | |
| 678 // DVAudioDecodeContext *s = avctx->priv_data; | |
| 679 return 0; | |
| 680 } | |
| 681 | |
| 682 AVCodec dvaudio_decoder = { | |
| 683 "dvaudio", | |
| 684 CODEC_TYPE_AUDIO, | |
| 685 CODEC_ID_DVAUDIO, | |
| 686 sizeof(DVAudioDecodeContext), | |
| 687 dvaudio_decode_init, | |
| 688 NULL, | |
| 689 dvaudio_decode_end, | |
| 690 dvaudio_decode_frame, | |
| 691 0, | |
| 692 NULL | |
| 693 }; |
