You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

285 lines
9.4KB

  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 "dirac.h"
  27. #include "avcodec.h"
  28. #include "golomb.h"
  29. #include "mpeg12data.h"
  30. // defaults for source parameters
  31. static const dirac_source_params dirac_source_parameters_defaults[] = {
  32. { 640, 480, 2, 0, 0, 1, 1, 640, 480, 0, 0, 1, 0 },
  33. { 176, 120, 2, 0, 0, 9, 2, 176, 120, 0, 0, 1, 1 },
  34. { 176, 144, 2, 0, 1, 10, 3, 176, 144, 0, 0, 1, 2 },
  35. { 352, 240, 2, 0, 0, 9, 2, 352, 240, 0, 0, 1, 1 },
  36. { 352, 288, 2, 0, 1, 10, 3, 352, 288, 0, 0, 1, 2 },
  37. { 704, 480, 2, 0, 0, 9, 2, 704, 480, 0, 0, 1, 1 },
  38. { 704, 576, 2, 0, 1, 10, 3, 704, 576, 0, 0, 1, 2 },
  39. { 720, 480, 1, 1, 0, 4, 2, 704, 480, 8, 0, 3, 1 },
  40. { 720, 576, 1, 1, 1, 3, 3, 704, 576, 8, 0, 3, 2 },
  41. { 1280, 720, 1, 0, 1, 7, 1, 1280, 720, 0, 0, 3, 3 },
  42. { 1280, 720, 1, 0, 1, 6, 1, 1280, 720, 0, 0, 3, 3 },
  43. { 1920, 1080, 1, 1, 1, 4, 1, 1920, 1080, 0, 0, 3, 3 },
  44. { 1920, 1080, 1, 1, 1, 3, 1, 1920, 1080, 0, 0, 3, 3 },
  45. { 1920, 1080, 1, 0, 1, 7, 1, 1920, 1080, 0, 0, 3, 3 },
  46. { 1920, 1080, 1, 0, 1, 6, 1, 1920, 1080, 0, 0, 3, 3 },
  47. { 2048, 1080, 0, 0, 1, 2, 1, 2048, 1080, 0, 0, 4, 4 },
  48. { 4096, 2160, 0, 0, 1, 2, 1, 4096, 2160, 0, 0, 4, 4 },
  49. { 3840, 2160, 1, 0, 1, 7, 1, 3840, 2160, 0, 0, 3, 3 },
  50. { 3840, 2160, 1, 0, 1, 6, 1, 3840, 2160, 0, 0, 3, 3 },
  51. { 7680, 4320, 1, 0, 1, 7, 1, 3840, 2160, 0, 0, 3, 3 },
  52. { 7680, 4320, 1, 0, 1, 6, 1, 3840, 2160, 0, 0, 3, 3 },
  53. };
  54. static const AVRational dirac_preset_aspect_ratios[] = {
  55. {1, 1},
  56. {10, 11},
  57. {12, 11},
  58. {40, 33},
  59. {16, 11},
  60. {4, 3},
  61. };
  62. static const AVRational dirac_frame_rate[] = {
  63. {15000, 1001},
  64. {25, 2},
  65. };
  66. static const struct {
  67. uint8_t bitdepth;
  68. enum AVColorRange color_range;
  69. } pixel_range_presets[] = {
  70. {8, AVCOL_RANGE_JPEG},
  71. {8, AVCOL_RANGE_MPEG},
  72. {10, AVCOL_RANGE_MPEG},
  73. {12, AVCOL_RANGE_MPEG},
  74. };
  75. static const enum AVColorPrimaries dirac_primaries[] = {
  76. AVCOL_PRI_BT709,
  77. AVCOL_PRI_SMPTE170M,
  78. AVCOL_PRI_BT470BG,
  79. };
  80. static const struct {
  81. enum AVColorPrimaries color_primaries;
  82. enum AVColorSpace colorspace;
  83. enum AVColorTransferCharacteristic color_trc;
  84. } dirac_color_presets[] = {
  85. { AVCOL_PRI_BT709, AVCOL_SPC_BT709, AVCOL_TRC_BT709 },
  86. { AVCOL_PRI_SMPTE170M, AVCOL_SPC_BT470BG, AVCOL_TRC_BT709 },
  87. { AVCOL_PRI_BT470BG, AVCOL_SPC_BT470BG, AVCOL_TRC_BT709 },
  88. { AVCOL_PRI_BT709, AVCOL_SPC_BT709, AVCOL_TRC_BT709 },
  89. { AVCOL_PRI_BT709, AVCOL_SPC_BT709, AVCOL_TRC_UNSPECIFIED /* DCinema */ },
  90. };
  91. static const enum PixelFormat dirac_pix_fmt[2][3] = {
  92. { PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV420P },
  93. { PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ420P },
  94. };
  95. static int parse_source_parameters(AVCodecContext *avctx, GetBitContext *gb,
  96. dirac_source_params *source)
  97. {
  98. AVRational frame_rate = (AVRational){0,0};
  99. unsigned luma_depth = 8, luma_offset = 16;
  100. int idx;
  101. if (get_bits1(gb)) {
  102. source->width = svq3_get_ue_golomb(gb);
  103. source->height = svq3_get_ue_golomb(gb);
  104. }
  105. // chroma subsampling
  106. if (get_bits1(gb))
  107. source->chroma_format = svq3_get_ue_golomb(gb);
  108. if (source->chroma_format > 2) {
  109. av_log(avctx, AV_LOG_ERROR, "Unknown chroma format %d\n",
  110. source->chroma_format);
  111. return -1;
  112. }
  113. if (get_bits1(gb))
  114. source->interlaced = svq3_get_ue_golomb(gb);
  115. if (source->interlaced > 1)
  116. return -1;
  117. // frame rate
  118. if (get_bits1(gb)) {
  119. source->frame_rate_index = svq3_get_ue_golomb(gb);
  120. if (source->frame_rate_index > 10)
  121. return -1;
  122. if (!source->frame_rate_index) {
  123. frame_rate.num = svq3_get_ue_golomb(gb);
  124. frame_rate.den = svq3_get_ue_golomb(gb);
  125. }
  126. }
  127. if (source->frame_rate_index > 0) {
  128. if (source->frame_rate_index <= 8)
  129. frame_rate = ff_frame_rate_tab[source->frame_rate_index];
  130. else
  131. frame_rate = dirac_frame_rate[source->frame_rate_index-9];
  132. }
  133. av_reduce(&avctx->time_base.num, &avctx->time_base.den,
  134. frame_rate.den, frame_rate.num, 1<<30);
  135. // aspect ratio
  136. if (get_bits1(gb)) {
  137. source->aspect_ratio_index = svq3_get_ue_golomb(gb);
  138. if (source->aspect_ratio_index > 6)
  139. return -1;
  140. if (!source->aspect_ratio_index) {
  141. avctx->sample_aspect_ratio.num = svq3_get_ue_golomb(gb);
  142. avctx->sample_aspect_ratio.den = svq3_get_ue_golomb(gb);
  143. }
  144. }
  145. if (source->aspect_ratio_index > 0)
  146. avctx->sample_aspect_ratio =
  147. dirac_preset_aspect_ratios[source->aspect_ratio_index-1];
  148. if (get_bits1(gb)) {
  149. source->clean_width = svq3_get_ue_golomb(gb);
  150. source->clean_height = svq3_get_ue_golomb(gb);
  151. source->clean_left_offset = svq3_get_ue_golomb(gb);
  152. source->clean_right_offset = svq3_get_ue_golomb(gb);
  153. }
  154. // Override signal range.
  155. if (get_bits1(gb)) {
  156. source->pixel_range_index = svq3_get_ue_golomb(gb);
  157. if (source->pixel_range_index > 4)
  158. return -1;
  159. // This assumes either fullrange or MPEG levels only
  160. if (!source->pixel_range_index) {
  161. luma_offset = svq3_get_ue_golomb(gb);
  162. luma_depth = av_log2(svq3_get_ue_golomb(gb))+1;
  163. svq3_get_ue_golomb(gb); // chroma offset
  164. svq3_get_ue_golomb(gb); // chroma excursion
  165. avctx->color_range = luma_offset ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
  166. }
  167. }
  168. if (source->pixel_range_index > 0) {
  169. idx = source->pixel_range_index-1;
  170. luma_depth = pixel_range_presets[idx].bitdepth;
  171. avctx->color_range = pixel_range_presets[idx].color_range;
  172. }
  173. if (luma_depth > 8)
  174. av_log(avctx, AV_LOG_WARNING, "Bitdepth greater than 8");
  175. avctx->pix_fmt = dirac_pix_fmt[!luma_offset][source->chroma_format];
  176. // color spec
  177. if (get_bits1(gb)) {
  178. idx = source->color_spec_index = svq3_get_ue_golomb(gb);
  179. if (source->color_spec_index > 4)
  180. return -1;
  181. avctx->color_primaries = dirac_color_presets[idx].color_primaries;
  182. avctx->colorspace = dirac_color_presets[idx].colorspace;
  183. avctx->color_trc = dirac_color_presets[idx].color_trc;
  184. if (!source->color_spec_index) {
  185. if (get_bits1(gb)) {
  186. idx = svq3_get_ue_golomb(gb);
  187. if (idx < 3)
  188. avctx->color_primaries = dirac_primaries[idx];
  189. }
  190. if (get_bits1(gb)) {
  191. idx = svq3_get_ue_golomb(gb);
  192. if (!idx)
  193. avctx->colorspace = AVCOL_SPC_BT709;
  194. else if (idx == 1)
  195. avctx->colorspace = AVCOL_SPC_BT470BG;
  196. }
  197. if (get_bits1(gb) && !svq3_get_ue_golomb(gb))
  198. avctx->color_trc = AVCOL_TRC_BT709;
  199. }
  200. } else {
  201. idx = source->color_spec_index;
  202. avctx->color_primaries = dirac_color_presets[idx].color_primaries;
  203. avctx->colorspace = dirac_color_presets[idx].colorspace;
  204. avctx->color_trc = dirac_color_presets[idx].color_trc;
  205. }
  206. return 0;
  207. }
  208. int ff_dirac_parse_sequence_header(AVCodecContext *avctx, GetBitContext *gb,
  209. dirac_source_params *source)
  210. {
  211. unsigned version_major, version_minor;
  212. unsigned video_format, picture_coding_mode;
  213. version_major = svq3_get_ue_golomb(gb);
  214. version_minor = svq3_get_ue_golomb(gb);
  215. avctx->profile = svq3_get_ue_golomb(gb);
  216. avctx->level = svq3_get_ue_golomb(gb);
  217. video_format = svq3_get_ue_golomb(gb);
  218. if (version_major < 2)
  219. av_log(avctx, AV_LOG_WARNING, "Stream is old and may not work\n");
  220. else if (version_major > 2)
  221. av_log(avctx, AV_LOG_WARNING, "Stream may have unhandled features\n");
  222. if (video_format > 20)
  223. return -1;
  224. // Fill in defaults for the source parameters.
  225. *source = dirac_source_parameters_defaults[video_format];
  226. // Override the defaults.
  227. if (parse_source_parameters(avctx, gb, source))
  228. return -1;
  229. if (avcodec_check_dimensions(avctx, source->width, source->height))
  230. return -1;
  231. avcodec_set_dimensions(avctx, source->width, source->height);
  232. // currently only used to signal field coding
  233. picture_coding_mode = svq3_get_ue_golomb(gb);
  234. if (picture_coding_mode != 0) {
  235. av_log(avctx, AV_LOG_ERROR, "Unsupported picture coding mode %d",
  236. picture_coding_mode);
  237. return -1;
  238. }
  239. return 0;
  240. }