350
323
static av_cold int dvvideo_init_encoder(AVCodecContext *avctx)
352
325
if (!avpriv_dv_codec_profile(avctx)) {
353
av_log(avctx, AV_LOG_ERROR, "Found no DV profile for %ix%i %s video\n",
326
av_log(avctx, AV_LOG_ERROR, "Found no DV profile for %ix%i %s video. "
327
"Valid DV profiles are:\n",
354
328
avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
329
ff_dv_print_profiles(avctx, AV_LOG_ERROR);
330
return AVERROR(EINVAL);
358
return dvvideo_init(avctx);
333
dv_vlc_map_tableinit();
335
return ff_dvvideo_init(avctx);
361
typedef struct BlockInfo {
362
const uint32_t *factor_table;
363
const uint8_t *scan_table;
364
uint8_t pos; /* position in block */
365
void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block);
366
uint8_t partial_bit_count;
367
uint32_t partial_bit_buffer;
371
338
/* bit budget for AC only in 5 MBs */
372
339
static const int vs_total_ac_bits = (100 * 4 + 68*2) * 5;
373
340
static const int mb_area_start[5] = { 1, 6, 21, 43, 64 };
375
static inline int put_bits_left(PutBitContext* s)
377
return (s->buf_end - s->buf) * 8 - put_bits_count(s);
380
/* decode AC coefficients */
381
static void dv_decode_ac(GetBitContext *gb, BlockInfo *mb, DCTELEM *block)
383
int last_index = gb->size_in_bits;
384
const uint8_t *scan_table = mb->scan_table;
385
const uint32_t *factor_table = mb->factor_table;
387
int partial_bit_count = mb->partial_bit_count;
388
int level, run, vlc_len, index;
391
UPDATE_CACHE(re, gb);
393
/* if we must parse a partial VLC, we do it here */
394
if (partial_bit_count > 0) {
395
re_cache = re_cache >> partial_bit_count | mb->partial_bit_buffer;
396
re_index -= partial_bit_count;
397
mb->partial_bit_count = 0;
400
/* get the AC coefficients until last_index is reached */
402
av_dlog(NULL, "%2d: bits=%04x index=%d\n", pos, SHOW_UBITS(re, gb, 16),
404
/* our own optimized GET_RL_VLC */
405
index = NEG_USR32(re_cache, TEX_VLC_BITS);
406
vlc_len = dv_rl_vlc[index].len;
408
index = NEG_USR32((unsigned)re_cache << TEX_VLC_BITS, -vlc_len) + dv_rl_vlc[index].level;
409
vlc_len = TEX_VLC_BITS - vlc_len;
411
level = dv_rl_vlc[index].level;
412
run = dv_rl_vlc[index].run;
414
/* gotta check if we're still within gb boundaries */
415
if (re_index + vlc_len > last_index) {
416
/* should be < 16 bits otherwise a codeword could have been parsed */
417
mb->partial_bit_count = last_index - re_index;
418
mb->partial_bit_buffer = re_cache & ~(-1u >> mb->partial_bit_count);
419
re_index = last_index;
424
av_dlog(NULL, "run=%d level=%d\n", run, level);
429
level = (level * factor_table[pos] + (1 << (dv_iweight_bits - 1))) >> dv_iweight_bits;
430
block[scan_table[pos]] = level;
432
UPDATE_CACHE(re, gb);
434
CLOSE_READER(re, gb);
438
static inline void bit_copy(PutBitContext *pb, GetBitContext *gb)
440
int bits_left = get_bits_left(gb);
441
while (bits_left >= MIN_CACHE_BITS) {
442
put_bits(pb, MIN_CACHE_BITS, get_bits(gb, MIN_CACHE_BITS));
443
bits_left -= MIN_CACHE_BITS;
446
put_bits(pb, bits_left, get_bits(gb, bits_left));
450
static inline void dv_calculate_mb_xy(DVVideoContext *s, DVwork_chunk *work_chunk, int m, int *mb_x, int *mb_y)
452
*mb_x = work_chunk->mb_coordinates[m] & 0xff;
453
*mb_y = work_chunk->mb_coordinates[m] >> 8;
455
/* We work with 720p frames split in half. The odd half-frame (chan==2,3) is displaced :-( */
456
if (s->sys->height == 720 && !(s->buf[1]&0x0C)) {
457
*mb_y -= (*mb_y>17)?18:-72; /* shifting the Y coordinate down by 72/2 macro blocks */
461
/* mb_x and mb_y are in units of 8 pixels */
462
static int dv_decode_video_segment(AVCodecContext *avctx, void *arg)
464
DVVideoContext *s = avctx->priv_data;
465
DVwork_chunk *work_chunk = arg;
466
int quant, dc, dct_mode, class1, j;
467
int mb_index, mb_x, mb_y, last_index;
468
int y_stride, linesize;
469
DCTELEM *block, *block1;
472
const uint8_t *buf_ptr;
473
PutBitContext pb, vs_pb;
475
BlockInfo mb_data[5 * DV_MAX_BPM], *mb, *mb1;
476
LOCAL_ALIGNED_16(DCTELEM, sblock, [5*DV_MAX_BPM], [64]);
477
LOCAL_ALIGNED_16(uint8_t, mb_bit_buffer, [ 80 + FF_INPUT_BUFFER_PADDING_SIZE]); /* allow some slack */
478
LOCAL_ALIGNED_16(uint8_t, vs_bit_buffer, [5*80 + FF_INPUT_BUFFER_PADDING_SIZE]); /* allow some slack */
479
const int log2_blocksize = 3-s->avctx->lowres;
480
int is_field_mode[5];
482
assert((((int)mb_bit_buffer) & 7) == 0);
483
assert((((int)vs_bit_buffer) & 7) == 0);
485
memset(sblock, 0, 5*DV_MAX_BPM*sizeof(*sblock));
487
/* pass 1: read DC and AC coefficients in blocks */
488
buf_ptr = &s->buf[work_chunk->buf_offset*80];
489
block1 = &sblock[0][0];
491
init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80);
492
for (mb_index = 0; mb_index < 5; mb_index++, mb1 += s->sys->bpm, block1 += s->sys->bpm * 64) {
494
quant = buf_ptr[3] & 0x0f;
496
init_put_bits(&pb, mb_bit_buffer, 80);
499
is_field_mode[mb_index] = 0;
500
for (j = 0; j < s->sys->bpm; j++) {
501
last_index = s->sys->block_sizes[j];
502
init_get_bits(&gb, buf_ptr, last_index);
505
dc = get_sbits(&gb, 9);
506
dct_mode = get_bits1(&gb);
507
class1 = get_bits(&gb, 2);
508
if (DV_PROFILE_IS_HD(s->sys)) {
509
mb->idct_put = s->idct_put[0];
510
mb->scan_table = s->dv_zigzag[0];
511
mb->factor_table = &s->sys->idct_factor[(j >= 4)*4*16*64 + class1*16*64 + quant*64];
512
is_field_mode[mb_index] |= !j && dct_mode;
514
mb->idct_put = s->idct_put[dct_mode && log2_blocksize == 3];
515
mb->scan_table = s->dv_zigzag[dct_mode];
516
mb->factor_table = &s->sys->idct_factor[(class1 == 3)*2*22*64 + dct_mode*22*64 +
517
(quant + dv_quant_offset[class1])*64];
520
/* convert to unsigned because 128 is not added in the
524
buf_ptr += last_index >> 3;
526
mb->partial_bit_count = 0;
528
av_dlog(avctx, "MB block: %d, %d ", mb_index, j);
529
dv_decode_ac(&gb, mb, block);
531
/* write the remaining bits in a new buffer only if the
540
/* pass 2: we can do it just after */
541
av_dlog(avctx, "***pass 2 size=%d MB#=%d\n", put_bits_count(&pb), mb_index);
544
init_get_bits(&gb, mb_bit_buffer, put_bits_count(&pb));
545
put_bits32(&pb, 0); // padding must be zeroed
547
for (j = 0; j < s->sys->bpm; j++, block += 64, mb++) {
548
if (mb->pos < 64 && get_bits_left(&gb) > 0) {
549
dv_decode_ac(&gb, mb, block);
550
/* if still not finished, no need to parse other blocks */
555
/* all blocks are finished, so the extra bytes can be used at
556
the video segment level */
557
if (j >= s->sys->bpm)
558
bit_copy(&vs_pb, &gb);
561
/* we need a pass over the whole video segment */
562
av_dlog(avctx, "***pass 3 size=%d\n", put_bits_count(&vs_pb));
563
block = &sblock[0][0];
565
init_get_bits(&gb, vs_bit_buffer, put_bits_count(&vs_pb));
566
put_bits32(&vs_pb, 0); // padding must be zeroed
567
flush_put_bits(&vs_pb);
568
for (mb_index = 0; mb_index < 5; mb_index++) {
569
for (j = 0; j < s->sys->bpm; j++) {
571
av_dlog(avctx, "start %d:%d\n", mb_index, j);
572
dv_decode_ac(&gb, mb, block);
574
if (mb->pos >= 64 && mb->pos < 127)
575
av_log(avctx, AV_LOG_ERROR, "AC EOB marker is absent pos=%d\n", mb->pos);
581
/* compute idct and place blocks */
582
block = &sblock[0][0];
584
for (mb_index = 0; mb_index < 5; mb_index++) {
585
dv_calculate_mb_xy(s, work_chunk, mb_index, &mb_x, &mb_y);
587
/* idct_put'ting luminance */
588
if ((s->sys->pix_fmt == PIX_FMT_YUV420P) ||
589
(s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) ||
590
(s->sys->height >= 720 && mb_y != 134)) {
591
y_stride = (s->picture.linesize[0] << ((!is_field_mode[mb_index]) * log2_blocksize));
593
y_stride = (2 << log2_blocksize);
595
y_ptr = s->picture.data[0] + ((mb_y * s->picture.linesize[0] + mb_x) << log2_blocksize);
596
linesize = s->picture.linesize[0] << is_field_mode[mb_index];
597
mb[0] .idct_put(y_ptr , linesize, block + 0*64);
598
if (s->sys->video_stype == 4) { /* SD 422 */
599
mb[2].idct_put(y_ptr + (1 << log2_blocksize) , linesize, block + 2*64);
601
mb[1].idct_put(y_ptr + (1 << log2_blocksize) , linesize, block + 1*64);
602
mb[2].idct_put(y_ptr + y_stride, linesize, block + 2*64);
603
mb[3].idct_put(y_ptr + (1 << log2_blocksize) + y_stride, linesize, block + 3*64);
608
/* idct_put'ting chrominance */
609
c_offset = (((mb_y >> (s->sys->pix_fmt == PIX_FMT_YUV420P)) * s->picture.linesize[1] +
610
(mb_x >> ((s->sys->pix_fmt == PIX_FMT_YUV411P) ? 2 : 1))) << log2_blocksize);
611
for (j = 2; j; j--) {
612
uint8_t *c_ptr = s->picture.data[j] + c_offset;
613
if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
614
uint64_t aligned_pixels[64/8];
615
uint8_t *pixels = (uint8_t*)aligned_pixels;
616
uint8_t *c_ptr1, *ptr1;
618
mb->idct_put(pixels, 8, block);
619
for (y = 0; y < (1 << log2_blocksize); y++, c_ptr += s->picture.linesize[j], pixels += 8) {
620
ptr1 = pixels + (1 << (log2_blocksize - 1));
621
c_ptr1 = c_ptr + (s->picture.linesize[j] << log2_blocksize);
622
for (x = 0; x < (1 << (log2_blocksize - 1)); x++) {
623
c_ptr[x] = pixels[x];
629
y_stride = (mb_y == 134) ? (1 << log2_blocksize) :
630
s->picture.linesize[j] << ((!is_field_mode[mb_index]) * log2_blocksize);
631
linesize = s->picture.linesize[j] << is_field_mode[mb_index];
632
(mb++)-> idct_put(c_ptr , linesize, block); block += 64;
633
if (s->sys->bpm == 8) {
634
(mb++)->idct_put(c_ptr + y_stride, linesize, block); block += 64;
643
343
/* Converts run and level (where level != 0) pair into VLC, returning bit size */
644
344
static av_always_inline int dv_rl2vlc(int run, int level, int sign, uint32_t* vlc)
467
static const int dv_weight_bits = 18;
468
static const int dv_weight_88[64] = {
469
131072, 257107, 257107, 242189, 252167, 242189, 235923, 237536,
470
237536, 235923, 229376, 231390, 223754, 231390, 229376, 222935,
471
224969, 217965, 217965, 224969, 222935, 200636, 218652, 211916,
472
212325, 211916, 218652, 200636, 188995, 196781, 205965, 206433,
473
206433, 205965, 196781, 188995, 185364, 185364, 200636, 200704,
474
200636, 185364, 185364, 174609, 180568, 195068, 195068, 180568,
475
174609, 170091, 175557, 189591, 175557, 170091, 165371, 170627,
476
170627, 165371, 160727, 153560, 160727, 144651, 144651, 136258,
478
static const int dv_weight_248[64] = {
479
131072, 242189, 257107, 237536, 229376, 200636, 242189, 223754,
480
224969, 196781, 262144, 242189, 229376, 200636, 257107, 237536,
481
211916, 185364, 235923, 217965, 229376, 211916, 206433, 180568,
482
242189, 223754, 224969, 196781, 211916, 185364, 235923, 217965,
483
200704, 175557, 222935, 205965, 200636, 185364, 195068, 170627,
484
229376, 211916, 206433, 180568, 200704, 175557, 222935, 205965,
485
175557, 153560, 188995, 174609, 165371, 144651, 200636, 185364,
486
195068, 170627, 175557, 153560, 188995, 174609, 165371, 144651,
767
489
static av_always_inline int dv_init_enc_block(EncBlockInfo* bi, uint8_t *data, int linesize, DVVideoContext *s, int bias)
769
491
const int *weight;
1061
#if CONFIG_DVVIDEO_DECODER
1062
/* NOTE: exactly one frame must be given (120000 bytes for NTSC,
1063
144000 bytes for PAL - or twice those for 50Mbps) */
1064
static int dvvideo_decode_frame(AVCodecContext *avctx,
1065
void *data, int *data_size,
1068
const uint8_t *buf = avpkt->data;
1069
int buf_size = avpkt->size;
1070
DVVideoContext *s = avctx->priv_data;
1071
const uint8_t* vsc_pack;
1074
s->sys = avpriv_dv_frame_profile(s->sys, buf, buf_size);
1075
if (!s->sys || buf_size < s->sys->frame_size || dv_init_dynamic_tables(s->sys)) {
1076
av_log(avctx, AV_LOG_ERROR, "could not find dv frame profile\n");
1077
return -1; /* NOTE: we only accept several full frames */
1080
if (s->picture.data[0])
1081
avctx->release_buffer(avctx, &s->picture);
1083
s->picture.reference = 0;
1084
s->picture.key_frame = 1;
1085
s->picture.pict_type = AV_PICTURE_TYPE_I;
1086
avctx->pix_fmt = s->sys->pix_fmt;
1087
avctx->time_base = s->sys->time_base;
1088
avcodec_set_dimensions(avctx, s->sys->width, s->sys->height);
1089
if (avctx->get_buffer(avctx, &s->picture) < 0) {
1090
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1093
s->picture.interlaced_frame = 1;
1094
s->picture.top_field_first = 0;
1097
avctx->execute(avctx, dv_decode_video_segment, s->sys->work_chunks, NULL,
1098
dv_work_pool_size(s->sys), sizeof(DVwork_chunk));
1103
*data_size = sizeof(AVFrame);
1104
*(AVFrame*)data = s->picture;
1106
/* Determine the codec's sample_aspect ratio from the packet */
1107
vsc_pack = buf + 80*5 + 48 + 5;
1108
if ( *vsc_pack == dv_video_control ) {
1109
apt = buf[4] & 0x07;
1110
is16_9 = (vsc_pack && ((vsc_pack[2] & 0x07) == 0x02 || (!apt && (vsc_pack[2] & 0x07) == 0x07)));
1111
avctx->sample_aspect_ratio = s->sys->sar[is16_9];
1114
return s->sys->frame_size;
1116
#endif /* CONFIG_DVVIDEO_DECODER */
1119
783
static inline int dv_write_pack(enum dv_pack_type pack_id, DVVideoContext *c,
1242
static int dvvideo_encode_frame(AVCodecContext *c, uint8_t *buf, int buf_size,
941
static int dvvideo_encode_frame(AVCodecContext *c, AVPacket *pkt,
942
const AVFrame *frame, int *got_packet)
1245
944
DVVideoContext *s = c->priv_data;
1247
947
s->sys = avpriv_dv_codec_profile(c);
1248
if (!s->sys || buf_size < s->sys->frame_size || dv_init_dynamic_tables(s->sys))
948
if (!s->sys || ff_dv_init_dynamic_tables(s->sys))
950
if ((ret = ff_alloc_packet(pkt, s->sys->frame_size)) < 0) {
951
av_log(c, AV_LOG_ERROR, "Error getting output packet.\n");
1251
955
c->pix_fmt = s->sys->pix_fmt;
1252
s->picture = *((AVFrame *)data);
1253
957
s->picture.key_frame = 1;
1254
958
s->picture.pict_type = AV_PICTURE_TYPE_I;
1257
961
c->execute(c, dv_encode_video_segment, s->sys->work_chunks, NULL,
1258
962
dv_work_pool_size(s->sys), sizeof(DVwork_chunk));
1262
dv_format_frame(s, buf);
1264
return s->sys->frame_size;
1268
static int dvvideo_close(AVCodecContext *c)
1270
DVVideoContext *s = c->priv_data;
1272
if (s->picture.data[0])
1273
c->release_buffer(c, &s->picture);
966
dv_format_frame(s, pkt->data);
968
pkt->flags |= AV_PKT_FLAG_KEY;
1279
#if CONFIG_DVVIDEO_ENCODER
1280
974
AVCodec ff_dvvideo_encoder = {
1281
975
.name = "dvvideo",
1282
976
.type = AVMEDIA_TYPE_VIDEO,
1283
.id = CODEC_ID_DVVIDEO,
977
.id = AV_CODEC_ID_DVVIDEO,
1284
978
.priv_data_size = sizeof(DVVideoContext),
1285
979
.init = dvvideo_init_encoder,
1286
.encode = dvvideo_encode_frame,
1287
.capabilities = CODEC_CAP_SLICE_THREADS,
1288
.pix_fmts = (const enum PixelFormat[]) {PIX_FMT_YUV411P, PIX_FMT_YUV422P, PIX_FMT_YUV420P, PIX_FMT_NONE},
1289
.long_name = NULL_IF_CONFIG_SMALL("DV (Digital Video)"),
980
.encode2 = dvvideo_encode_frame,
981
.capabilities = CODEC_CAP_SLICE_THREADS,
982
.pix_fmts = (const enum AVPixelFormat[]) {
983
AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE
985
.long_name = NULL_IF_CONFIG_SMALL("DV (Digital Video)"),
1291
987
#endif // CONFIG_DVVIDEO_ENCODER
1293
#if CONFIG_DVVIDEO_DECODER
1294
AVCodec ff_dvvideo_decoder = {
1296
.type = AVMEDIA_TYPE_VIDEO,
1297
.id = CODEC_ID_DVVIDEO,
1298
.priv_data_size = sizeof(DVVideoContext),
1299
.init = dvvideo_init,
1300
.close = dvvideo_close,
1301
.decode = dvvideo_decode_frame,
1302
.capabilities = CODEC_CAP_DR1 | CODEC_CAP_SLICE_THREADS,
1304
.long_name = NULL_IF_CONFIG_SMALL("DV (Digital Video)"),