• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/dirac.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007 Marco Gerards <marco@gnu.org>
00003  * Copyright (C) 2009 David Conrad
00004  * Copyright (C) 2011 Jordi Ortiz
00005  *
00006  * This file is part of FFmpeg.
00007  *
00008  * FFmpeg is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * FFmpeg is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with FFmpeg; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 /* defaults for source parameters */
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 
00064 static const AVRational dirac_preset_aspect_ratios[] = {
00065     {1, 1},
00066     {10, 11},
00067     {12, 11},
00068     {40, 33},
00069     {16, 11},
00070     {4, 3},
00071 };
00072 
00077 static const AVRational dirac_frame_rate[] = {
00078     {15000, 1001},
00079     {25, 2},
00080 };
00081 
00086 static const struct {
00087     uint8_t             bitdepth;
00088     enum AVColorRange   color_range;
00089 } pixel_range_presets[] = {
00090     {8,  AVCOL_RANGE_JPEG},
00091     {8,  AVCOL_RANGE_MPEG},
00092     {10, AVCOL_RANGE_MPEG},
00093     {12, AVCOL_RANGE_MPEG},
00094 };
00095 
00096 static const enum AVColorPrimaries dirac_primaries[] = {
00097     AVCOL_PRI_BT709,
00098     AVCOL_PRI_SMPTE170M,
00099     AVCOL_PRI_BT470BG,
00100 };
00101 
00102 static const struct {
00103     enum AVColorPrimaries color_primaries;
00104     enum AVColorSpace colorspace;
00105     enum AVColorTransferCharacteristic color_trc;
00106 } dirac_color_presets[] = {
00107     { AVCOL_PRI_BT709,     AVCOL_SPC_BT709,   AVCOL_TRC_BT709 },
00108     { AVCOL_PRI_SMPTE170M, AVCOL_SPC_BT470BG, AVCOL_TRC_BT709 },
00109     { AVCOL_PRI_BT470BG,   AVCOL_SPC_BT470BG, AVCOL_TRC_BT709 },
00110     { AVCOL_PRI_BT709,     AVCOL_SPC_BT709,   AVCOL_TRC_BT709 },
00111     { AVCOL_PRI_BT709,     AVCOL_SPC_BT709,   AVCOL_TRC_UNSPECIFIED /* DCinema */ },
00112 };
00113 
00118 static const enum PixelFormat dirac_pix_fmt[2][3] = {
00119     { PIX_FMT_YUV444P,  PIX_FMT_YUV422P,  PIX_FMT_YUV420P  },
00120     { PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ420P },
00121 };
00122 
00127 static int parse_source_parameters(AVCodecContext *avctx, GetBitContext *gb,
00128                                    dirac_source_params *source)
00129 {
00130     AVRational frame_rate = (AVRational){0,0};
00131     unsigned luma_depth = 8, luma_offset = 16;
00132     int idx;
00133 
00134     /* [DIRAC_STD] 10.3.2 Frame size. frame_size(video_params) */
00135     if (get_bits1(gb)) {                         /* [DIRAC_STD] custom_dimensions_flag */
00136         source->width  = svq3_get_ue_golomb(gb); /* [DIRAC_STD] FRAME_WIDTH            */
00137         source->height = svq3_get_ue_golomb(gb); /* [DIRAC_STD] FRAME_HEIGHT           */
00138     }
00139 
00140     /* [DIRAC_STD] 10.3.3 Chroma Sampling Format.
00141        chroma_sampling_format(video_params) */
00142     if (get_bits1(gb))                                  /* [DIRAC_STD] custom_chroma_format_flag */
00143         source->chroma_format = svq3_get_ue_golomb(gb); /*[DIRAC_STD] CHROMA_FORMAT_INDEX        */
00144     if (source->chroma_format > 2U) {
00145         av_log(avctx, AV_LOG_ERROR, "Unknown chroma format %d\n",
00146                source->chroma_format);
00147         return -1;
00148     }
00149 
00150     /* [DIRAC_STD] 10.3.4 Scan Format. scan_format(video_params) */
00151     if (get_bits1(gb))                               /* [DIRAC_STD] custom_scan_format_flag */
00152         source->interlaced = svq3_get_ue_golomb(gb); /* [DIRAC_STD] SOURCE_SAMPLING         */
00153     if (source->interlaced > 1U)
00154         return -1;
00155 
00156     /* [DIRAC_STD] 10.3.5 Frame Rate. frame_rate(video_params) */
00157     if (get_bits1(gb)) { /* [DIRAC_STD] custom_frame_rate_flag */
00158         source->frame_rate_index = svq3_get_ue_golomb(gb);
00159 
00160         if (source->frame_rate_index > 10U)
00161             return -1;
00162 
00163         if (!source->frame_rate_index){
00164             frame_rate.num = svq3_get_ue_golomb(gb); /* [DIRAC_STD] FRAME_RATE_NUMER */
00165             frame_rate.den = svq3_get_ue_golomb(gb); /* [DIRAC_STD] FRAME_RATE_DENOM */
00166         }
00167     }
00168     if (source->frame_rate_index > 0) { /* [DIRAC_STD] preset_frame_rate(video_params,index) */
00169         if (source->frame_rate_index <= 8)
00170             frame_rate = avpriv_frame_rate_tab[source->frame_rate_index];  /* [DIRAC_STD] Table 10.3 values 1-8  */
00171         else
00172             frame_rate = dirac_frame_rate[source->frame_rate_index-9]; /* [DIRAC_STD] Table 10.3 values 9-10 */
00173     }
00174     av_reduce(&avctx->time_base.num, &avctx->time_base.den,
00175               frame_rate.den, frame_rate.num, 1<<30);
00176 
00177     /* [DIRAC_STD] 10.3.6 Pixel Aspect Ratio. pixel_aspect_ratio(video_params) */
00178     if (get_bits1(gb)) { /* [DIRAC_STD] custom_pixel_aspect_ratio_flag */
00179         source->aspect_ratio_index = svq3_get_ue_golomb(gb); /* [DIRAC_STD] index */
00180 
00181         if (source->aspect_ratio_index > 6U)
00182             return -1;
00183 
00184         if (!source->aspect_ratio_index) {
00185             avctx->sample_aspect_ratio.num = svq3_get_ue_golomb(gb);
00186             avctx->sample_aspect_ratio.den = svq3_get_ue_golomb(gb);
00187         }
00188     }
00189     if (source->aspect_ratio_index > 0) /* [DIRAC_STD] Take value from Table 10.4 Available preset pixel aspect ratio values */
00190         avctx->sample_aspect_ratio =
00191             dirac_preset_aspect_ratios[source->aspect_ratio_index-1];
00192 
00193     /* [DIRAC_STD] 10.3.7 Clean area. clean_area(video_params) */
00194     if (get_bits1(gb)) { /* [DIRAC_STD] custom_clean_area_flag */
00195         source->clean_width        = svq3_get_ue_golomb(gb); /* [DIRAC_STD] CLEAN_WIDTH        */
00196         source->clean_height       = svq3_get_ue_golomb(gb); /* [DIRAC_STD] CLEAN_HEIGHT       */
00197         source->clean_left_offset  = svq3_get_ue_golomb(gb); /* [DIRAC_STD] CLEAN_LEFT_OFFSET  */
00198         source->clean_right_offset = svq3_get_ue_golomb(gb); /* [DIRAC_STD] CLEAN_RIGHT_OFFSET */
00199     }
00200 
00201     /*[DIRAC_STD] 10.3.8 Signal range. signal_range(video_params)
00202       WARNING: Some adaptation seemed to be done using the AVCOL_RANGE_MPEG/JPEG values */
00203     if (get_bits1(gb)) {                                    /*[DIRAC_STD] custom_signal_range_flag */
00204         source->pixel_range_index = svq3_get_ue_golomb(gb); /*[DIRAC_STD] index */
00205 
00206         if (source->pixel_range_index > 4U)
00207             return -1;
00208 
00209         /* This assumes either fullrange or MPEG levels only */
00210         if (!source->pixel_range_index) {
00211             luma_offset = svq3_get_ue_golomb(gb);
00212             luma_depth  = av_log2(svq3_get_ue_golomb(gb))+1;
00213             svq3_get_ue_golomb(gb); /* chroma offset @Jordi: Why are these two ignored? */
00214             svq3_get_ue_golomb(gb); /* chroma excursion */
00215 
00216             avctx->color_range = luma_offset ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
00217         }
00218     }
00219     if (source->pixel_range_index > 0) { /*[DIRAC_STD] Take values from Table 10.5 Available signal range presets */
00220         idx                = source->pixel_range_index-1;
00221         luma_depth         = pixel_range_presets[idx].bitdepth;
00222         avctx->color_range = pixel_range_presets[idx].color_range;
00223     }
00224 
00225     if (luma_depth > 8)
00226         av_log(avctx, AV_LOG_WARNING, "Bitdepth greater than 8");
00227 
00228     avctx->pix_fmt = dirac_pix_fmt[!luma_offset][source->chroma_format];
00229 
00230     /* [DIRAC_STD] 10.3.9 Colour specification. colour_spec(video_params) */
00231     if (get_bits1(gb)) { /* [DIRAC_STD] custom_colour_spec_flag */
00232         idx = source->color_spec_index = svq3_get_ue_golomb(gb); /* [DIRAC_STD] index */
00233 
00234         if (source->color_spec_index > 4U)
00235             return -1;
00236 
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         if (!source->color_spec_index) {
00242             /* [DIRAC_STD] 10.3.9.1 Color primaries */
00243             if (get_bits1(gb)) {
00244                 idx = svq3_get_ue_golomb(gb);
00245                 if (idx < 3U)
00246                     avctx->color_primaries = dirac_primaries[idx];
00247             }
00248             /* [DIRAC_STD] 10.3.9.2 Color matrix */
00249             if (get_bits1(gb)) {
00250                 idx = svq3_get_ue_golomb(gb);
00251                 if (!idx)
00252                     avctx->colorspace = AVCOL_SPC_BT709;
00253                 else if (idx == 1)
00254                     avctx->colorspace = AVCOL_SPC_BT470BG;
00255             }
00256             /* [DIRAC_STD] 10.3.9.3 Transfer function */
00257             if (get_bits1(gb) && !svq3_get_ue_golomb(gb))
00258                 avctx->color_trc = AVCOL_TRC_BT709;
00259         }
00260     } else {
00261         idx = source->color_spec_index;
00262         avctx->color_primaries = dirac_color_presets[idx].color_primaries;
00263         avctx->colorspace      = dirac_color_presets[idx].colorspace;
00264         avctx->color_trc       = dirac_color_presets[idx].color_trc;
00265     }
00266 
00267     return 0;
00268 }
00269 
00274 int avpriv_dirac_parse_sequence_header(AVCodecContext *avctx, GetBitContext *gb,
00275                                    dirac_source_params *source)
00276 {
00277     unsigned version_major;
00278     unsigned video_format, picture_coding_mode;
00279 
00280     /* [DIRAC_SPEC] 10.1 Parse Parameters. parse_parameters() */
00281     version_major  = svq3_get_ue_golomb(gb);
00282     svq3_get_ue_golomb(gb); /* version_minor */
00283     avctx->profile = svq3_get_ue_golomb(gb);
00284     avctx->level   = svq3_get_ue_golomb(gb);
00285     /* [DIRAC_SPEC] sequence_header() -> base_video_format as defined in
00286        10.2 Base Video Format, table 10.1 Dirac predefined video formats */
00287     video_format   = svq3_get_ue_golomb(gb);
00288 
00289     if (version_major < 2)
00290         av_log(avctx, AV_LOG_WARNING, "Stream is old and may not work\n");
00291     else if (version_major > 2)
00292         av_log(avctx, AV_LOG_WARNING, "Stream may have unhandled features\n");
00293 
00294     if (video_format > 20U)
00295         return -1;
00296 
00297     /* Fill in defaults for the source parameters. */
00298     *source = dirac_source_parameters_defaults[video_format];
00299 
00300     /*[DIRAC_STD] 10.3 Source Parameters
00301       Override the defaults. */
00302     if (parse_source_parameters(avctx, gb, source))
00303         return -1;
00304 
00305     if (av_image_check_size(source->width, source->height, 0, avctx))
00306         return -1;
00307 
00308     avcodec_set_dimensions(avctx, source->width, source->height);
00309 
00310     /*[DIRAC_STD] picture_coding_mode shall be 0 for fields and 1 for frames
00311       currently only used to signal field coding */
00312     picture_coding_mode = svq3_get_ue_golomb(gb);
00313     if (picture_coding_mode != 0) {
00314         av_log(avctx, AV_LOG_ERROR, "Unsupported picture coding mode %d",
00315                picture_coding_mode);
00316         return -1;
00317     }
00318     return 0;
00319 }
Generated on Fri Feb 1 2013 14:34:32 for FFmpeg by doxygen 1.7.1