Mercurial > libavcodec.hg
annotate dv.c @ 1489:337d13aee605 libavcodec
* DV handling was streamlined for both muxing/demuxing and
decoding. All muxing/demuxing functionality is now available
in libavformat/dv.[ch].
* dv1394.c and avidec.c were hooked up with general DV demuxer.
* DVAUDIO is dead! Long live pcm_s16le!
* DV audio is now always recognized -- which means we can
now hear all those ducks quaking in pond.dv.
| author | romansh |
|---|---|
| date | Mon, 29 Sep 2003 17:54:07 +0000 |
| parents | 8edad1e372d1 |
| children | ad7e62df9962 |
| 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" | |
|
1489
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
28 #include "dvdata.h" |
| 723 | 29 |
| 30 typedef struct DVVideoDecodeContext { | |
|
1489
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
31 const DVprofile* sys; |
| 723 | 32 GetBitContext gb; |
| 925 | 33 AVFrame picture; |
| 723 | 34 DCTELEM block[5*6][64] __align8; |
|
1489
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
35 |
|
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
36 /* FIXME: the following is extracted from DSP */ |
| 1064 | 37 uint8_t dv_zigzag[2][64]; |
| 38 uint8_t idct_permutation[64]; | |
|
1489
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
39 void (*get_pixels)(DCTELEM *block, const uint8_t *pixels, int line_size); |
|
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
40 void (*fdct)(DCTELEM *block); |
|
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
41 |
| 723 | 42 /* XXX: move it to static storage ? */ |
| 1064 | 43 uint8_t dv_shift[2][22][64]; |
| 44 void (*idct_put[2])(uint8_t *dest, int line_size, DCTELEM *block); | |
| 723 | 45 } DVVideoDecodeContext; |
| 46 | |
|
1489
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
47 #define TEX_VLC_BITS 9 |
| 723 | 48 /* XXX: also include quantization */ |
| 49 static RL_VLC_ELEM *dv_rl_vlc[1]; | |
|
1489
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
50 static VLC_TYPE dv_vlc_codes[15][23]; |
| 723 | 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; | |
|
1489
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
83 VLC dv_vlc; |
| 723 | 84 |
| 85 done = 1; | |
| 86 | |
| 87 /* NOTE: as a trick, we use the fact the no codes are unused | |
| 88 to accelerate the parsing of partial codes */ | |
| 89 init_vlc(&dv_vlc, TEX_VLC_BITS, NB_DV_VLC, | |
| 90 dv_vlc_len, 1, 1, dv_vlc_bits, 2, 2); | |
| 91 | |
| 92 dv_rl_vlc[0] = av_malloc(dv_vlc.table_size * sizeof(RL_VLC_ELEM)); | |
| 93 for(i = 0; i < dv_vlc.table_size; i++){ | |
| 94 int code= dv_vlc.table[i][0]; | |
| 95 int len = dv_vlc.table[i][1]; | |
| 96 int level, run; | |
| 97 | |
| 98 if(len<0){ //more bits needed | |
| 99 run= 0; | |
| 100 level= code; | |
| 101 } else if (code == (NB_DV_VLC - 1)) { | |
| 102 /* EOB */ | |
| 103 run = 0; | |
| 104 level = 256; | |
| 105 } else { | |
| 106 run= dv_vlc_run[code] + 1; | |
| 107 level= dv_vlc_level[code]; | |
| 108 } | |
| 109 dv_rl_vlc[0][i].len = len; | |
| 110 dv_rl_vlc[0][i].level = level; | |
| 111 dv_rl_vlc[0][i].run = run; | |
| 112 } | |
|
1489
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
113 |
|
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
114 memset(dv_vlc_codes, 0xff, sizeof(dv_vlc_codes)); |
|
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
115 for (i = 0; i < NB_DV_VLC - 1; i++) { |
|
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
116 if (dv_vlc_run[i] < 15 && dv_vlc_level[i] < 23 && dv_vlc_len[i] < 15) |
|
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
117 dv_vlc_codes[dv_vlc_run[i]][dv_vlc_level[i]] = i; |
|
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
118 } |
| 723 | 119 } |
| 725 | 120 |
| 121 /* ugly way to get the idct & scantable */ | |
| 122 /* XXX: fix it */ | |
| 123 memset(&s2, 0, sizeof(MpegEncContext)); | |
| 124 s2.avctx = avctx; | |
| 1092 | 125 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
|
126 if (DCT_common_init(&s2) < 0) |
| 725 | 127 return -1; |
| 128 | |
|
1489
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
129 s->get_pixels = s2.dsp.get_pixels; |
|
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
130 s->fdct = s2.dsp.fdct; |
|
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
131 |
| 1092 | 132 s->idct_put[0] = s2.dsp.idct_put; |
| 133 memcpy(s->idct_permutation, s2.dsp.idct_permutation, 64); | |
| 725 | 134 memcpy(s->dv_zigzag[0], s2.intra_scantable.permutated, 64); |
| 135 | |
| 136 /* XXX: use MMX also for idct248 */ | |
| 137 s->idct_put[1] = simple_idct248_put; | |
| 138 memcpy(s->dv_zigzag[1], dv_248_zigzag, 64); | |
| 139 | |
| 723 | 140 /* XXX: do it only for constant case */ |
| 141 dv_build_unquantize_tables(s); | |
|
1489
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
142 |
| 723 | 143 return 0; |
| 144 } | |
| 145 | |
|
1489
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
146 // #define VLC_DEBUG |
| 723 | 147 |
| 725 | 148 typedef struct BlockInfo { |
| 1064 | 149 const uint8_t *shift_table; |
| 150 const uint8_t *scan_table; | |
| 151 uint8_t pos; /* position in block */ | |
| 152 uint8_t eob_reached; /* true if EOB has been reached */ | |
| 153 uint8_t dct_mode; | |
| 154 uint8_t partial_bit_count; | |
| 155 uint16_t partial_bit_buffer; | |
| 725 | 156 int shift_offset; |
| 157 } BlockInfo; | |
| 723 | 158 |
| 159 /* block size in bits */ | |
| 1064 | 160 static const uint16_t block_sizes[6] = { |
| 723 | 161 112, 112, 112, 112, 80, 80 |
| 162 }; | |
| 163 | |
| 164 #ifndef ALT_BITSTREAM_READER | |
|
1256
b8e1c17b8d7d
some libmpeg2 style bitstream reader fixes (no dv doesnt yet work with it)
michaelni
parents:
1228
diff
changeset
|
165 #warning only works with ALT_BITSTREAM_READER |
| 723 | 166 #endif |
| 167 | |
| 168 /* decode ac coefs */ | |
| 169 static void dv_decode_ac(DVVideoDecodeContext *s, | |
| 1008 | 170 BlockInfo *mb, DCTELEM *block, int last_index) |
| 723 | 171 { |
| 172 int last_re_index; | |
| 725 | 173 int shift_offset = mb->shift_offset; |
| 1064 | 174 const uint8_t *scan_table = mb->scan_table; |
| 175 const uint8_t *shift_table = mb->shift_table; | |
| 725 | 176 int pos = mb->pos; |
| 723 | 177 int level, pos1, sign, run; |
| 178 int partial_bit_count; | |
|
1256
b8e1c17b8d7d
some libmpeg2 style bitstream reader fixes (no dv doesnt yet work with it)
michaelni
parents:
1228
diff
changeset
|
179 #ifndef ALT_BITSTREAM_READER //FIXME |
|
b8e1c17b8d7d
some libmpeg2 style bitstream reader fixes (no dv doesnt yet work with it)
michaelni
parents:
1228
diff
changeset
|
180 int re_index=0; |
|
b8e1c17b8d7d
some libmpeg2 style bitstream reader fixes (no dv doesnt yet work with it)
michaelni
parents:
1228
diff
changeset
|
181 int re1_index=0; |
|
b8e1c17b8d7d
some libmpeg2 style bitstream reader fixes (no dv doesnt yet work with it)
michaelni
parents:
1228
diff
changeset
|
182 #endif |
| 723 | 183 OPEN_READER(re, &s->gb); |
| 184 | |
| 185 #ifdef VLC_DEBUG | |
| 725 | 186 printf("start\n"); |
| 723 | 187 #endif |
| 188 | |
| 189 /* if we must parse a partial vlc, we do it here */ | |
| 725 | 190 partial_bit_count = mb->partial_bit_count; |
| 723 | 191 if (partial_bit_count > 0) { |
| 1064 | 192 uint8_t buf[4]; |
| 193 uint32_t v; | |
| 723 | 194 int l, l1; |
| 195 GetBitContext gb1; | |
| 196 | |
| 197 /* build the dummy bit buffer */ | |
| 198 l = 16 - partial_bit_count; | |
| 199 UPDATE_CACHE(re, &s->gb); | |
| 200 #ifdef VLC_DEBUG | |
| 201 printf("show=%04x\n", SHOW_UBITS(re, &s->gb, 16)); | |
| 202 #endif | |
| 725 | 203 v = (mb->partial_bit_buffer << l) | SHOW_UBITS(re, &s->gb, l); |
| 723 | 204 buf[0] = v >> 8; |
| 205 buf[1] = v; | |
| 206 #ifdef VLC_DEBUG | |
| 207 printf("v=%04x cnt=%d %04x\n", | |
| 725 | 208 v, partial_bit_count, (mb->partial_bit_buffer << l)); |
| 723 | 209 #endif |
| 210 /* 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
|
211 init_get_bits(&gb1, buf, 4*8); |
| 723 | 212 { |
| 213 OPEN_READER(re1, &gb1); | |
| 214 UPDATE_CACHE(re1, &gb1); | |
| 215 GET_RL_VLC(level, run, re1, &gb1, dv_rl_vlc[0], | |
| 216 TEX_VLC_BITS, 2); | |
| 217 l = re1_index; | |
| 218 CLOSE_READER(re1, &gb1); | |
| 219 } | |
| 220 #ifdef VLC_DEBUG | |
| 221 printf("****run=%d level=%d size=%d\n", run, level, l); | |
| 222 #endif | |
| 223 /* compute codeword length */ | |
| 224 l1 = (level != 256 && level != 0); | |
| 225 /* if too long, we cannot parse */ | |
| 226 l -= partial_bit_count; | |
| 227 if ((re_index + l + l1) > last_index) | |
| 228 return; | |
| 229 /* skip read bits */ | |
| 230 last_re_index = 0; /* avoid warning */ | |
| 231 re_index += l; | |
| 725 | 232 /* by definition, if we can read the vlc, all partial bits |
| 723 | 233 will be read (otherwise we could have read the vlc before) */ |
| 725 | 234 mb->partial_bit_count = 0; |
| 723 | 235 UPDATE_CACHE(re, &s->gb); |
| 236 goto handle_vlc; | |
| 237 } | |
| 238 | |
| 239 /* get the AC coefficients until last_index is reached */ | |
| 240 for(;;) { | |
| 241 UPDATE_CACHE(re, &s->gb); | |
| 242 #ifdef VLC_DEBUG | |
| 243 printf("%2d: bits=%04x index=%d\n", | |
| 244 pos, SHOW_UBITS(re, &s->gb, 16), re_index); | |
| 245 #endif | |
| 246 last_re_index = re_index; | |
| 247 GET_RL_VLC(level, run, re, &s->gb, dv_rl_vlc[0], | |
| 248 TEX_VLC_BITS, 2); | |
| 249 handle_vlc: | |
| 250 #ifdef VLC_DEBUG | |
| 251 printf("run=%d level=%d\n", run, level); | |
| 252 #endif | |
| 253 if (level == 256) { | |
| 254 if (re_index > last_index) { | |
| 255 cannot_read: | |
| 256 /* put position before read code */ | |
| 257 re_index = last_re_index; | |
| 725 | 258 mb->eob_reached = 0; |
| 723 | 259 break; |
| 260 } | |
| 261 /* EOB */ | |
| 725 | 262 mb->eob_reached = 1; |
| 723 | 263 break; |
| 264 } else if (level != 0) { | |
| 265 if ((re_index + 1) > last_index) | |
| 266 goto cannot_read; | |
| 267 sign = SHOW_SBITS(re, &s->gb, 1); | |
| 268 level = (level ^ sign) - sign; | |
| 269 LAST_SKIP_BITS(re, &s->gb, 1); | |
| 270 pos += run; | |
| 271 /* error */ | |
| 272 if (pos >= 64) { | |
| 273 goto read_error; | |
| 274 } | |
| 275 pos1 = scan_table[pos]; | |
| 725 | 276 level = level << (shift_table[pos1] + shift_offset); |
| 723 | 277 block[pos1] = level; |
| 278 // printf("run=%d level=%d shift=%d\n", run, level, shift_table[pos1]); | |
| 279 } else { | |
| 280 if (re_index > last_index) | |
| 281 goto cannot_read; | |
| 282 /* level is zero: means run without coding. No | |
| 283 sign is coded */ | |
| 284 pos += run; | |
| 285 /* error */ | |
| 286 if (pos >= 64) { | |
| 287 read_error: | |
| 288 #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
|
289 fprintf(stderr, "error pos=%d\n", pos); |
| 723 | 290 #endif |
| 291 /* for errors, we consider the eob is reached */ | |
| 725 | 292 mb->eob_reached = 1; |
| 723 | 293 break; |
| 294 } | |
| 295 } | |
| 296 } | |
| 297 CLOSE_READER(re, &s->gb); | |
| 725 | 298 mb->pos = pos; |
| 723 | 299 } |
| 300 | |
| 301 static inline void bit_copy(PutBitContext *pb, GetBitContext *gb, int bits_left) | |
| 302 { | |
| 303 while (bits_left >= 16) { | |
| 304 put_bits(pb, 16, get_bits(gb, 16)); | |
| 305 bits_left -= 16; | |
| 306 } | |
| 307 if (bits_left > 0) { | |
| 308 put_bits(pb, bits_left, get_bits(gb, bits_left)); | |
| 309 } | |
| 310 } | |
| 311 | |
| 312 /* mb_x and mb_y are in units of 8 pixels */ | |
| 313 static inline void dv_decode_video_segment(DVVideoDecodeContext *s, | |
| 1064 | 314 uint8_t *buf_ptr1, |
| 315 const uint16_t *mb_pos_ptr) | |
| 723 | 316 { |
| 317 int quant, dc, dct_mode, class1, j; | |
| 318 int mb_index, mb_x, mb_y, v, last_index; | |
| 319 DCTELEM *block, *block1; | |
| 320 int c_offset, bits_left; | |
| 1064 | 321 uint8_t *y_ptr; |
| 725 | 322 BlockInfo mb_data[5 * 6], *mb, *mb1; |
| 1064 | 323 void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block); |
| 324 uint8_t *buf_ptr; | |
| 723 | 325 PutBitContext pb, vs_pb; |
| 1064 | 326 uint8_t mb_bit_buffer[80 + 4]; /* allow some slack */ |
| 725 | 327 int mb_bit_count; |
| 1064 | 328 uint8_t vs_bit_buffer[5 * 80 + 4]; /* allow some slack */ |
| 723 | 329 int vs_bit_count; |
| 330 | |
| 331 memset(s->block, 0, sizeof(s->block)); | |
| 332 | |
| 333 /* pass 1 : read DC and AC coefficients in blocks */ | |
| 334 buf_ptr = buf_ptr1; | |
| 335 block1 = &s->block[0][0]; | |
| 725 | 336 mb1 = mb_data; |
| 723 | 337 init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80, NULL, NULL); |
| 338 vs_bit_count = 0; | |
| 339 for(mb_index = 0; mb_index < 5; mb_index++) { | |
| 340 /* skip header */ | |
| 341 quant = buf_ptr[3] & 0x0f; | |
| 342 buf_ptr += 4; | |
| 725 | 343 init_put_bits(&pb, mb_bit_buffer, 80, NULL, NULL); |
| 344 mb_bit_count = 0; | |
| 345 mb = mb1; | |
| 723 | 346 block = block1; |
| 347 for(j = 0;j < 6; j++) { | |
| 348 /* 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
|
349 init_get_bits(&s->gb, buf_ptr, 14*8); |
| 723 | 350 |
| 351 /* get the dc */ | |
| 352 dc = get_bits(&s->gb, 9); | |
| 353 dc = (dc << (32 - 9)) >> (32 - 9); | |
| 354 dct_mode = get_bits1(&s->gb); | |
| 725 | 355 mb->dct_mode = dct_mode; |
| 356 mb->scan_table = s->dv_zigzag[dct_mode]; | |
| 723 | 357 class1 = get_bits(&s->gb, 2); |
| 725 | 358 mb->shift_offset = (class1 == 3); |
| 359 mb->shift_table = s->dv_shift[dct_mode] | |
| 723 | 360 [quant + dv_quant_offset[class1]]; |
| 361 dc = dc << 2; | |
| 362 /* convert to unsigned because 128 is not added in the | |
| 363 standard IDCT */ | |
| 364 dc += 1024; | |
| 365 block[0] = dc; | |
| 366 last_index = block_sizes[j]; | |
| 367 buf_ptr += last_index >> 3; | |
| 725 | 368 mb->pos = 0; |
| 369 mb->partial_bit_count = 0; | |
| 723 | 370 |
| 725 | 371 dv_decode_ac(s, mb, block, last_index); |
| 723 | 372 |
| 373 /* write the remaining bits in a new buffer only if the | |
| 374 block is finished */ | |
|
1256
b8e1c17b8d7d
some libmpeg2 style bitstream reader fixes (no dv doesnt yet work with it)
michaelni
parents:
1228
diff
changeset
|
375 bits_left = last_index - get_bits_count(&s->gb); |
| 725 | 376 if (mb->eob_reached) { |
| 377 mb->partial_bit_count = 0; | |
| 378 mb_bit_count += bits_left; | |
| 723 | 379 bit_copy(&pb, &s->gb, bits_left); |
| 380 } else { | |
| 381 /* should be < 16 bits otherwise a codeword could have | |
| 382 been parsed */ | |
| 725 | 383 mb->partial_bit_count = bits_left; |
| 384 mb->partial_bit_buffer = get_bits(&s->gb, bits_left); | |
| 723 | 385 } |
| 386 block += 64; | |
| 725 | 387 mb++; |
| 723 | 388 } |
| 389 | |
| 390 flush_put_bits(&pb); | |
| 391 | |
| 392 /* pass 2 : we can do it just after */ | |
| 393 #ifdef VLC_DEBUG | |
| 725 | 394 printf("***pass 2 size=%d\n", mb_bit_count); |
| 723 | 395 #endif |
| 396 block = block1; | |
| 725 | 397 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
|
398 init_get_bits(&s->gb, mb_bit_buffer, 80*8); |
| 723 | 399 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
|
400 if (!mb->eob_reached && get_bits_count(&s->gb) < mb_bit_count) { |
| 725 | 401 dv_decode_ac(s, mb, block, mb_bit_count); |
| 723 | 402 /* if still not finished, no need to parse other blocks */ |
| 725 | 403 if (!mb->eob_reached) { |
| 723 | 404 /* we could not parse the current AC coefficient, |
| 405 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
|
406 bits_left = mb_bit_count - get_bits_count(&s->gb); |
| 723 | 407 if (bits_left > 0) { |
| 725 | 408 mb->partial_bit_count += bits_left; |
| 409 mb->partial_bit_buffer = | |
| 410 (mb->partial_bit_buffer << bits_left) | | |
| 723 | 411 get_bits(&s->gb, bits_left); |
| 412 } | |
| 413 goto next_mb; | |
| 414 } | |
| 415 } | |
| 416 block += 64; | |
| 725 | 417 mb++; |
| 723 | 418 } |
| 419 /* all blocks are finished, so the extra bytes can be used at | |
| 420 the video segment level */ | |
|
1256
b8e1c17b8d7d
some libmpeg2 style bitstream reader fixes (no dv doesnt yet work with it)
michaelni
parents:
1228
diff
changeset
|
421 bits_left = mb_bit_count - get_bits_count(&s->gb); |
| 723 | 422 vs_bit_count += bits_left; |
| 423 bit_copy(&vs_pb, &s->gb, bits_left); | |
| 424 next_mb: | |
| 725 | 425 mb1 += 6; |
| 723 | 426 block1 += 6 * 64; |
| 427 } | |
| 428 | |
| 429 /* we need a pass other the whole video segment */ | |
| 430 flush_put_bits(&vs_pb); | |
| 431 | |
| 432 #ifdef VLC_DEBUG | |
| 433 printf("***pass 3 size=%d\n", vs_bit_count); | |
| 434 #endif | |
| 435 block = &s->block[0][0]; | |
| 436 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
|
437 init_get_bits(&s->gb, vs_bit_buffer, 5 * 80*8); |
| 723 | 438 for(mb_index = 0; mb_index < 5; mb_index++) { |
| 439 for(j = 0;j < 6; j++) { | |
| 725 | 440 if (!mb->eob_reached) { |
| 723 | 441 #ifdef VLC_DEBUG |
| 442 printf("start %d:%d\n", mb_index, j); | |
| 443 #endif | |
| 725 | 444 dv_decode_ac(s, mb, block, vs_bit_count); |
| 723 | 445 } |
| 446 block += 64; | |
| 725 | 447 mb++; |
| 723 | 448 } |
| 449 } | |
| 450 | |
| 451 /* compute idct and place blocks */ | |
| 452 block = &s->block[0][0]; | |
| 453 mb = mb_data; | |
| 454 for(mb_index = 0; mb_index < 5; mb_index++) { | |
| 455 v = *mb_pos_ptr++; | |
| 456 mb_x = v & 0xff; | |
| 457 mb_y = v >> 8; | |
|
1489
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
458 y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 8); |
|
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
459 if (s->sys->pix_fmt == PIX_FMT_YUV411P) |
|
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
460 c_offset = (mb_y * s->picture.linesize[1] * 8) + ((mb_x >> 2) * 8); |
| 723 | 461 else |
|
1489
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
462 c_offset = ((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8); |
| 723 | 463 for(j = 0;j < 6; j++) { |
| 725 | 464 idct_put = s->idct_put[mb->dct_mode]; |
| 723 | 465 if (j < 4) { |
|
1489
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
466 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) { |
| 737 | 467 /* NOTE: at end of line, the macroblock is handled as 420 */ |
|
1489
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
468 idct_put(y_ptr + (j * 8), s->picture.linesize[0], block); |
| 723 | 469 } else { |
|
1489
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
470 idct_put(y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]), |
|
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
471 s->picture.linesize[0], block); |
| 723 | 472 } |
| 473 } else { | |
|
1489
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
474 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) { |
| 737 | 475 uint8_t pixels[64], *c_ptr, *c_ptr1, *ptr; |
| 476 int y, linesize; | |
| 477 /* NOTE: at end of line, the macroblock is handled as 420 */ | |
| 478 idct_put(pixels, 8, block); | |
|
1489
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
479 linesize = s->picture.linesize[6 - j]; |
|
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
480 c_ptr = s->picture.data[6 - j] + c_offset; |
| 737 | 481 ptr = pixels; |
| 482 for(y = 0;y < 8; y++) { | |
| 483 /* convert to 411P */ | |
|
1270
f5318caa93f4
seems i guessed correctly (last 411 chroma block isnt scaled but cut and reordered)
michaelni
parents:
1256
diff
changeset
|
484 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
|
485 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
|
486 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
|
487 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
|
488 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
|
489 c_ptr += linesize; |
| 737 | 490 ptr += 8; |
| 491 } | |
| 492 } else { | |
| 493 /* don't ask me why they inverted Cb and Cr ! */ | |
|
1489
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
494 idct_put(s->picture.data[6 - j] + c_offset, |
|
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
495 s->picture.linesize[6 - j], block); |
| 737 | 496 } |
| 723 | 497 } |
| 498 block += 64; | |
| 725 | 499 mb++; |
| 723 | 500 } |
| 501 } | |
| 502 } | |
| 503 | |
| 504 /* NOTE: exactly one frame must be given (120000 bytes for NTSC, | |
| 505 144000 bytes for PAL) */ | |
| 506 static int dvvideo_decode_frame(AVCodecContext *avctx, | |
| 507 void *data, int *data_size, | |
| 1064 | 508 uint8_t *buf, int buf_size) |
| 723 | 509 { |
| 510 DVVideoDecodeContext *s = avctx->priv_data; | |
|
1489
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
511 int ds, vs; |
| 1064 | 512 const uint16_t *mb_pos_ptr; |
| 723 | 513 |
|
1489
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
514 s->sys = dv_frame_profile(buf); |
|
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
515 if (!s->sys || buf_size < s->sys->frame_size) |
|
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
516 return -1; /* NOTE: we only accept several full frames */ |
| 723 | 517 |
|
1489
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
518 |
| 1228 | 519 if(s->picture.data[0]) |
| 520 avctx->release_buffer(avctx, &s->picture); | |
| 521 | |
|
1489
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
522 s->picture.reference = 0; |
|
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
523 avctx->pix_fmt = s->sys->pix_fmt; |
| 903 | 524 if(avctx->get_buffer(avctx, &s->picture) < 0) { |
| 525 fprintf(stderr, "get_buffer() failed\n"); | |
| 526 return -1; | |
| 835 | 527 } |
| 528 | |
| 723 | 529 /* for each DIF segment */ |
|
1489
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
530 mb_pos_ptr = s->sys->video_place; |
|
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
531 for (ds = 0; ds < s->sys->difseg_size; ds++) { |
|
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
532 buf += 6 * 80; /* skip DIF segment header */ |
| 723 | 533 |
| 534 for(vs = 0; vs < 27; vs++) { | |
|
1489
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
535 if ((vs % 3) == 0) |
|
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
536 buf += 80; /* skip audio block */ |
|
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
537 |
|
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
538 dv_decode_video_segment(s, buf, mb_pos_ptr); |
|
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
539 buf += 5 * 80; |
| 723 | 540 mb_pos_ptr += 5; |
| 541 } | |
| 542 } | |
| 543 | |
|
734
2d6b3e3d6c6f
10l - MMX/FPU state was not restored, causing nonsense fpu behaviour in caller (mplayer)
arpi_esp
parents:
733
diff
changeset
|
544 emms_c(); |
|
2d6b3e3d6c6f
10l - MMX/FPU state was not restored, causing nonsense fpu behaviour in caller (mplayer)
arpi_esp
parents:
733
diff
changeset
|
545 |
| 723 | 546 /* return image */ |
| 925 | 547 *data_size = sizeof(AVFrame); |
| 548 *(AVFrame*)data= s->picture; | |
| 903 | 549 |
|
1489
337d13aee605
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
1416
diff
changeset
|
550 return s->sys->frame_size; |
| 723 | 551 } |
| 552 | |
| 553 static int dvvideo_decode_end(AVCodecContext *avctx) | |
| 554 { | |
| 1214 | 555 avcodec_default_free_buffers(avctx); |
| 723 | 556 |
| 557 return 0; | |
| 558 } | |
| 559 | |
| 560 AVCodec dvvideo_decoder = { | |
| 561 "dvvideo", | |
| 562 CODEC_TYPE_VIDEO, | |
| 563 CODEC_ID_DVVIDEO, | |
| 564 sizeof(DVVideoDecodeContext), | |
| 565 dvvideo_decode_init, | |
| 566 NULL, | |
| 567 dvvideo_decode_end, | |
| 568 dvvideo_decode_frame, | |
| 835 | 569 CODEC_CAP_DR1, |
| 723 | 570 NULL |
| 571 }; |
