00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "dirac.h"
00029 #include "avcodec.h"
00030 #include "golomb.h"
00031 #include "mpeg12data.h"
00032
00033
00034 static const dirac_source_params dirac_source_parameters_defaults[] = {
00035 { 640, 480, 2, 0, 0, 1, 1, 640, 480, 0, 0, 1, 0 },
00036 { 176, 120, 2, 0, 0, 9, 2, 176, 120, 0, 0, 1, 1 },
00037 { 176, 144, 2, 0, 1, 10, 3, 176, 144, 0, 0, 1, 2 },
00038 { 352, 240, 2, 0, 0, 9, 2, 352, 240, 0, 0, 1, 1 },
00039 { 352, 288, 2, 0, 1, 10, 3, 352, 288, 0, 0, 1, 2 },
00040 { 704, 480, 2, 0, 0, 9, 2, 704, 480, 0, 0, 1, 1 },
00041 { 704, 576, 2, 0, 1, 10, 3, 704, 576, 0, 0, 1, 2 },
00042 { 720, 480, 1, 1, 0, 4, 2, 704, 480, 8, 0, 3, 1 },
00043 { 720, 576, 1, 1, 1, 3, 3, 704, 576, 8, 0, 3, 2 },
00044
00045 { 1280, 720, 1, 0, 1, 7, 1, 1280, 720, 0, 0, 3, 3 },
00046 { 1280, 720, 1, 0, 1, 6, 1, 1280, 720, 0, 0, 3, 3 },
00047 { 1920, 1080, 1, 1, 1, 4, 1, 1920, 1080, 0, 0, 3, 3 },
00048 { 1920, 1080, 1, 1, 1, 3, 1, 1920, 1080, 0, 0, 3, 3 },
00049 { 1920, 1080, 1, 0, 1, 7, 1, 1920, 1080, 0, 0, 3, 3 },
00050 { 1920, 1080, 1, 0, 1, 6, 1, 1920, 1080, 0, 0, 3, 3 },
00051 { 2048, 1080, 0, 0, 1, 2, 1, 2048, 1080, 0, 0, 4, 4 },
00052 { 4096, 2160, 0, 0, 1, 2, 1, 4096, 2160, 0, 0, 4, 4 },
00053
00054 { 3840, 2160, 1, 0, 1, 7, 1, 3840, 2160, 0, 0, 3, 3 },
00055 { 3840, 2160, 1, 0, 1, 6, 1, 3840, 2160, 0, 0, 3, 3 },
00056 { 7680, 4320, 1, 0, 1, 7, 1, 3840, 2160, 0, 0, 3, 3 },
00057 { 7680, 4320, 1, 0, 1, 6, 1, 3840, 2160, 0, 0, 3, 3 },
00058 };
00059
00060 static const AVRational dirac_preset_aspect_ratios[] = {
00061 {1, 1},
00062 {10, 11},
00063 {12, 11},
00064 {40, 33},
00065 {16, 11},
00066 {4, 3},
00067 };
00068
00069 static const AVRational dirac_frame_rate[] = {
00070 {15000, 1001},
00071 {25, 2},
00072 };
00073
00074 static const struct {
00075 uint8_t bitdepth;
00076 enum AVColorRange color_range;
00077 } pixel_range_presets[] = {
00078 {8, AVCOL_RANGE_JPEG},
00079 {8, AVCOL_RANGE_MPEG},
00080 {10, AVCOL_RANGE_MPEG},
00081 {12, AVCOL_RANGE_MPEG},
00082 };
00083
00084 static const enum AVColorPrimaries dirac_primaries[] = {
00085 AVCOL_PRI_BT709,
00086 AVCOL_PRI_SMPTE170M,
00087 AVCOL_PRI_BT470BG,
00088 };
00089
00090 static const struct {
00091 enum AVColorPrimaries color_primaries;
00092 enum AVColorSpace colorspace;
00093 enum AVColorTransferCharacteristic color_trc;
00094 } dirac_color_presets[] = {
00095 { AVCOL_PRI_BT709, AVCOL_SPC_BT709, AVCOL_TRC_BT709 },
00096 { AVCOL_PRI_SMPTE170M, AVCOL_SPC_BT470BG, AVCOL_TRC_BT709 },
00097 { AVCOL_PRI_BT470BG, AVCOL_SPC_BT470BG, AVCOL_TRC_BT709 },
00098 { AVCOL_PRI_BT709, AVCOL_SPC_BT709, AVCOL_TRC_BT709 },
00099 { AVCOL_PRI_BT709, AVCOL_SPC_BT709, AVCOL_TRC_UNSPECIFIED },
00100 };
00101
00102 static const enum PixelFormat dirac_pix_fmt[2][3] = {
00103 { PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV420P },
00104 { PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ420P },
00105 };
00106
00107 static int parse_source_parameters(AVCodecContext *avctx, GetBitContext *gb,
00108 dirac_source_params *source)
00109 {
00110 AVRational frame_rate = (AVRational){0,0};
00111 unsigned luma_depth = 8, luma_offset = 16;
00112 int idx;
00113
00114 if (get_bits1(gb)) {
00115 source->width = svq3_get_ue_golomb(gb);
00116 source->height = svq3_get_ue_golomb(gb);
00117 }
00118
00119
00120 if (get_bits1(gb))
00121 source->chroma_format = svq3_get_ue_golomb(gb);
00122 if (source->chroma_format > 2) {
00123 av_log(avctx, AV_LOG_ERROR, "Unknown chroma format %d\n",
00124 source->chroma_format);
00125 return -1;
00126 }
00127
00128 if (get_bits1(gb))
00129 source->interlaced = svq3_get_ue_golomb(gb);
00130 if (source->interlaced > 1)
00131 return -1;
00132
00133
00134 if (get_bits1(gb)) {
00135 source->frame_rate_index = svq3_get_ue_golomb(gb);
00136
00137 if (source->frame_rate_index > 10)
00138 return -1;
00139
00140 if (!source->frame_rate_index) {
00141 frame_rate.num = svq3_get_ue_golomb(gb);
00142 frame_rate.den = svq3_get_ue_golomb(gb);
00143 }
00144 }
00145 if (source->frame_rate_index > 0) {
00146 if (source->frame_rate_index <= 8)
00147 frame_rate = ff_frame_rate_tab[source->frame_rate_index];
00148 else
00149 frame_rate = dirac_frame_rate[source->frame_rate_index-9];
00150 }
00151 av_reduce(&avctx->time_base.num, &avctx->time_base.den,
00152 frame_rate.den, frame_rate.num, 1<<30);
00153
00154
00155 if (get_bits1(gb)) {
00156 source->aspect_ratio_index = svq3_get_ue_golomb(gb);
00157
00158 if (source->aspect_ratio_index > 6)
00159 return -1;
00160
00161 if (!source->aspect_ratio_index) {
00162 avctx->sample_aspect_ratio.num = svq3_get_ue_golomb(gb);
00163 avctx->sample_aspect_ratio.den = svq3_get_ue_golomb(gb);
00164 }
00165 }
00166 if (source->aspect_ratio_index > 0)
00167 avctx->sample_aspect_ratio =
00168 dirac_preset_aspect_ratios[source->aspect_ratio_index-1];
00169
00170 if (get_bits1(gb)) {
00171 source->clean_width = svq3_get_ue_golomb(gb);
00172 source->clean_height = svq3_get_ue_golomb(gb);
00173 source->clean_left_offset = svq3_get_ue_golomb(gb);
00174 source->clean_right_offset = svq3_get_ue_golomb(gb);
00175 }
00176
00177
00178 if (get_bits1(gb)) {
00179 source->pixel_range_index = svq3_get_ue_golomb(gb);
00180
00181 if (source->pixel_range_index > 4)
00182 return -1;
00183
00184
00185 if (!source->pixel_range_index) {
00186 luma_offset = svq3_get_ue_golomb(gb);
00187 luma_depth = av_log2(svq3_get_ue_golomb(gb))+1;
00188 svq3_get_ue_golomb(gb);
00189 svq3_get_ue_golomb(gb);
00190
00191 avctx->color_range = luma_offset ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
00192 }
00193 }
00194 if (source->pixel_range_index > 0) {
00195 idx = source->pixel_range_index-1;
00196 luma_depth = pixel_range_presets[idx].bitdepth;
00197 avctx->color_range = pixel_range_presets[idx].color_range;
00198 }
00199
00200 if (luma_depth > 8)
00201 av_log(avctx, AV_LOG_WARNING, "Bitdepth greater than 8");
00202
00203 avctx->pix_fmt = dirac_pix_fmt[!luma_offset][source->chroma_format];
00204
00205
00206 if (get_bits1(gb)) {
00207 idx = source->color_spec_index = svq3_get_ue_golomb(gb);
00208
00209 if (source->color_spec_index > 4)
00210 return -1;
00211
00212 avctx->color_primaries = dirac_color_presets[idx].color_primaries;
00213 avctx->colorspace = dirac_color_presets[idx].colorspace;
00214 avctx->color_trc = dirac_color_presets[idx].color_trc;
00215
00216 if (!source->color_spec_index) {
00217 if (get_bits1(gb)) {
00218 idx = svq3_get_ue_golomb(gb);
00219 if (idx < 3)
00220 avctx->color_primaries = dirac_primaries[idx];
00221 }
00222
00223 if (get_bits1(gb)) {
00224 idx = svq3_get_ue_golomb(gb);
00225 if (!idx)
00226 avctx->colorspace = AVCOL_SPC_BT709;
00227 else if (idx == 1)
00228 avctx->colorspace = AVCOL_SPC_BT470BG;
00229 }
00230
00231 if (get_bits1(gb) && !svq3_get_ue_golomb(gb))
00232 avctx->color_trc = AVCOL_TRC_BT709;
00233 }
00234 } else {
00235 idx = source->color_spec_index;
00236 avctx->color_primaries = dirac_color_presets[idx].color_primaries;
00237 avctx->colorspace = dirac_color_presets[idx].colorspace;
00238 avctx->color_trc = dirac_color_presets[idx].color_trc;
00239 }
00240
00241 return 0;
00242 }
00243
00244 int ff_dirac_parse_sequence_header(AVCodecContext *avctx, GetBitContext *gb,
00245 dirac_source_params *source)
00246 {
00247 unsigned version_major, version_minor;
00248 unsigned video_format, picture_coding_mode;
00249
00250 version_major = svq3_get_ue_golomb(gb);
00251 version_minor = svq3_get_ue_golomb(gb);
00252 avctx->profile = svq3_get_ue_golomb(gb);
00253 avctx->level = svq3_get_ue_golomb(gb);
00254 video_format = svq3_get_ue_golomb(gb);
00255
00256 if (version_major < 2)
00257 av_log(avctx, AV_LOG_WARNING, "Stream is old and may not work\n");
00258 else if (version_major > 2)
00259 av_log(avctx, AV_LOG_WARNING, "Stream may have unhandled features\n");
00260
00261 if (video_format > 20)
00262 return -1;
00263
00264
00265 *source = dirac_source_parameters_defaults[video_format];
00266
00267
00268 if (parse_source_parameters(avctx, gb, source))
00269 return -1;
00270
00271 if (avcodec_check_dimensions(avctx, source->width, source->height))
00272 return -1;
00273
00274 avcodec_set_dimensions(avctx, source->width, source->height);
00275
00276
00277 picture_coding_mode = svq3_get_ue_golomb(gb);
00278 if (picture_coding_mode != 0) {
00279 av_log(avctx, AV_LOG_ERROR, "Unsupported picture coding mode %d",
00280 picture_coding_mode);
00281 return -1;
00282 }
00283 return 0;
00284 }