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.

403 lines
15KB

  1. /*
  2. * Copyright (C) 2007 Marco Gerards <marco@gnu.org>
  3. * Copyright (C) 2009 David Conrad
  4. * Copyright (C) 2011 Jordi Ortiz
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * Dirac Decoder
  25. * @author Marco Gerards <marco@gnu.org>, David Conrad, Jordi Ortiz <nenjordi@gmail.com>
  26. */
  27. #include "libavutil/imgutils.h"
  28. #include "avcodec.h"
  29. #include "dirac.h"
  30. #include "golomb.h"
  31. #include "internal.h"
  32. #include "mpeg12data.h"
  33. #if CONFIG_DIRAC_PARSE
  34. typedef struct dirac_source_params {
  35. unsigned width;
  36. unsigned height;
  37. uint8_t chroma_format; ///< 0: 444 1: 422 2: 420
  38. uint8_t interlaced;
  39. uint8_t top_field_first;
  40. uint8_t frame_rate_index; ///< index into dirac_frame_rate[]
  41. uint8_t aspect_ratio_index; ///< index into dirac_aspect_ratio[]
  42. uint16_t clean_width;
  43. uint16_t clean_height;
  44. uint16_t clean_left_offset;
  45. uint16_t clean_right_offset;
  46. uint8_t pixel_range_index; ///< index into dirac_pixel_range_presets[]
  47. uint8_t color_spec_index; ///< index into dirac_color_spec_presets[]
  48. } dirac_source_params;
  49. /* defaults for source parameters */
  50. static const dirac_source_params dirac_source_parameters_defaults[] = {
  51. { 640, 480, 2, 0, 0, 1, 1, 640, 480, 0, 0, 1, 0 },
  52. { 176, 120, 2, 0, 0, 9, 2, 176, 120, 0, 0, 1, 1 },
  53. { 176, 144, 2, 0, 1, 10, 3, 176, 144, 0, 0, 1, 2 },
  54. { 352, 240, 2, 0, 0, 9, 2, 352, 240, 0, 0, 1, 1 },
  55. { 352, 288, 2, 0, 1, 10, 3, 352, 288, 0, 0, 1, 2 },
  56. { 704, 480, 2, 0, 0, 9, 2, 704, 480, 0, 0, 1, 1 },
  57. { 704, 576, 2, 0, 1, 10, 3, 704, 576, 0, 0, 1, 2 },
  58. { 720, 480, 1, 1, 0, 4, 2, 704, 480, 8, 0, 3, 1 },
  59. { 720, 576, 1, 1, 1, 3, 3, 704, 576, 8, 0, 3, 2 },
  60. { 1280, 720, 1, 0, 1, 7, 1, 1280, 720, 0, 0, 3, 3 },
  61. { 1280, 720, 1, 0, 1, 6, 1, 1280, 720, 0, 0, 3, 3 },
  62. { 1920, 1080, 1, 1, 1, 4, 1, 1920, 1080, 0, 0, 3, 3 },
  63. { 1920, 1080, 1, 1, 1, 3, 1, 1920, 1080, 0, 0, 3, 3 },
  64. { 1920, 1080, 1, 0, 1, 7, 1, 1920, 1080, 0, 0, 3, 3 },
  65. { 1920, 1080, 1, 0, 1, 6, 1, 1920, 1080, 0, 0, 3, 3 },
  66. { 2048, 1080, 0, 0, 1, 2, 1, 2048, 1080, 0, 0, 4, 4 },
  67. { 4096, 2160, 0, 0, 1, 2, 1, 4096, 2160, 0, 0, 4, 4 },
  68. { 3840, 2160, 1, 0, 1, 7, 1, 3840, 2160, 0, 0, 3, 3 },
  69. { 3840, 2160, 1, 0, 1, 6, 1, 3840, 2160, 0, 0, 3, 3 },
  70. { 7680, 4320, 1, 0, 1, 7, 1, 3840, 2160, 0, 0, 3, 3 },
  71. { 7680, 4320, 1, 0, 1, 6, 1, 3840, 2160, 0, 0, 3, 3 },
  72. };
  73. /* [DIRAC_STD] Table 10.4 - Available preset pixel aspect ratio values */
  74. static const AVRational dirac_preset_aspect_ratios[] = {
  75. { 1, 1 },
  76. { 10, 11 },
  77. { 12, 11 },
  78. { 40, 33 },
  79. { 16, 11 },
  80. { 4, 3 },
  81. };
  82. /* [DIRAC_STD] Values 9,10 of 10.3.5 Frame Rate.
  83. * Table 10.3 Available preset frame rate values
  84. */
  85. static const AVRational dirac_frame_rate[] = {
  86. { 15000, 1001 },
  87. { 25, 2 },
  88. };
  89. /* [DIRAC_STD] This should be equivalent to Table 10.5 Available signal
  90. * range presets */
  91. static const struct {
  92. uint8_t bitdepth;
  93. enum AVColorRange color_range;
  94. } pixel_range_presets[] = {
  95. { 8, AVCOL_RANGE_JPEG },
  96. { 8, AVCOL_RANGE_MPEG },
  97. { 10, AVCOL_RANGE_MPEG },
  98. { 12, AVCOL_RANGE_MPEG },
  99. };
  100. static const enum AVColorPrimaries dirac_primaries[] = {
  101. AVCOL_PRI_BT709,
  102. AVCOL_PRI_SMPTE170M,
  103. AVCOL_PRI_BT470BG,
  104. };
  105. static const struct {
  106. enum AVColorPrimaries color_primaries;
  107. enum AVColorSpace colorspace;
  108. enum AVColorTransferCharacteristic color_trc;
  109. } dirac_color_presets[] = {
  110. { AVCOL_PRI_BT709, AVCOL_SPC_BT709, AVCOL_TRC_BT709 },
  111. { AVCOL_PRI_SMPTE170M, AVCOL_SPC_BT470BG, AVCOL_TRC_BT709 },
  112. { AVCOL_PRI_BT470BG, AVCOL_SPC_BT470BG, AVCOL_TRC_BT709 },
  113. { AVCOL_PRI_BT709, AVCOL_SPC_BT709, AVCOL_TRC_BT709 },
  114. { AVCOL_PRI_BT709, AVCOL_SPC_BT709, AVCOL_TRC_UNSPECIFIED /* DCinema */ },
  115. };
  116. /* [DIRAC_STD] Table 10.2 Supported chroma sampling formats */
  117. static const enum AVPixelFormat dirac_pix_fmt[][3] = {
  118. {AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12},
  119. {AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12},
  120. {AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12},
  121. };
  122. /* [DIRAC_STD] 10.3 Parse Source Parameters.
  123. * source_parameters(base_video_format) */
  124. static int parse_source_parameters(AVDiracSeqHeader *dsh, GetBitContext *gb,
  125. void *log_ctx)
  126. {
  127. AVRational frame_rate = { 0, 0 };
  128. unsigned luma_depth = 8, luma_offset = 16;
  129. int idx;
  130. int chroma_x_shift, chroma_y_shift;
  131. /* [DIRAC_STD] 10.3.2 Frame size. frame_size(video_params) */
  132. /* [DIRAC_STD] custom_dimensions_flag */
  133. if (get_bits1(gb)) {
  134. dsh->width = svq3_get_ue_golomb(gb); /* [DIRAC_STD] FRAME_WIDTH */
  135. dsh->height = svq3_get_ue_golomb(gb); /* [DIRAC_STD] FRAME_HEIGHT */
  136. }
  137. /* [DIRAC_STD] 10.3.3 Chroma Sampling Format.
  138. * chroma_sampling_format(video_params) */
  139. /* [DIRAC_STD] custom_chroma_format_flag */
  140. if (get_bits1(gb))
  141. /* [DIRAC_STD] CHROMA_FORMAT_INDEX */
  142. dsh->chroma_format = svq3_get_ue_golomb(gb);
  143. if (dsh->chroma_format > 2U) {
  144. if (log_ctx)
  145. av_log(log_ctx, AV_LOG_ERROR, "Unknown chroma format %d\n",
  146. dsh->chroma_format);
  147. return AVERROR_INVALIDDATA;
  148. }
  149. /* [DIRAC_STD] 10.3.4 Scan Format. scan_format(video_params) */
  150. /* [DIRAC_STD] custom_scan_format_flag */
  151. if (get_bits1(gb))
  152. /* [DIRAC_STD] SOURCE_SAMPLING */
  153. dsh->interlaced = svq3_get_ue_golomb(gb);
  154. if (dsh->interlaced > 1U)
  155. return AVERROR_INVALIDDATA;
  156. /* [DIRAC_STD] 10.3.5 Frame Rate. frame_rate(video_params) */
  157. if (get_bits1(gb)) { /* [DIRAC_STD] custom_frame_rate_flag */
  158. dsh->frame_rate_index = svq3_get_ue_golomb(gb);
  159. if (dsh->frame_rate_index > 10U)
  160. return AVERROR_INVALIDDATA;
  161. if (!dsh->frame_rate_index) {
  162. /* [DIRAC_STD] FRAME_RATE_NUMER */
  163. frame_rate.num = svq3_get_ue_golomb(gb);
  164. /* [DIRAC_STD] FRAME_RATE_DENOM */
  165. frame_rate.den = svq3_get_ue_golomb(gb);
  166. }
  167. }
  168. /* [DIRAC_STD] preset_frame_rate(video_params, index) */
  169. if (dsh->frame_rate_index > 0) {
  170. if (dsh->frame_rate_index <= 8)
  171. frame_rate = ff_mpeg12_frame_rate_tab[dsh->frame_rate_index];
  172. else
  173. /* [DIRAC_STD] Table 10.3 values 9-10 */
  174. frame_rate = dirac_frame_rate[dsh->frame_rate_index - 9];
  175. }
  176. dsh->framerate = frame_rate;
  177. /* [DIRAC_STD] 10.3.6 Pixel Aspect Ratio.
  178. * pixel_aspect_ratio(video_params) */
  179. if (get_bits1(gb)) { /* [DIRAC_STD] custom_pixel_aspect_ratio_flag */
  180. /* [DIRAC_STD] index */
  181. dsh->aspect_ratio_index = svq3_get_ue_golomb(gb);
  182. if (dsh->aspect_ratio_index > 6U)
  183. return AVERROR_INVALIDDATA;
  184. if (!dsh->aspect_ratio_index) {
  185. dsh->sample_aspect_ratio.num = svq3_get_ue_golomb(gb);
  186. dsh->sample_aspect_ratio.den = svq3_get_ue_golomb(gb);
  187. }
  188. }
  189. /* [DIRAC_STD] Take value from Table 10.4 Available preset pixel
  190. * aspect ratio values */
  191. if (dsh->aspect_ratio_index > 0)
  192. dsh->sample_aspect_ratio =
  193. dirac_preset_aspect_ratios[dsh->aspect_ratio_index - 1];
  194. /* [DIRAC_STD] 10.3.7 Clean area. clean_area(video_params) */
  195. if (get_bits1(gb)) { /* [DIRAC_STD] custom_clean_area_flag */
  196. /* [DIRAC_STD] CLEAN_WIDTH */
  197. dsh->clean_width = svq3_get_ue_golomb(gb);
  198. /* [DIRAC_STD] CLEAN_HEIGHT */
  199. dsh->clean_height = svq3_get_ue_golomb(gb);
  200. /* [DIRAC_STD] CLEAN_LEFT_OFFSET */
  201. dsh->clean_left_offset = svq3_get_ue_golomb(gb);
  202. /* [DIRAC_STD] CLEAN_RIGHT_OFFSET */
  203. dsh->clean_right_offset = svq3_get_ue_golomb(gb);
  204. }
  205. /* [DIRAC_STD] 10.3.8 Signal range. signal_range(video_params)
  206. * WARNING: Some adaptation seems to be done using the
  207. * AVCOL_RANGE_MPEG/JPEG values */
  208. if (get_bits1(gb)) { /* [DIRAC_STD] custom_signal_range_flag */
  209. /* [DIRAC_STD] index */
  210. dsh->pixel_range_index = svq3_get_ue_golomb(gb);
  211. if (dsh->pixel_range_index > 4U)
  212. return AVERROR_INVALIDDATA;
  213. /* This assumes either fullrange or MPEG levels only */
  214. if (!dsh->pixel_range_index) {
  215. luma_offset = svq3_get_ue_golomb(gb);
  216. luma_depth = av_log2(svq3_get_ue_golomb(gb)) + 1;
  217. svq3_get_ue_golomb(gb); /* chroma offset */
  218. svq3_get_ue_golomb(gb); /* chroma excursion */
  219. dsh->color_range = luma_offset ? AVCOL_RANGE_MPEG
  220. : AVCOL_RANGE_JPEG;
  221. }
  222. }
  223. /* [DIRAC_STD] Table 10.5
  224. * Available signal range presets <--> pixel_range_presets */
  225. if (dsh->pixel_range_index > 0) {
  226. idx = dsh->pixel_range_index - 1;
  227. luma_depth = pixel_range_presets[idx].bitdepth;
  228. dsh->color_range = pixel_range_presets[idx].color_range;
  229. }
  230. dsh->bit_depth = luma_depth;
  231. /* Full range 8 bts uses the same pix_fmts as limited range 8 bits */
  232. dsh->pixel_range_index += dsh->pixel_range_index == 1;
  233. if (dsh->pixel_range_index < 2U)
  234. return AVERROR_INVALIDDATA;
  235. dsh->pix_fmt = dirac_pix_fmt[dsh->chroma_format][dsh->pixel_range_index-2];
  236. avcodec_get_chroma_sub_sample(dsh->pix_fmt, &chroma_x_shift, &chroma_y_shift);
  237. if ((dsh->width % (1<<chroma_x_shift)) || (dsh->height % (1<<chroma_y_shift))) {
  238. if (log_ctx)
  239. av_log(log_ctx, AV_LOG_ERROR, "Dimensions must be an integer multiple of the chroma subsampling\n");
  240. return AVERROR_INVALIDDATA;
  241. }
  242. /* [DIRAC_STD] 10.3.9 Colour specification. colour_spec(video_params) */
  243. if (get_bits1(gb)) { /* [DIRAC_STD] custom_colour_spec_flag */
  244. /* [DIRAC_STD] index */
  245. idx = dsh->color_spec_index = svq3_get_ue_golomb(gb);
  246. if (dsh->color_spec_index > 4U)
  247. return AVERROR_INVALIDDATA;
  248. dsh->color_primaries = dirac_color_presets[idx].color_primaries;
  249. dsh->colorspace = dirac_color_presets[idx].colorspace;
  250. dsh->color_trc = dirac_color_presets[idx].color_trc;
  251. if (!dsh->color_spec_index) {
  252. /* [DIRAC_STD] 10.3.9.1 Colour primaries */
  253. if (get_bits1(gb)) {
  254. idx = svq3_get_ue_golomb(gb);
  255. if (idx < 3U)
  256. dsh->color_primaries = dirac_primaries[idx];
  257. }
  258. /* [DIRAC_STD] 10.3.9.2 Colour matrix */
  259. if (get_bits1(gb)) {
  260. idx = svq3_get_ue_golomb(gb);
  261. if (!idx)
  262. dsh->colorspace = AVCOL_SPC_BT709;
  263. else if (idx == 1)
  264. dsh->colorspace = AVCOL_SPC_BT470BG;
  265. }
  266. /* [DIRAC_STD] 10.3.9.3 Transfer function */
  267. if (get_bits1(gb) && !svq3_get_ue_golomb(gb))
  268. dsh->color_trc = AVCOL_TRC_BT709;
  269. }
  270. } else {
  271. idx = dsh->color_spec_index;
  272. dsh->color_primaries = dirac_color_presets[idx].color_primaries;
  273. dsh->colorspace = dirac_color_presets[idx].colorspace;
  274. dsh->color_trc = dirac_color_presets[idx].color_trc;
  275. }
  276. return 0;
  277. }
  278. /* [DIRAC_STD] 10. Sequence Header. sequence_header() */
  279. int av_dirac_parse_sequence_header(AVDiracSeqHeader **pdsh,
  280. const uint8_t *buf, size_t buf_size,
  281. void *log_ctx)
  282. {
  283. AVDiracSeqHeader *dsh;
  284. GetBitContext gb;
  285. unsigned video_format, picture_coding_mode;
  286. int ret;
  287. dsh = av_mallocz(sizeof(*dsh));
  288. if (!dsh)
  289. return AVERROR(ENOMEM);
  290. ret = init_get_bits8(&gb, buf, buf_size);
  291. if (ret < 0)
  292. goto fail;
  293. /* [DIRAC_SPEC] 10.1 Parse Parameters. parse_parameters() */
  294. dsh->version.major = svq3_get_ue_golomb(&gb);
  295. dsh->version.minor = svq3_get_ue_golomb(&gb);
  296. dsh->profile = svq3_get_ue_golomb(&gb);
  297. dsh->level = svq3_get_ue_golomb(&gb);
  298. /* [DIRAC_SPEC] sequence_header() -> base_video_format as defined in
  299. * 10.2 Base Video Format, table 10.1 Dirac predefined video formats */
  300. video_format = svq3_get_ue_golomb(&gb);
  301. if (dsh->version.major < 2 && log_ctx)
  302. av_log(log_ctx, AV_LOG_WARNING, "Stream is old and may not work\n");
  303. else if (dsh->version.major > 2 && log_ctx)
  304. av_log(log_ctx, AV_LOG_WARNING, "Stream may have unhandled features\n");
  305. if (video_format > 20U)
  306. return AVERROR_INVALIDDATA;
  307. /* Fill in defaults for the source parameters. */
  308. dsh->width = dirac_source_parameters_defaults[video_format].width;
  309. dsh->height = dirac_source_parameters_defaults[video_format].height;
  310. dsh->chroma_format = dirac_source_parameters_defaults[video_format].chroma_format;
  311. dsh->interlaced = dirac_source_parameters_defaults[video_format].interlaced;
  312. dsh->top_field_first = dirac_source_parameters_defaults[video_format].top_field_first;
  313. dsh->frame_rate_index = dirac_source_parameters_defaults[video_format].frame_rate_index;
  314. dsh->aspect_ratio_index = dirac_source_parameters_defaults[video_format].aspect_ratio_index;
  315. dsh->clean_width = dirac_source_parameters_defaults[video_format].clean_width;
  316. dsh->clean_height = dirac_source_parameters_defaults[video_format].clean_height;
  317. dsh->clean_left_offset = dirac_source_parameters_defaults[video_format].clean_left_offset;
  318. dsh->clean_right_offset = dirac_source_parameters_defaults[video_format].clean_right_offset;
  319. dsh->pixel_range_index = dirac_source_parameters_defaults[video_format].pixel_range_index;
  320. dsh->color_spec_index = dirac_source_parameters_defaults[video_format].color_spec_index;
  321. /* [DIRAC_STD] 10.3 Source Parameters
  322. * Override the defaults. */
  323. ret = parse_source_parameters(dsh, &gb, log_ctx);
  324. if (ret < 0)
  325. goto fail;
  326. /* [DIRAC_STD] picture_coding_mode shall be 0 for fields and 1 for frames
  327. * currently only used to signal field coding */
  328. picture_coding_mode = svq3_get_ue_golomb(&gb);
  329. if (picture_coding_mode != 0) {
  330. if (log_ctx) {
  331. av_log(log_ctx, AV_LOG_ERROR, "Unsupported picture coding mode %d",
  332. picture_coding_mode);
  333. }
  334. ret = AVERROR_INVALIDDATA;
  335. goto fail;
  336. }
  337. *pdsh = dsh;
  338. return 0;
  339. fail:
  340. av_freep(&dsh);
  341. *pdsh = NULL;
  342. return ret;
  343. }
  344. #else
  345. int av_dirac_parse_sequence_header(AVDiracSeqHeader **pdsh,
  346. const uint8_t *buf, size_t buf_size,
  347. void *log_ctx)
  348. {
  349. return AVERROR(ENOSYS);
  350. }
  351. #endif