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