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.

394 lines
15KB

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