dirac.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright (C) 2007 Marco Gerards <marco@gnu.org>
  3. * Copyright (C) 2009 David Conrad
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Dirac Decoder
  24. * @author Marco Gerards <marco@gnu.org>
  25. */
  26. #include "libavcore/imgutils.h"
  27. #include "dirac.h"
  28. #include "avcodec.h"
  29. #include "golomb.h"
  30. #include "mpeg12data.h"
  31. // defaults for source parameters
  32. static const dirac_source_params dirac_source_parameters_defaults[] = {
  33. { 640, 480, 2, 0, 0, 1, 1, 640, 480, 0, 0, 1, 0 },
  34. { 176, 120, 2, 0, 0, 9, 2, 176, 120, 0, 0, 1, 1 },
  35. { 176, 144, 2, 0, 1, 10, 3, 176, 144, 0, 0, 1, 2 },
  36. { 352, 240, 2, 0, 0, 9, 2, 352, 240, 0, 0, 1, 1 },
  37. { 352, 288, 2, 0, 1, 10, 3, 352, 288, 0, 0, 1, 2 },
  38. { 704, 480, 2, 0, 0, 9, 2, 704, 480, 0, 0, 1, 1 },
  39. { 704, 576, 2, 0, 1, 10, 3, 704, 576, 0, 0, 1, 2 },
  40. { 720, 480, 1, 1, 0, 4, 2, 704, 480, 8, 0, 3, 1 },
  41. { 720, 576, 1, 1, 1, 3, 3, 704, 576, 8, 0, 3, 2 },
  42. { 1280, 720, 1, 0, 1, 7, 1, 1280, 720, 0, 0, 3, 3 },
  43. { 1280, 720, 1, 0, 1, 6, 1, 1280, 720, 0, 0, 3, 3 },
  44. { 1920, 1080, 1, 1, 1, 4, 1, 1920, 1080, 0, 0, 3, 3 },
  45. { 1920, 1080, 1, 1, 1, 3, 1, 1920, 1080, 0, 0, 3, 3 },
  46. { 1920, 1080, 1, 0, 1, 7, 1, 1920, 1080, 0, 0, 3, 3 },
  47. { 1920, 1080, 1, 0, 1, 6, 1, 1920, 1080, 0, 0, 3, 3 },
  48. { 2048, 1080, 0, 0, 1, 2, 1, 2048, 1080, 0, 0, 4, 4 },
  49. { 4096, 2160, 0, 0, 1, 2, 1, 4096, 2160, 0, 0, 4, 4 },
  50. { 3840, 2160, 1, 0, 1, 7, 1, 3840, 2160, 0, 0, 3, 3 },
  51. { 3840, 2160, 1, 0, 1, 6, 1, 3840, 2160, 0, 0, 3, 3 },
  52. { 7680, 4320, 1, 0, 1, 7, 1, 3840, 2160, 0, 0, 3, 3 },
  53. { 7680, 4320, 1, 0, 1, 6, 1, 3840, 2160, 0, 0, 3, 3 },
  54. };
  55. static const AVRational dirac_preset_aspect_ratios[] = {
  56. {1, 1},
  57. {10, 11},
  58. {12, 11},
  59. {40, 33},
  60. {16, 11},
  61. {4, 3},
  62. };
  63. static const AVRational dirac_frame_rate[] = {
  64. {15000, 1001},
  65. {25, 2},
  66. };
  67. static const struct {
  68. uint8_t bitdepth;
  69. enum AVColorRange color_range;
  70. } pixel_range_presets[] = {
  71. {8, AVCOL_RANGE_JPEG},
  72. {8, AVCOL_RANGE_MPEG},
  73. {10, AVCOL_RANGE_MPEG},
  74. {12, AVCOL_RANGE_MPEG},
  75. };
  76. static const enum AVColorPrimaries dirac_primaries[] = {
  77. AVCOL_PRI_BT709,
  78. AVCOL_PRI_SMPTE170M,
  79. AVCOL_PRI_BT470BG,
  80. };
  81. static const struct {
  82. enum AVColorPrimaries color_primaries;
  83. enum AVColorSpace colorspace;
  84. enum AVColorTransferCharacteristic color_trc;
  85. } dirac_color_presets[] = {
  86. { AVCOL_PRI_BT709, AVCOL_SPC_BT709, AVCOL_TRC_BT709 },
  87. { AVCOL_PRI_SMPTE170M, AVCOL_SPC_BT470BG, AVCOL_TRC_BT709 },
  88. { AVCOL_PRI_BT470BG, AVCOL_SPC_BT470BG, AVCOL_TRC_BT709 },
  89. { AVCOL_PRI_BT709, AVCOL_SPC_BT709, AVCOL_TRC_BT709 },
  90. { AVCOL_PRI_BT709, AVCOL_SPC_BT709, AVCOL_TRC_UNSPECIFIED /* DCinema */ },
  91. };
  92. static const enum PixelFormat dirac_pix_fmt[2][3] = {
  93. { PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV420P },
  94. { PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ420P },
  95. };
  96. static int parse_source_parameters(AVCodecContext *avctx, GetBitContext *gb,
  97. dirac_source_params *source)
  98. {
  99. AVRational frame_rate = (AVRational){0,0};
  100. unsigned luma_depth = 8, luma_offset = 16;
  101. int idx;
  102. if (get_bits1(gb)) {
  103. source->width = svq3_get_ue_golomb(gb);
  104. source->height = svq3_get_ue_golomb(gb);
  105. }
  106. // chroma subsampling
  107. if (get_bits1(gb))
  108. source->chroma_format = svq3_get_ue_golomb(gb);
  109. if (source->chroma_format > 2) {
  110. av_log(avctx, AV_LOG_ERROR, "Unknown chroma format %d\n",
  111. source->chroma_format);
  112. return -1;
  113. }
  114. if (get_bits1(gb))
  115. source->interlaced = svq3_get_ue_golomb(gb);
  116. if (source->interlaced > 1)
  117. return -1;
  118. // frame rate
  119. if (get_bits1(gb)) {
  120. source->frame_rate_index = svq3_get_ue_golomb(gb);
  121. if (source->frame_rate_index > 10)
  122. return -1;
  123. if (!source->frame_rate_index) {
  124. frame_rate.num = svq3_get_ue_golomb(gb);
  125. frame_rate.den = svq3_get_ue_golomb(gb);
  126. }
  127. }
  128. if (source->frame_rate_index > 0) {
  129. if (source->frame_rate_index <= 8)
  130. frame_rate = ff_frame_rate_tab[source->frame_rate_index];
  131. else
  132. frame_rate = dirac_frame_rate[source->frame_rate_index-9];
  133. }
  134. av_reduce(&avctx->time_base.num, &avctx->time_base.den,
  135. frame_rate.den, frame_rate.num, 1<<30);
  136. // aspect ratio
  137. if (get_bits1(gb)) {
  138. source->aspect_ratio_index = svq3_get_ue_golomb(gb);
  139. if (source->aspect_ratio_index > 6)
  140. return -1;
  141. if (!source->aspect_ratio_index) {
  142. avctx->sample_aspect_ratio.num = svq3_get_ue_golomb(gb);
  143. avctx->sample_aspect_ratio.den = svq3_get_ue_golomb(gb);
  144. }
  145. }
  146. if (source->aspect_ratio_index > 0)
  147. avctx->sample_aspect_ratio =
  148. dirac_preset_aspect_ratios[source->aspect_ratio_index-1];
  149. if (get_bits1(gb)) {
  150. source->clean_width = svq3_get_ue_golomb(gb);
  151. source->clean_height = svq3_get_ue_golomb(gb);
  152. source->clean_left_offset = svq3_get_ue_golomb(gb);
  153. source->clean_right_offset = svq3_get_ue_golomb(gb);
  154. }
  155. // Override signal range.
  156. if (get_bits1(gb)) {
  157. source->pixel_range_index = svq3_get_ue_golomb(gb);
  158. if (source->pixel_range_index > 4)
  159. return -1;
  160. // This assumes either fullrange or MPEG levels only
  161. if (!source->pixel_range_index) {
  162. luma_offset = svq3_get_ue_golomb(gb);
  163. luma_depth = av_log2(svq3_get_ue_golomb(gb))+1;
  164. svq3_get_ue_golomb(gb); // chroma offset
  165. svq3_get_ue_golomb(gb); // chroma excursion
  166. avctx->color_range = luma_offset ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
  167. }
  168. }
  169. if (source->pixel_range_index > 0) {
  170. idx = source->pixel_range_index-1;
  171. luma_depth = pixel_range_presets[idx].bitdepth;
  172. avctx->color_range = pixel_range_presets[idx].color_range;
  173. }
  174. if (luma_depth > 8)
  175. av_log(avctx, AV_LOG_WARNING, "Bitdepth greater than 8");
  176. avctx->pix_fmt = dirac_pix_fmt[!luma_offset][source->chroma_format];
  177. // color spec
  178. if (get_bits1(gb)) {
  179. idx = source->color_spec_index = svq3_get_ue_golomb(gb);
  180. if (source->color_spec_index > 4)
  181. return -1;
  182. avctx->color_primaries = dirac_color_presets[idx].color_primaries;
  183. avctx->colorspace = dirac_color_presets[idx].colorspace;
  184. avctx->color_trc = dirac_color_presets[idx].color_trc;
  185. if (!source->color_spec_index) {
  186. if (get_bits1(gb)) {
  187. idx = svq3_get_ue_golomb(gb);
  188. if (idx < 3)
  189. avctx->color_primaries = dirac_primaries[idx];
  190. }
  191. if (get_bits1(gb)) {
  192. idx = svq3_get_ue_golomb(gb);
  193. if (!idx)
  194. avctx->colorspace = AVCOL_SPC_BT709;
  195. else if (idx == 1)
  196. avctx->colorspace = AVCOL_SPC_BT470BG;
  197. }
  198. if (get_bits1(gb) && !svq3_get_ue_golomb(gb))
  199. avctx->color_trc = AVCOL_TRC_BT709;
  200. }
  201. } else {
  202. idx = source->color_spec_index;
  203. avctx->color_primaries = dirac_color_presets[idx].color_primaries;
  204. avctx->colorspace = dirac_color_presets[idx].colorspace;
  205. avctx->color_trc = dirac_color_presets[idx].color_trc;
  206. }
  207. return 0;
  208. }
  209. int ff_dirac_parse_sequence_header(AVCodecContext *avctx, GetBitContext *gb,
  210. dirac_source_params *source)
  211. {
  212. unsigned version_major, version_minor;
  213. unsigned video_format, picture_coding_mode;
  214. version_major = svq3_get_ue_golomb(gb);
  215. version_minor = svq3_get_ue_golomb(gb);
  216. avctx->profile = svq3_get_ue_golomb(gb);
  217. avctx->level = svq3_get_ue_golomb(gb);
  218. video_format = svq3_get_ue_golomb(gb);
  219. if (version_major < 2)
  220. av_log(avctx, AV_LOG_WARNING, "Stream is old and may not work\n");
  221. else if (version_major > 2)
  222. av_log(avctx, AV_LOG_WARNING, "Stream may have unhandled features\n");
  223. if (video_format > 20)
  224. return -1;
  225. // Fill in defaults for the source parameters.
  226. *source = dirac_source_parameters_defaults[video_format];
  227. // Override the defaults.
  228. if (parse_source_parameters(avctx, gb, source))
  229. return -1;
  230. if (av_check_image_size(source->width, source->height, 0, avctx))
  231. return -1;
  232. avcodec_set_dimensions(avctx, source->width, source->height);
  233. // currently only used to signal field coding
  234. picture_coding_mode = svq3_get_ue_golomb(gb);
  235. if (picture_coding_mode != 0) {
  236. av_log(avctx, AV_LOG_ERROR, "Unsupported picture coding mode %d",
  237. picture_coding_mode);
  238. return -1;
  239. }
  240. return 0;
  241. }