00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00038 #include "libavutil/pixdesc.h"
00039 #include "avcodec.h"
00040 #include "dsputil.h"
00041 #include "get_bits.h"
00042 #include "put_bits.h"
00043 #include "simple_idct.h"
00044 #include "dvdata.h"
00045
00046 typedef struct BlockInfo {
00047 const uint32_t *factor_table;
00048 const uint8_t *scan_table;
00049 uint8_t pos;
00050 void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block);
00051 uint8_t partial_bit_count;
00052 uint32_t partial_bit_buffer;
00053 int shift_offset;
00054 } BlockInfo;
00055
00056 static const int dv_iweight_bits = 14;
00057
00058
00059 static void dv_decode_ac(GetBitContext *gb, BlockInfo *mb, DCTELEM *block)
00060 {
00061 int last_index = gb->size_in_bits;
00062 const uint8_t *scan_table = mb->scan_table;
00063 const uint32_t *factor_table = mb->factor_table;
00064 int pos = mb->pos;
00065 int partial_bit_count = mb->partial_bit_count;
00066 int level, run, vlc_len, index;
00067
00068 OPEN_READER(re, gb);
00069 UPDATE_CACHE(re, gb);
00070
00071
00072 if (partial_bit_count > 0) {
00073 re_cache = re_cache >> partial_bit_count | mb->partial_bit_buffer;
00074 re_index -= partial_bit_count;
00075 mb->partial_bit_count = 0;
00076 }
00077
00078
00079 for (;;) {
00080 av_dlog(NULL, "%2d: bits=%04x index=%d\n", pos, SHOW_UBITS(re, gb, 16),
00081 re_index);
00082
00083 index = NEG_USR32(re_cache, TEX_VLC_BITS);
00084 vlc_len = ff_dv_rl_vlc[index].len;
00085 if (vlc_len < 0) {
00086 index = NEG_USR32((unsigned)re_cache << TEX_VLC_BITS, -vlc_len) +
00087 ff_dv_rl_vlc[index].level;
00088 vlc_len = TEX_VLC_BITS - vlc_len;
00089 }
00090 level = ff_dv_rl_vlc[index].level;
00091 run = ff_dv_rl_vlc[index].run;
00092
00093
00094 if (re_index + vlc_len > last_index) {
00095
00096 mb->partial_bit_count = last_index - re_index;
00097 mb->partial_bit_buffer = re_cache & ~(-1u >> mb->partial_bit_count);
00098 re_index = last_index;
00099 break;
00100 }
00101 re_index += vlc_len;
00102
00103 av_dlog(NULL, "run=%d level=%d\n", run, level);
00104 pos += run;
00105 if (pos >= 64)
00106 break;
00107
00108 level = (level * factor_table[pos] + (1 << (dv_iweight_bits - 1))) >> dv_iweight_bits;
00109 block[scan_table[pos]] = level;
00110
00111 UPDATE_CACHE(re, gb);
00112 }
00113 CLOSE_READER(re, gb);
00114 mb->pos = pos;
00115 }
00116
00117 static inline void bit_copy(PutBitContext *pb, GetBitContext *gb)
00118 {
00119 int bits_left = get_bits_left(gb);
00120 while (bits_left >= MIN_CACHE_BITS) {
00121 put_bits(pb, MIN_CACHE_BITS, get_bits(gb, MIN_CACHE_BITS));
00122 bits_left -= MIN_CACHE_BITS;
00123 }
00124 if (bits_left > 0) {
00125 put_bits(pb, bits_left, get_bits(gb, bits_left));
00126 }
00127 }
00128
00129
00130 static int dv_decode_video_segment(AVCodecContext *avctx, void *arg)
00131 {
00132 DVVideoContext *s = avctx->priv_data;
00133 DVwork_chunk *work_chunk = arg;
00134 int quant, dc, dct_mode, class1, j;
00135 int mb_index, mb_x, mb_y, last_index;
00136 int y_stride, linesize;
00137 DCTELEM *block, *block1;
00138 int c_offset;
00139 uint8_t *y_ptr;
00140 const uint8_t *buf_ptr;
00141 PutBitContext pb, vs_pb;
00142 GetBitContext gb;
00143 BlockInfo mb_data[5 * DV_MAX_BPM], *mb, *mb1;
00144 LOCAL_ALIGNED_16(DCTELEM, sblock, [5*DV_MAX_BPM], [64]);
00145 LOCAL_ALIGNED_16(uint8_t, mb_bit_buffer, [ 80 + FF_INPUT_BUFFER_PADDING_SIZE]);
00146 LOCAL_ALIGNED_16(uint8_t, vs_bit_buffer, [5*80 + FF_INPUT_BUFFER_PADDING_SIZE]);
00147 const int log2_blocksize = 3-s->avctx->lowres;
00148 int is_field_mode[5];
00149
00150 assert((((int)mb_bit_buffer) & 7) == 0);
00151 assert((((int)vs_bit_buffer) & 7) == 0);
00152
00153 memset(sblock, 0, 5*DV_MAX_BPM*sizeof(*sblock));
00154
00155
00156 buf_ptr = &s->buf[work_chunk->buf_offset*80];
00157 block1 = &sblock[0][0];
00158 mb1 = mb_data;
00159 init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80);
00160 for (mb_index = 0; mb_index < 5; mb_index++, mb1 += s->sys->bpm, block1 += s->sys->bpm * 64) {
00161
00162 quant = buf_ptr[3] & 0x0f;
00163 buf_ptr += 4;
00164 init_put_bits(&pb, mb_bit_buffer, 80);
00165 mb = mb1;
00166 block = block1;
00167 is_field_mode[mb_index] = 0;
00168 for (j = 0; j < s->sys->bpm; j++) {
00169 last_index = s->sys->block_sizes[j];
00170 init_get_bits(&gb, buf_ptr, last_index);
00171
00172
00173 dc = get_sbits(&gb, 9);
00174 dct_mode = get_bits1(&gb);
00175 class1 = get_bits(&gb, 2);
00176 if (DV_PROFILE_IS_HD(s->sys)) {
00177 mb->idct_put = s->idct_put[0];
00178 mb->scan_table = s->dv_zigzag[0];
00179 mb->factor_table = &s->sys->idct_factor[(j >= 4)*4*16*64 + class1*16*64 + quant*64];
00180 is_field_mode[mb_index] |= !j && dct_mode;
00181 } else {
00182 mb->idct_put = s->idct_put[dct_mode && log2_blocksize == 3];
00183 mb->scan_table = s->dv_zigzag[dct_mode];
00184 mb->factor_table = &s->sys->idct_factor[(class1 == 3)*2*22*64 + dct_mode*22*64 +
00185 (quant + ff_dv_quant_offset[class1])*64];
00186 }
00187 dc = dc << 2;
00188
00189
00190 dc += 1024;
00191 block[0] = dc;
00192 buf_ptr += last_index >> 3;
00193 mb->pos = 0;
00194 mb->partial_bit_count = 0;
00195
00196 av_dlog(avctx, "MB block: %d, %d ", mb_index, j);
00197 dv_decode_ac(&gb, mb, block);
00198
00199
00200
00201 if (mb->pos >= 64)
00202 bit_copy(&pb, &gb);
00203
00204 block += 64;
00205 mb++;
00206 }
00207
00208
00209 av_dlog(avctx, "***pass 2 size=%d MB#=%d\n", put_bits_count(&pb), mb_index);
00210 block = block1;
00211 mb = mb1;
00212 init_get_bits(&gb, mb_bit_buffer, put_bits_count(&pb));
00213 put_bits32(&pb, 0);
00214 flush_put_bits(&pb);
00215 for (j = 0; j < s->sys->bpm; j++, block += 64, mb++) {
00216 if (mb->pos < 64 && get_bits_left(&gb) > 0) {
00217 dv_decode_ac(&gb, mb, block);
00218
00219 if (mb->pos < 64)
00220 break;
00221 }
00222 }
00223
00224
00225 if (j >= s->sys->bpm)
00226 bit_copy(&vs_pb, &gb);
00227 }
00228
00229
00230 av_dlog(avctx, "***pass 3 size=%d\n", put_bits_count(&vs_pb));
00231 block = &sblock[0][0];
00232 mb = mb_data;
00233 init_get_bits(&gb, vs_bit_buffer, put_bits_count(&vs_pb));
00234 put_bits32(&vs_pb, 0);
00235 flush_put_bits(&vs_pb);
00236 for (mb_index = 0; mb_index < 5; mb_index++) {
00237 for (j = 0; j < s->sys->bpm; j++) {
00238 if (mb->pos < 64) {
00239 av_dlog(avctx, "start %d:%d\n", mb_index, j);
00240 dv_decode_ac(&gb, mb, block);
00241 }
00242 if (mb->pos >= 64 && mb->pos < 127)
00243 av_log(avctx, AV_LOG_ERROR, "AC EOB marker is absent pos=%d\n", mb->pos);
00244 block += 64;
00245 mb++;
00246 }
00247 }
00248
00249
00250 block = &sblock[0][0];
00251 mb = mb_data;
00252 for (mb_index = 0; mb_index < 5; mb_index++) {
00253 dv_calculate_mb_xy(s, work_chunk, mb_index, &mb_x, &mb_y);
00254
00255
00256 if ((s->sys->pix_fmt == PIX_FMT_YUV420P) ||
00257 (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) ||
00258 (s->sys->height >= 720 && mb_y != 134)) {
00259 y_stride = (s->picture.linesize[0] << ((!is_field_mode[mb_index]) * log2_blocksize));
00260 } else {
00261 y_stride = (2 << log2_blocksize);
00262 }
00263 y_ptr = s->picture.data[0] + ((mb_y * s->picture.linesize[0] + mb_x) << log2_blocksize);
00264 linesize = s->picture.linesize[0] << is_field_mode[mb_index];
00265 mb[0] .idct_put(y_ptr , linesize, block + 0*64);
00266 if (s->sys->video_stype == 4) {
00267 mb[2].idct_put(y_ptr + (1 << log2_blocksize) , linesize, block + 2*64);
00268 } else {
00269 mb[1].idct_put(y_ptr + (1 << log2_blocksize) , linesize, block + 1*64);
00270 mb[2].idct_put(y_ptr + y_stride, linesize, block + 2*64);
00271 mb[3].idct_put(y_ptr + (1 << log2_blocksize) + y_stride, linesize, block + 3*64);
00272 }
00273 mb += 4;
00274 block += 4*64;
00275
00276
00277 c_offset = (((mb_y >> (s->sys->pix_fmt == PIX_FMT_YUV420P)) * s->picture.linesize[1] +
00278 (mb_x >> ((s->sys->pix_fmt == PIX_FMT_YUV411P) ? 2 : 1))) << log2_blocksize);
00279 for (j = 2; j; j--) {
00280 uint8_t *c_ptr = s->picture.data[j] + c_offset;
00281 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
00282 uint64_t aligned_pixels[64/8];
00283 uint8_t *pixels = (uint8_t*)aligned_pixels;
00284 uint8_t *c_ptr1, *ptr1;
00285 int x, y;
00286 mb->idct_put(pixels, 8, block);
00287 for (y = 0; y < (1 << log2_blocksize); y++, c_ptr += s->picture.linesize[j], pixels += 8) {
00288 ptr1 = pixels + (1 << (log2_blocksize - 1));
00289 c_ptr1 = c_ptr + (s->picture.linesize[j] << log2_blocksize);
00290 for (x = 0; x < (1 << (log2_blocksize - 1)); x++) {
00291 c_ptr[x] = pixels[x];
00292 c_ptr1[x] = ptr1[x];
00293 }
00294 }
00295 block += 64; mb++;
00296 } else {
00297 y_stride = (mb_y == 134) ? (1 << log2_blocksize) :
00298 s->picture.linesize[j] << ((!is_field_mode[mb_index]) * log2_blocksize);
00299 linesize = s->picture.linesize[j] << is_field_mode[mb_index];
00300 (mb++)-> idct_put(c_ptr , linesize, block); block += 64;
00301 if (s->sys->bpm == 8) {
00302 (mb++)->idct_put(c_ptr + y_stride, linesize, block); block += 64;
00303 }
00304 }
00305 }
00306 }
00307 return 0;
00308 }
00309
00310
00311
00312 static int dvvideo_decode_frame(AVCodecContext *avctx,
00313 void *data, int *data_size,
00314 AVPacket *avpkt)
00315 {
00316 uint8_t *buf = avpkt->data;
00317 int buf_size = avpkt->size;
00318 DVVideoContext *s = avctx->priv_data;
00319 const uint8_t* vsc_pack;
00320 int apt, is16_9;
00321
00322 s->sys = avpriv_dv_frame_profile2(avctx, s->sys, buf, buf_size);
00323 if (!s->sys || buf_size < s->sys->frame_size || ff_dv_init_dynamic_tables(s->sys)) {
00324 av_log(avctx, AV_LOG_ERROR, "could not find dv frame profile\n");
00325 return -1;
00326 }
00327
00328 if (s->picture.data[0])
00329 avctx->release_buffer(avctx, &s->picture);
00330
00331 avcodec_get_frame_defaults(&s->picture);
00332 s->picture.reference = 0;
00333 s->picture.key_frame = 1;
00334 s->picture.pict_type = AV_PICTURE_TYPE_I;
00335 avctx->pix_fmt = s->sys->pix_fmt;
00336 avctx->time_base = s->sys->time_base;
00337 avcodec_set_dimensions(avctx, s->sys->width, s->sys->height);
00338 if (avctx->get_buffer(avctx, &s->picture) < 0) {
00339 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00340 return -1;
00341 }
00342 s->picture.interlaced_frame = 1;
00343 s->picture.top_field_first = 0;
00344
00345 s->buf = buf;
00346 avctx->execute(avctx, dv_decode_video_segment, s->sys->work_chunks, NULL,
00347 dv_work_pool_size(s->sys), sizeof(DVwork_chunk));
00348
00349 emms_c();
00350
00351
00352 *data_size = sizeof(AVFrame);
00353 *(AVFrame*)data = s->picture;
00354
00355
00356 vsc_pack = buf + 80*5 + 48 + 5;
00357 if ( *vsc_pack == dv_video_control ) {
00358 apt = buf[4] & 0x07;
00359 is16_9 = (vsc_pack[2] & 0x07) == 0x02 || (!apt && (vsc_pack[2] & 0x07) == 0x07);
00360 avctx->sample_aspect_ratio = s->sys->sar[is16_9];
00361 }
00362
00363 return s->sys->frame_size;
00364 }
00365
00366 static int dvvideo_close(AVCodecContext *c)
00367 {
00368 DVVideoContext *s = c->priv_data;
00369
00370 if (s->picture.data[0])
00371 c->release_buffer(c, &s->picture);
00372
00373 return 0;
00374 }
00375
00376 AVCodec ff_dvvideo_decoder = {
00377 .name = "dvvideo",
00378 .type = AVMEDIA_TYPE_VIDEO,
00379 .id = CODEC_ID_DVVIDEO,
00380 .priv_data_size = sizeof(DVVideoContext),
00381 .init = ff_dvvideo_init,
00382 .close = dvvideo_close,
00383 .decode = dvvideo_decode_frame,
00384 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_SLICE_THREADS,
00385 .max_lowres = 3,
00386 .long_name = NULL_IF_CONFIG_SMALL("DV (Digital Video)"),
00387 };