Libav 0.7.1
|
00001 /* 00002 * Copyright (C) 2007 Marco Gerards <marco@gnu.org> 00003 * Copyright (C) 2009 David Conrad 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * Libav is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00028 #include "libavutil/imgutils.h" 00029 #include "dirac.h" 00030 #include "avcodec.h" 00031 #include "golomb.h" 00032 #include "mpeg12data.h" 00033 00034 // defaults for source parameters 00035 static const dirac_source_params dirac_source_parameters_defaults[] = { 00036 { 640, 480, 2, 0, 0, 1, 1, 640, 480, 0, 0, 1, 0 }, 00037 { 176, 120, 2, 0, 0, 9, 2, 176, 120, 0, 0, 1, 1 }, 00038 { 176, 144, 2, 0, 1, 10, 3, 176, 144, 0, 0, 1, 2 }, 00039 { 352, 240, 2, 0, 0, 9, 2, 352, 240, 0, 0, 1, 1 }, 00040 { 352, 288, 2, 0, 1, 10, 3, 352, 288, 0, 0, 1, 2 }, 00041 { 704, 480, 2, 0, 0, 9, 2, 704, 480, 0, 0, 1, 1 }, 00042 { 704, 576, 2, 0, 1, 10, 3, 704, 576, 0, 0, 1, 2 }, 00043 { 720, 480, 1, 1, 0, 4, 2, 704, 480, 8, 0, 3, 1 }, 00044 { 720, 576, 1, 1, 1, 3, 3, 704, 576, 8, 0, 3, 2 }, 00045 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 00055 { 3840, 2160, 1, 0, 1, 7, 1, 3840, 2160, 0, 0, 3, 3 }, 00056 { 3840, 2160, 1, 0, 1, 6, 1, 3840, 2160, 0, 0, 3, 3 }, 00057 { 7680, 4320, 1, 0, 1, 7, 1, 3840, 2160, 0, 0, 3, 3 }, 00058 { 7680, 4320, 1, 0, 1, 6, 1, 3840, 2160, 0, 0, 3, 3 }, 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 static const AVRational dirac_frame_rate[] = { 00071 {15000, 1001}, 00072 {25, 2}, 00073 }; 00074 00075 static const struct { 00076 uint8_t bitdepth; 00077 enum AVColorRange color_range; 00078 } pixel_range_presets[] = { 00079 {8, AVCOL_RANGE_JPEG}, 00080 {8, AVCOL_RANGE_MPEG}, 00081 {10, AVCOL_RANGE_MPEG}, 00082 {12, AVCOL_RANGE_MPEG}, 00083 }; 00084 00085 static const enum AVColorPrimaries dirac_primaries[] = { 00086 AVCOL_PRI_BT709, 00087 AVCOL_PRI_SMPTE170M, 00088 AVCOL_PRI_BT470BG, 00089 }; 00090 00091 static const struct { 00092 enum AVColorPrimaries color_primaries; 00093 enum AVColorSpace colorspace; 00094 enum AVColorTransferCharacteristic color_trc; 00095 } dirac_color_presets[] = { 00096 { AVCOL_PRI_BT709, AVCOL_SPC_BT709, AVCOL_TRC_BT709 }, 00097 { AVCOL_PRI_SMPTE170M, AVCOL_SPC_BT470BG, AVCOL_TRC_BT709 }, 00098 { AVCOL_PRI_BT470BG, AVCOL_SPC_BT470BG, AVCOL_TRC_BT709 }, 00099 { AVCOL_PRI_BT709, AVCOL_SPC_BT709, AVCOL_TRC_BT709 }, 00100 { AVCOL_PRI_BT709, AVCOL_SPC_BT709, AVCOL_TRC_UNSPECIFIED /* DCinema */ }, 00101 }; 00102 00103 static const enum PixelFormat dirac_pix_fmt[2][3] = { 00104 { PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV420P }, 00105 { PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ420P }, 00106 }; 00107 00108 static int parse_source_parameters(AVCodecContext *avctx, GetBitContext *gb, 00109 dirac_source_params *source) 00110 { 00111 AVRational frame_rate = (AVRational){0,0}; 00112 unsigned luma_depth = 8, luma_offset = 16; 00113 int idx; 00114 00115 if (get_bits1(gb)) { 00116 source->width = svq3_get_ue_golomb(gb); 00117 source->height = svq3_get_ue_golomb(gb); 00118 } 00119 00120 // chroma subsampling 00121 if (get_bits1(gb)) 00122 source->chroma_format = svq3_get_ue_golomb(gb); 00123 if (source->chroma_format > 2) { 00124 av_log(avctx, AV_LOG_ERROR, "Unknown chroma format %d\n", 00125 source->chroma_format); 00126 return -1; 00127 } 00128 00129 if (get_bits1(gb)) 00130 source->interlaced = svq3_get_ue_golomb(gb); 00131 if (source->interlaced > 1) 00132 return -1; 00133 00134 // frame rate 00135 if (get_bits1(gb)) { 00136 source->frame_rate_index = svq3_get_ue_golomb(gb); 00137 00138 if (source->frame_rate_index > 10) 00139 return -1; 00140 00141 if (!source->frame_rate_index) { 00142 frame_rate.num = svq3_get_ue_golomb(gb); 00143 frame_rate.den = svq3_get_ue_golomb(gb); 00144 } 00145 } 00146 if (source->frame_rate_index > 0) { 00147 if (source->frame_rate_index <= 8) 00148 frame_rate = ff_frame_rate_tab[source->frame_rate_index]; 00149 else 00150 frame_rate = dirac_frame_rate[source->frame_rate_index-9]; 00151 } 00152 av_reduce(&avctx->time_base.num, &avctx->time_base.den, 00153 frame_rate.den, frame_rate.num, 1<<30); 00154 00155 // aspect ratio 00156 if (get_bits1(gb)) { 00157 source->aspect_ratio_index = svq3_get_ue_golomb(gb); 00158 00159 if (source->aspect_ratio_index > 6) 00160 return -1; 00161 00162 if (!source->aspect_ratio_index) { 00163 avctx->sample_aspect_ratio.num = svq3_get_ue_golomb(gb); 00164 avctx->sample_aspect_ratio.den = svq3_get_ue_golomb(gb); 00165 } 00166 } 00167 if (source->aspect_ratio_index > 0) 00168 avctx->sample_aspect_ratio = 00169 dirac_preset_aspect_ratios[source->aspect_ratio_index-1]; 00170 00171 if (get_bits1(gb)) { 00172 source->clean_width = svq3_get_ue_golomb(gb); 00173 source->clean_height = svq3_get_ue_golomb(gb); 00174 source->clean_left_offset = svq3_get_ue_golomb(gb); 00175 source->clean_right_offset = svq3_get_ue_golomb(gb); 00176 } 00177 00178 // Override signal range. 00179 if (get_bits1(gb)) { 00180 source->pixel_range_index = svq3_get_ue_golomb(gb); 00181 00182 if (source->pixel_range_index > 4) 00183 return -1; 00184 00185 // This assumes either fullrange or MPEG levels only 00186 if (!source->pixel_range_index) { 00187 luma_offset = svq3_get_ue_golomb(gb); 00188 luma_depth = av_log2(svq3_get_ue_golomb(gb))+1; 00189 svq3_get_ue_golomb(gb); // chroma offset 00190 svq3_get_ue_golomb(gb); // chroma excursion 00191 00192 avctx->color_range = luma_offset ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG; 00193 } 00194 } 00195 if (source->pixel_range_index > 0) { 00196 idx = source->pixel_range_index-1; 00197 luma_depth = pixel_range_presets[idx].bitdepth; 00198 avctx->color_range = pixel_range_presets[idx].color_range; 00199 } 00200 00201 if (luma_depth > 8) 00202 av_log(avctx, AV_LOG_WARNING, "Bitdepth greater than 8"); 00203 00204 avctx->pix_fmt = dirac_pix_fmt[!luma_offset][source->chroma_format]; 00205 00206 // color spec 00207 if (get_bits1(gb)) { 00208 idx = source->color_spec_index = svq3_get_ue_golomb(gb); 00209 00210 if (source->color_spec_index > 4) 00211 return -1; 00212 00213 avctx->color_primaries = dirac_color_presets[idx].color_primaries; 00214 avctx->colorspace = dirac_color_presets[idx].colorspace; 00215 avctx->color_trc = dirac_color_presets[idx].color_trc; 00216 00217 if (!source->color_spec_index) { 00218 if (get_bits1(gb)) { 00219 idx = svq3_get_ue_golomb(gb); 00220 if (idx < 3) 00221 avctx->color_primaries = dirac_primaries[idx]; 00222 } 00223 00224 if (get_bits1(gb)) { 00225 idx = svq3_get_ue_golomb(gb); 00226 if (!idx) 00227 avctx->colorspace = AVCOL_SPC_BT709; 00228 else if (idx == 1) 00229 avctx->colorspace = AVCOL_SPC_BT470BG; 00230 } 00231 00232 if (get_bits1(gb) && !svq3_get_ue_golomb(gb)) 00233 avctx->color_trc = AVCOL_TRC_BT709; 00234 } 00235 } else { 00236 idx = source->color_spec_index; 00237 avctx->color_primaries = dirac_color_presets[idx].color_primaries; 00238 avctx->colorspace = dirac_color_presets[idx].colorspace; 00239 avctx->color_trc = dirac_color_presets[idx].color_trc; 00240 } 00241 00242 return 0; 00243 } 00244 00245 int ff_dirac_parse_sequence_header(AVCodecContext *avctx, GetBitContext *gb, 00246 dirac_source_params *source) 00247 { 00248 unsigned version_major; 00249 unsigned video_format, picture_coding_mode; 00250 00251 version_major = svq3_get_ue_golomb(gb); 00252 svq3_get_ue_golomb(gb); /* version_minor */ 00253 avctx->profile = svq3_get_ue_golomb(gb); 00254 avctx->level = svq3_get_ue_golomb(gb); 00255 video_format = svq3_get_ue_golomb(gb); 00256 00257 if (version_major < 2) 00258 av_log(avctx, AV_LOG_WARNING, "Stream is old and may not work\n"); 00259 else if (version_major > 2) 00260 av_log(avctx, AV_LOG_WARNING, "Stream may have unhandled features\n"); 00261 00262 if (video_format > 20) 00263 return -1; 00264 00265 // Fill in defaults for the source parameters. 00266 *source = dirac_source_parameters_defaults[video_format]; 00267 00268 // Override the defaults. 00269 if (parse_source_parameters(avctx, gb, source)) 00270 return -1; 00271 00272 if (av_image_check_size(source->width, source->height, 0, avctx)) 00273 return -1; 00274 00275 avcodec_set_dimensions(avctx, source->width, source->height); 00276 00277 // currently only used to signal field coding 00278 picture_coding_mode = svq3_get_ue_golomb(gb); 00279 if (picture_coding_mode != 0) { 00280 av_log(avctx, AV_LOG_ERROR, "Unsupported picture coding mode %d", 00281 picture_coding_mode); 00282 return -1; 00283 } 00284 return 0; 00285 }