comparison dv.c @ 3167:312b86bc4202 libavcodec

DVCPRO50 support. Patch by Daniel Maas dmaas at maasdigital dot com
author romansh
date Mon, 06 Mar 2006 08:54:33 +0000
parents 28a087ad41d1
children 1deaaa111fbc
comparison
equal deleted inserted replaced
3166:ab1273ffe275 3167:312b86bc4202
3 * Copyright (c) 2002 Fabrice Bellard. 3 * Copyright (c) 2002 Fabrice Bellard.
4 * Copyright (c) 2004 Roman Shaposhnik. 4 * Copyright (c) 2004 Roman Shaposhnik.
5 * 5 *
6 * DV encoder 6 * DV encoder
7 * Copyright (c) 2003 Roman Shaposhnik. 7 * Copyright (c) 2003 Roman Shaposhnik.
8 *
9 * 50 Mbps (DVCPRO50) support
10 * Copyright (c) 2006 Daniel Maas <dmaas@maasdigital.com>
8 * 11 *
9 * Many thanks to Dan Dennedy <dan@dennedy.org> for providing wealth 12 * Many thanks to Dan Dennedy <dan@dennedy.org> for providing wealth
10 * of DV technical info. 13 * of DV technical info.
11 * 14 *
12 * This library is free software; you can redistribute it and/or 15 * This library is free software; you can redistribute it and/or
49 void (*get_pixels)(DCTELEM *block, const uint8_t *pixels, int line_size); 52 void (*get_pixels)(DCTELEM *block, const uint8_t *pixels, int line_size);
50 void (*fdct[2])(DCTELEM *block); 53 void (*fdct[2])(DCTELEM *block);
51 void (*idct_put[2])(uint8_t *dest, int line_size, DCTELEM *block); 54 void (*idct_put[2])(uint8_t *dest, int line_size, DCTELEM *block);
52 } DVVideoContext; 55 } DVVideoContext;
53 56
54 /* MultiThreading - applies to entire DV codec, not just the avcontext */ 57 /* MultiThreading - dv_anchor applies to entire DV codec, not just the avcontext */
55 uint8_t** dv_anchor; 58 /* one element is needed for each video segment in a DV frame */
59 /* at most there are 2 DIF channels * 12 DIF sequences * 27 video segments (PAL 50Mbps) */
60 #define DV_ANCHOR_SIZE (2*12*27)
61
62 static void* dv_anchor[DV_ANCHOR_SIZE];
56 63
57 #define TEX_VLC_BITS 9 64 #define TEX_VLC_BITS 9
58 65
59 #ifdef DV_CODEC_TINY_TARGET 66 #ifdef DV_CODEC_TINY_TARGET
60 #define DV_VLC_MAP_RUN_SIZE 15 67 #define DV_VLC_MAP_RUN_SIZE 15
116 dv_vlc_map = av_mallocz_static(DV_VLC_MAP_LEV_SIZE*DV_VLC_MAP_RUN_SIZE*sizeof(struct dv_vlc_pair)); 123 dv_vlc_map = av_mallocz_static(DV_VLC_MAP_LEV_SIZE*DV_VLC_MAP_RUN_SIZE*sizeof(struct dv_vlc_pair));
117 if (!dv_vlc_map) 124 if (!dv_vlc_map)
118 return -ENOMEM; 125 return -ENOMEM;
119 126
120 /* dv_anchor lets each thread know its Id */ 127 /* dv_anchor lets each thread know its Id */
121 dv_anchor = av_malloc(12*27*sizeof(void*)); 128 for (i=0; i<DV_ANCHOR_SIZE; i++)
122 if (!dv_anchor) {
123 return -ENOMEM;
124 }
125 for (i=0; i<12*27; i++)
126 dv_anchor[i] = (void*)(size_t)i; 129 dv_anchor[i] = (void*)(size_t)i;
127 130
128 /* it's faster to include sign bit in a generic VLC parsing scheme */ 131 /* it's faster to include sign bit in a generic VLC parsing scheme */
129 for (i=0, j=0; i<NB_DV_VLC; i++, j++) { 132 for (i=0, j=0; i<NB_DV_VLC; i++, j++) {
130 new_dv_vlc_bits[j] = dv_vlc_bits[i]; 133 new_dv_vlc_bits[j] = dv_vlc_bits[i];
237 240
238 /* XXX: do it only for constant case */ 241 /* XXX: do it only for constant case */
239 dv_build_unquantize_tables(s, dsp.idct_permutation); 242 dv_build_unquantize_tables(s, dsp.idct_permutation);
240 243
241 /* FIXME: I really don't think this should be here */ 244 /* FIXME: I really don't think this should be here */
242 if (dv_codec_profile(avctx)) 245 s->sys = dv_codec_profile(avctx);
243 avctx->pix_fmt = dv_codec_profile(avctx)->pix_fmt; 246 if(!s->sys) {
247 av_log(avctx, AV_LOG_ERROR, "Cannot determine type of DV output stream\n");
248 return -EINVAL;
249 }
250 avctx->pix_fmt = s->sys->pix_fmt;
251 avctx->bit_rate = av_rescale(s->sys->frame_size * 8,
252 s->sys->frame_rate,
253 s->sys->frame_rate_base);
244 avctx->coded_frame = &s->picture; 254 avctx->coded_frame = &s->picture;
245 s->avctx= avctx; 255 s->avctx= avctx;
246 256
247 return 0; 257 return 0;
248 } 258 }
493 mb = mb_data; 503 mb = mb_data;
494 for(mb_index = 0; mb_index < 5; mb_index++) { 504 for(mb_index = 0; mb_index < 5; mb_index++) {
495 v = *mb_pos_ptr++; 505 v = *mb_pos_ptr++;
496 mb_x = v & 0xff; 506 mb_x = v & 0xff;
497 mb_y = v >> 8; 507 mb_y = v >> 8;
498 y_ptr = s->picture.data[0] + ((mb_y * s->picture.linesize[0] + mb_x)<<log2_blocksize); 508 if (s->sys->pix_fmt == PIX_FMT_YUV422P) {
499 if (s->sys->pix_fmt == PIX_FMT_YUV411P) 509 y_ptr = s->picture.data[0] + ((mb_y * s->picture.linesize[0] + (mb_x>>1))<<log2_blocksize);
500 c_offset = ((mb_y * s->picture.linesize[1] + (mb_x >> 2))<<log2_blocksize); 510 c_offset = ((mb_y * s->picture.linesize[1] + (mb_x >> 2))<<log2_blocksize);
501 else 511 } else { /* 4:1:1 or 4:2:0 */
502 c_offset = (((mb_y >> 1) * s->picture.linesize[1] + (mb_x >> 1))<<log2_blocksize); 512 y_ptr = s->picture.data[0] + ((mb_y * s->picture.linesize[0] + mb_x)<<log2_blocksize);
513 if (s->sys->pix_fmt == PIX_FMT_YUV411P)
514 c_offset = ((mb_y * s->picture.linesize[1] + (mb_x >> 2))<<log2_blocksize);
515 else /* 4:2:0 */
516 c_offset = (((mb_y >> 1) * s->picture.linesize[1] + (mb_x >> 1))<<log2_blocksize);
517 }
503 for(j = 0;j < 6; j++) { 518 for(j = 0;j < 6; j++) {
504 idct_put = s->idct_put[mb->dct_mode && log2_blocksize==3]; 519 idct_put = s->idct_put[mb->dct_mode && log2_blocksize==3];
505 if (j < 4) { 520 if (s->sys->pix_fmt == PIX_FMT_YUV422P) { /* 4:2:2 */
506 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) { 521 if (j == 0 || j == 2) {
507 /* NOTE: at end of line, the macroblock is handled as 420 */ 522 /* Y0 Y1 */
508 idct_put(y_ptr + (j<<log2_blocksize), s->picture.linesize[0], block); 523 idct_put(y_ptr + ((j >> 1)<<log2_blocksize),
509 } else {
510 idct_put(y_ptr + (((j & 1) + (j >> 1) * s->picture.linesize[0])<<log2_blocksize),
511 s->picture.linesize[0], block); 524 s->picture.linesize[0], block);
525 } else if(j > 3) {
526 /* Cr Cb */
527 idct_put(s->picture.data[6 - j] + c_offset,
528 s->picture.linesize[6 - j], block);
512 } 529 }
513 } else { 530 /* note: j=1 and j=3 are "dummy" blocks in 4:2:2 */
514 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) { 531 } else { /* 4:1:1 or 4:2:0 */
515 uint64_t aligned_pixels[64/8]; 532 if (j < 4) {
516 uint8_t *pixels= (uint8_t*)aligned_pixels; 533 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
517 uint8_t *c_ptr, *c_ptr1, *ptr, *ptr1; 534 /* NOTE: at end of line, the macroblock is handled as 420 */
518 int x, y, linesize; 535 idct_put(y_ptr + (j<<log2_blocksize), s->picture.linesize[0], block);
519 /* NOTE: at end of line, the macroblock is handled as 420 */ 536 } else {
520 idct_put(pixels, 8, block); 537 idct_put(y_ptr + (((j & 1) + (j >> 1) * s->picture.linesize[0])<<log2_blocksize),
521 linesize = s->picture.linesize[6 - j]; 538 s->picture.linesize[0], block);
522 c_ptr = s->picture.data[6 - j] + c_offset;
523 ptr = pixels;
524 for(y = 0;y < (1<<log2_blocksize); y++) {
525 ptr1= ptr + (1<<(log2_blocksize-1));
526 c_ptr1 = c_ptr + (linesize<<log2_blocksize);
527 for(x=0; x < (1<<(log2_blocksize-1)); x++){
528 c_ptr[x]= ptr[x]; c_ptr1[x]= ptr1[x];
529 }
530 c_ptr += linesize;
531 ptr += 8;
532 } 539 }
533 } else { 540 } else {
534 /* don't ask me why they inverted Cb and Cr ! */ 541 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
535 idct_put(s->picture.data[6 - j] + c_offset, 542 uint64_t aligned_pixels[64/8];
536 s->picture.linesize[6 - j], block); 543 uint8_t *pixels= (uint8_t*)aligned_pixels;
544 uint8_t *c_ptr, *c_ptr1, *ptr, *ptr1;
545 int x, y, linesize;
546 /* NOTE: at end of line, the macroblock is handled as 420 */
547 idct_put(pixels, 8, block);
548 linesize = s->picture.linesize[6 - j];
549 c_ptr = s->picture.data[6 - j] + c_offset;
550 ptr = pixels;
551 for(y = 0;y < (1<<log2_blocksize); y++) {
552 ptr1= ptr + (1<<(log2_blocksize-1));
553 c_ptr1 = c_ptr + (linesize<<log2_blocksize);
554 for(x=0; x < (1<<(log2_blocksize-1)); x++){
555 c_ptr[x]= ptr[x]; c_ptr1[x]= ptr1[x];
556 }
557 c_ptr += linesize;
558 ptr += 8;
559 }
560 } else {
561 /* don't ask me why they inverted Cb and Cr ! */
562 idct_put(s->picture.data[6 - j] + c_offset,
563 s->picture.linesize[6 - j], block);
564 }
537 } 565 }
538 } 566 }
539 block += 64; 567 block += 64;
540 mb++; 568 mb++;
541 } 569 }
843 pb = &pbs[0]; 871 pb = &pbs[0];
844 for(mb_index = 0; mb_index < 5; mb_index++) { 872 for(mb_index = 0; mb_index < 5; mb_index++) {
845 v = *mb_pos_ptr++; 873 v = *mb_pos_ptr++;
846 mb_x = v & 0xff; 874 mb_x = v & 0xff;
847 mb_y = v >> 8; 875 mb_y = v >> 8;
848 y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 8); 876 if (s->sys->pix_fmt == PIX_FMT_YUV422P) {
849 c_offset = (s->sys->pix_fmt == PIX_FMT_YUV411P) ? 877 y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 4);
850 ((mb_y * s->picture.linesize[1] * 8) + ((mb_x >> 2) * 8)) : 878 } else { /* 4:1:1 */
851 (((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8)); 879 y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 8);
880 }
881 if (s->sys->pix_fmt == PIX_FMT_YUV420P) {
882 c_offset = (((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8));
883 } else { /* 4:2:2 or 4:1:1 */
884 c_offset = ((mb_y * s->picture.linesize[1] * 8) + ((mb_x >> 2) * 8));
885 }
852 do_edge_wrap = 0; 886 do_edge_wrap = 0;
853 qnos[mb_index] = 15; /* No quantization */ 887 qnos[mb_index] = 15; /* No quantization */
854 ptr = dif + mb_index*80 + 4; 888 ptr = dif + mb_index*80 + 4;
855 for(j = 0;j < 6; j++) { 889 for(j = 0;j < 6; j++) {
856 if (j < 4) { /* Four Y blocks */ 890 int dummy = 0;
857 /* NOTE: at end of line, the macroblock is handled as 420 */ 891 if (s->sys->pix_fmt == PIX_FMT_YUV422P) { /* 4:2:2 */
858 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) { 892 if (j == 0 || j == 2) {
859 data = y_ptr + (j * 8); 893 /* Y0 Y1 */
894 data = y_ptr + ((j>>1) * 8);
895 linesize = s->picture.linesize[0];
896 } else if (j > 3) {
897 /* Cr Cb */
898 data = s->picture.data[6 - j] + c_offset;
899 linesize = s->picture.linesize[6 - j];
860 } else { 900 } else {
861 data = y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]); 901 /* j=1 and j=3 are "dummy" blocks, used for AC data only */
902 data = 0;
903 linesize = 0;
904 dummy = 1;
862 } 905 }
863 linesize = s->picture.linesize[0]; 906 } else { /* 4:1:1 or 4:2:0 */
864 } else { /* Cr and Cb blocks */ 907 if (j < 4) { /* Four Y blocks */
865 /* don't ask Fabrice why they inverted Cb and Cr ! */ 908 /* NOTE: at end of line, the macroblock is handled as 420 */
866 data = s->picture.data[6 - j] + c_offset; 909 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
867 linesize = s->picture.linesize[6 - j]; 910 data = y_ptr + (j * 8);
868 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) 911 } else {
869 do_edge_wrap = 1; 912 data = y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]);
913 }
914 linesize = s->picture.linesize[0];
915 } else { /* Cr and Cb blocks */
916 /* don't ask Fabrice why they inverted Cb and Cr ! */
917 data = s->picture.data[6 - j] + c_offset;
918 linesize = s->picture.linesize[6 - j];
919 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8))
920 do_edge_wrap = 1;
921 }
870 } 922 }
871 923
872 /* Everything is set up -- now just copy data -> DCT block */ 924 /* Everything is set up -- now just copy data -> DCT block */
873 if (do_edge_wrap) { /* Edge wrap copy: 4x16 -> 8x8 */ 925 if (do_edge_wrap) { /* Edge wrap copy: 4x16 -> 8x8 */
874 uint8_t* d; 926 uint8_t* d;
879 b[4] = d[0]; b[5] = d[1]; b[6] = d[2]; b[7] = d[3]; 931 b[4] = d[0]; b[5] = d[1]; b[6] = d[2]; b[7] = d[3];
880 data += linesize; 932 data += linesize;
881 b += 8; 933 b += 8;
882 } 934 }
883 } else { /* Simple copy: 8x8 -> 8x8 */ 935 } else { /* Simple copy: 8x8 -> 8x8 */
884 s->get_pixels(block, data, linesize); 936 if (!dummy)
937 s->get_pixels(block, data, linesize);
885 } 938 }
886 939
887 if(s->avctx->flags & CODEC_FLAG_INTERLACED_DCT) 940 if(s->avctx->flags & CODEC_FLAG_INTERLACED_DCT)
888 enc_blk->dct_mode = dv_guess_dct_mode(block); 941 enc_blk->dct_mode = dv_guess_dct_mode(block);
889 else 942 else
891 enc_blk->area_q[0] = enc_blk->area_q[1] = enc_blk->area_q[2] = enc_blk->area_q[3] = 0; 944 enc_blk->area_q[0] = enc_blk->area_q[1] = enc_blk->area_q[2] = enc_blk->area_q[3] = 0;
892 enc_blk->partial_bit_count = 0; 945 enc_blk->partial_bit_count = 0;
893 enc_blk->partial_bit_buffer = 0; 946 enc_blk->partial_bit_buffer = 0;
894 enc_blk->cur_ac = 0; 947 enc_blk->cur_ac = 0;
895 948
896 s->fdct[enc_blk->dct_mode](block); 949 if (dummy) {
950 /* We rely on the fact that encoding all zeros leads to an immediate EOB,
951 which is precisely what the spec calls for in the "dummy" blocks. */
952 memset(block, 0, sizeof(block));
953 } else {
954 s->fdct[enc_blk->dct_mode](block);
955 }
897 956
898 dv_set_class_number(block, enc_blk, 957 dv_set_class_number(block, enc_blk,
899 enc_blk->dct_mode ? ff_zigzag248_direct : ff_zigzag_direct, 958 enc_blk->dct_mode ? ff_zigzag248_direct : ff_zigzag_direct,
900 enc_blk->dct_mode ? dv_weight_248 : dv_weight_88, 959 enc_blk->dct_mode ? dv_weight_248 : dv_weight_88,
901 j/4); 960 j/4);
948 1007
949 static int dv_decode_mt(AVCodecContext *avctx, void* sl) 1008 static int dv_decode_mt(AVCodecContext *avctx, void* sl)
950 { 1009 {
951 DVVideoContext *s = avctx->priv_data; 1010 DVVideoContext *s = avctx->priv_data;
952 int slice = (size_t)sl; 1011 int slice = (size_t)sl;
953 dv_decode_video_segment(s, &s->buf[((slice/27)*6+(slice/3)+slice*5+7)*80], 1012
1013 /* which DIF channel is this? */
1014 int chan = slice / (s->sys->difseg_size * 27);
1015
1016 /* slice within the DIF channel */
1017 int chan_slice = slice % (s->sys->difseg_size * 27);
1018
1019 /* byte offset of this channel's data */
1020 int chan_offset = chan * s->sys->difseg_size * 150 * 80;
1021
1022 dv_decode_video_segment(s, &s->buf[((chan_slice/27)*6+(chan_slice/3)+chan_slice*5+7)*80 + chan_offset],
954 &s->sys->video_place[slice*5]); 1023 &s->sys->video_place[slice*5]);
955 return 0; 1024 return 0;
956 } 1025 }
957 1026
958 static int dv_encode_mt(AVCodecContext *avctx, void* sl) 1027 static int dv_encode_mt(AVCodecContext *avctx, void* sl)
959 { 1028 {
960 DVVideoContext *s = avctx->priv_data; 1029 DVVideoContext *s = avctx->priv_data;
961 int slice = (size_t)sl; 1030 int slice = (size_t)sl;
962 dv_encode_video_segment(s, &s->buf[((slice/27)*6+(slice/3)+slice*5+7)*80], 1031
1032 /* which DIF channel is this? */
1033 int chan = slice / (s->sys->difseg_size * 27);
1034
1035 /* slice within the DIF channel */
1036 int chan_slice = slice % (s->sys->difseg_size * 27);
1037
1038 /* byte offset of this channel's data */
1039 int chan_offset = chan * s->sys->difseg_size * 150 * 80;
1040
1041 dv_encode_video_segment(s, &s->buf[((chan_slice/27)*6+(chan_slice/3)+chan_slice*5+7)*80 + chan_offset],
963 &s->sys->video_place[slice*5]); 1042 &s->sys->video_place[slice*5]);
964 return 0; 1043 return 0;
965 } 1044 }
966 1045
967 /* NOTE: exactly one frame must be given (120000 bytes for NTSC, 1046 /* NOTE: exactly one frame must be given (120000 bytes for NTSC,
968 144000 bytes for PAL) */ 1047 144000 bytes for PAL - or twice those for 50Mbps) */
969 static int dvvideo_decode_frame(AVCodecContext *avctx, 1048 static int dvvideo_decode_frame(AVCodecContext *avctx,
970 void *data, int *data_size, 1049 void *data, int *data_size,
971 uint8_t *buf, int buf_size) 1050 uint8_t *buf, int buf_size)
972 { 1051 {
973 DVVideoContext *s = avctx->priv_data; 1052 DVVideoContext *s = avctx->priv_data;
991 s->picture.interlaced_frame = 1; 1070 s->picture.interlaced_frame = 1;
992 s->picture.top_field_first = 0; 1071 s->picture.top_field_first = 0;
993 1072
994 s->buf = buf; 1073 s->buf = buf;
995 avctx->execute(avctx, dv_decode_mt, (void**)&dv_anchor[0], NULL, 1074 avctx->execute(avctx, dv_decode_mt, (void**)&dv_anchor[0], NULL,
996 s->sys->difseg_size * 27); 1075 s->sys->n_difchan * s->sys->difseg_size * 27);
997 1076
998 emms_c(); 1077 emms_c();
999 1078
1000 /* return image */ 1079 /* return image */
1001 *data_size = sizeof(AVFrame); 1080 *data_size = sizeof(AVFrame);
1020 s->picture.key_frame = 1; 1099 s->picture.key_frame = 1;
1021 s->picture.pict_type = FF_I_TYPE; 1100 s->picture.pict_type = FF_I_TYPE;
1022 1101
1023 s->buf = buf; 1102 s->buf = buf;
1024 c->execute(c, dv_encode_mt, (void**)&dv_anchor[0], NULL, 1103 c->execute(c, dv_encode_mt, (void**)&dv_anchor[0], NULL,
1025 s->sys->difseg_size * 27); 1104 s->sys->n_difchan * s->sys->difseg_size * 27);
1026 1105
1027 emms_c(); 1106 emms_c();
1107
1108 /* Fill in just enough of the header for dv_frame_profile() to
1109 return the correct result, so that the frame can be decoded
1110 correctly. The rest of the metadata is filled in by the dvvideo
1111 avformat. (this should probably change so that encode_frame()
1112 fills in ALL of the metadata - e.g. for Quicktime-wrapped DV
1113 streams) */
1114
1115 /* NTSC/PAL format */
1116 buf[3] = s->sys->dsf ? 0x80 : 0x00;
1117
1118 /* 25Mbps or 50Mbps */
1119 buf[80*5 + 48 + 3] = (s->sys->pix_fmt == PIX_FMT_YUV422P) ? 0x4 : 0x0;
1120
1028 return s->sys->frame_size; 1121 return s->sys->frame_size;
1029 } 1122 }
1030 1123
1031 static int dvvideo_close(AVCodecContext *c) 1124 static int dvvideo_close(AVCodecContext *c)
1032 { 1125 {