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