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.

395 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 "bitstream.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 + luma Offset */
  117. static const enum AVPixelFormat dirac_pix_fmt[2][3] = {
  118. { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P },
  119. { AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P },
  120. };
  121. /* [DIRAC_STD] 10.3 Parse Source Parameters.
  122. * source_parameters(base_video_format) */
  123. static int parse_source_parameters(AVDiracSeqHeader *dsh, BitstreamContext *bc,
  124. void *log_ctx)
  125. {
  126. AVRational frame_rate = { 0, 0 };
  127. unsigned luma_depth = 8, luma_offset = 16;
  128. int idx;
  129. /* [DIRAC_STD] 10.3.2 Frame size. frame_size(video_params) */
  130. /* [DIRAC_STD] custom_dimensions_flag */
  131. if (bitstream_read_bit(bc)) {
  132. dsh->width = get_interleaved_ue_golomb(bc); /* [DIRAC_STD] FRAME_WIDTH */
  133. dsh->height = get_interleaved_ue_golomb(bc); /* [DIRAC_STD] FRAME_HEIGHT */
  134. }
  135. /* [DIRAC_STD] 10.3.3 Chroma Sampling Format.
  136. * chroma_sampling_format(video_params) */
  137. /* [DIRAC_STD] custom_chroma_format_flag */
  138. if (bitstream_read_bit(bc))
  139. /* [DIRAC_STD] CHROMA_FORMAT_INDEX */
  140. dsh->chroma_format = get_interleaved_ue_golomb(bc);
  141. if (dsh->chroma_format > 2) {
  142. if (log_ctx)
  143. av_log(log_ctx, AV_LOG_ERROR, "Unknown chroma format %d\n",
  144. dsh->chroma_format);
  145. return AVERROR_INVALIDDATA;
  146. }
  147. /* [DIRAC_STD] 10.3.4 Scan Format. scan_format(video_params) */
  148. /* [DIRAC_STD] custom_scan_format_flag */
  149. if (bitstream_read_bit(bc))
  150. /* [DIRAC_STD] SOURCE_SAMPLING */
  151. dsh->interlaced = get_interleaved_ue_golomb(bc);
  152. if (dsh->interlaced > 1)
  153. return AVERROR_INVALIDDATA;
  154. /* [DIRAC_STD] 10.3.5 Frame Rate. frame_rate(video_params) */
  155. if (bitstream_read_bit(bc)) { /* [DIRAC_STD] custom_frame_rate_flag */
  156. dsh->frame_rate_index = get_interleaved_ue_golomb(bc);
  157. if (dsh->frame_rate_index > 10)
  158. return AVERROR_INVALIDDATA;
  159. if (!dsh->frame_rate_index) {
  160. /* [DIRAC_STD] FRAME_RATE_NUMER */
  161. frame_rate.num = get_interleaved_ue_golomb(bc);
  162. /* [DIRAC_STD] FRAME_RATE_DENOM */
  163. frame_rate.den = get_interleaved_ue_golomb(bc);
  164. }
  165. }
  166. /* [DIRAC_STD] preset_frame_rate(video_params, index) */
  167. if (dsh->frame_rate_index > 0) {
  168. if (dsh->frame_rate_index <= 8)
  169. frame_rate = ff_mpeg12_frame_rate_tab[dsh->frame_rate_index];
  170. else
  171. /* [DIRAC_STD] Table 10.3 values 9-10 */
  172. frame_rate = dirac_frame_rate[dsh->frame_rate_index - 9];
  173. }
  174. dsh->framerate = frame_rate;
  175. /* [DIRAC_STD] 10.3.6 Pixel Aspect Ratio.
  176. * pixel_aspect_ratio(video_params) */
  177. if (bitstream_read_bit(bc)) { /* [DIRAC_STD] custom_pixel_aspect_ratio_flag */
  178. /* [DIRAC_STD] index */
  179. dsh->aspect_ratio_index = get_interleaved_ue_golomb(bc);
  180. if (dsh->aspect_ratio_index > 6)
  181. return AVERROR_INVALIDDATA;
  182. if (!dsh->aspect_ratio_index) {
  183. dsh->sample_aspect_ratio.num = get_interleaved_ue_golomb(bc);
  184. dsh->sample_aspect_ratio.den = get_interleaved_ue_golomb(bc);
  185. }
  186. }
  187. /* [DIRAC_STD] Take value from Table 10.4 Available preset pixel
  188. * aspect ratio values */
  189. if (dsh->aspect_ratio_index > 0)
  190. dsh->sample_aspect_ratio =
  191. dirac_preset_aspect_ratios[dsh->aspect_ratio_index - 1];
  192. /* [DIRAC_STD] 10.3.7 Clean area. clean_area(video_params) */
  193. if (bitstream_read_bit(bc)) { /* [DIRAC_STD] custom_clean_area_flag */
  194. /* [DIRAC_STD] CLEAN_WIDTH */
  195. dsh->clean_width = get_interleaved_ue_golomb(bc);
  196. /* [DIRAC_STD] CLEAN_HEIGHT */
  197. dsh->clean_height = get_interleaved_ue_golomb(bc);
  198. /* [DIRAC_STD] CLEAN_LEFT_OFFSET */
  199. dsh->clean_left_offset = get_interleaved_ue_golomb(bc);
  200. /* [DIRAC_STD] CLEAN_RIGHT_OFFSET */
  201. dsh->clean_right_offset = get_interleaved_ue_golomb(bc);
  202. }
  203. /* [DIRAC_STD] 10.3.8 Signal range. signal_range(video_params)
  204. * WARNING: Some adaptation seems to be done using the
  205. * AVCOL_RANGE_MPEG/JPEG values */
  206. if (bitstream_read_bit(bc)) { /* [DIRAC_STD] custom_signal_range_flag */
  207. /* [DIRAC_STD] index */
  208. dsh->pixel_range_index = get_interleaved_ue_golomb(bc);
  209. if (dsh->pixel_range_index > 4)
  210. return AVERROR_INVALIDDATA;
  211. // This assumes either fullrange or MPEG levels only
  212. if (!dsh->pixel_range_index) {
  213. luma_offset = get_interleaved_ue_golomb(bc);
  214. luma_depth = av_log2(get_interleaved_ue_golomb(bc)) + 1;
  215. get_interleaved_ue_golomb(bc); /* chroma offset */
  216. get_interleaved_ue_golomb(bc); /* chroma excursion */
  217. dsh->color_range = luma_offset ? AVCOL_RANGE_MPEG
  218. : AVCOL_RANGE_JPEG;
  219. }
  220. }
  221. /* [DIRAC_STD] Table 10.5
  222. * Available signal range presets <--> pixel_range_presets */
  223. if (dsh->pixel_range_index > 0) {
  224. idx = dsh->pixel_range_index - 1;
  225. luma_depth = pixel_range_presets[idx].bitdepth;
  226. dsh->color_range = pixel_range_presets[idx].color_range;
  227. }
  228. if (luma_depth > 8 && log_ctx)
  229. av_log(log_ctx, AV_LOG_WARNING, "Bitdepth greater than 8");
  230. dsh->pix_fmt = dirac_pix_fmt[!luma_offset][dsh->chroma_format];
  231. /* [DIRAC_STD] 10.3.9 Colour specification. colour_spec(video_params) */
  232. if (bitstream_read_bit(bc)) { /* [DIRAC_STD] custom_colour_spec_flag */
  233. /* [DIRAC_STD] index */
  234. idx = dsh->color_spec_index = get_interleaved_ue_golomb(bc);
  235. if (dsh->color_spec_index > 4)
  236. return AVERROR_INVALIDDATA;
  237. dsh->color_primaries = dirac_color_presets[idx].color_primaries;
  238. dsh->colorspace = dirac_color_presets[idx].colorspace;
  239. dsh->color_trc = dirac_color_presets[idx].color_trc;
  240. if (!dsh->color_spec_index) {
  241. /* [DIRAC_STD] 10.3.9.1 Colour primaries */
  242. if (bitstream_read_bit(bc)) {
  243. idx = get_interleaved_ue_golomb(bc);
  244. if (idx < 3)
  245. dsh->color_primaries = dirac_primaries[idx];
  246. }
  247. /* [DIRAC_STD] 10.3.9.2 Colour matrix */
  248. if (bitstream_read_bit(bc)) {
  249. idx = get_interleaved_ue_golomb(bc);
  250. if (!idx)
  251. dsh->colorspace = AVCOL_SPC_BT709;
  252. else if (idx == 1)
  253. dsh->colorspace = AVCOL_SPC_BT470BG;
  254. }
  255. /* [DIRAC_STD] 10.3.9.3 Transfer function */
  256. if (bitstream_read_bit(bc) && !get_interleaved_ue_golomb(bc))
  257. dsh->color_trc = AVCOL_TRC_BT709;
  258. }
  259. } else {
  260. idx = dsh->color_spec_index;
  261. dsh->color_primaries = dirac_color_presets[idx].color_primaries;
  262. dsh->colorspace = dirac_color_presets[idx].colorspace;
  263. dsh->color_trc = dirac_color_presets[idx].color_trc;
  264. }
  265. return 0;
  266. }
  267. /* [DIRAC_STD] 10. Sequence Header. sequence_header() */
  268. int av_dirac_parse_sequence_header(AVDiracSeqHeader **pdsh,
  269. const uint8_t *buf, size_t buf_size,
  270. void *log_ctx)
  271. {
  272. AVDiracSeqHeader *dsh;
  273. BitstreamContext bc;
  274. unsigned version_major;
  275. unsigned video_format, picture_coding_mode;
  276. int ret;
  277. dsh = av_mallocz(sizeof(*dsh));
  278. if (!dsh)
  279. return AVERROR(ENOMEM);
  280. ret = bitstream_init8(&bc, buf, buf_size);
  281. if (ret < 0)
  282. goto fail;
  283. /* [DIRAC_SPEC] 10.1 Parse Parameters. parse_parameters() */
  284. version_major = get_interleaved_ue_golomb(&bc);
  285. get_interleaved_ue_golomb(&bc); /* version_minor */
  286. dsh->profile = get_interleaved_ue_golomb(&bc);
  287. dsh->level = get_interleaved_ue_golomb(&bc);
  288. /* [DIRAC_SPEC] sequence_header() -> base_video_format as defined in
  289. * 10.2 Base Video Format, table 10.1 Dirac predefined video formats */
  290. video_format = get_interleaved_ue_golomb(&bc);
  291. if (log_ctx) {
  292. if (version_major < 2)
  293. av_log(log_ctx, AV_LOG_WARNING, "Stream is old and may not work\n");
  294. else if (version_major > 2)
  295. av_log(log_ctx, AV_LOG_WARNING, "Stream may have unhandled features\n");
  296. }
  297. if (video_format > 20) {
  298. ret = AVERROR_INVALIDDATA;
  299. goto fail;
  300. }
  301. // Fill in defaults for the source parameters.
  302. dsh->width = dirac_source_parameters_defaults[video_format].width;
  303. dsh->height = dirac_source_parameters_defaults[video_format].height;
  304. dsh->chroma_format = dirac_source_parameters_defaults[video_format].chroma_format;
  305. dsh->interlaced = dirac_source_parameters_defaults[video_format].interlaced;
  306. dsh->top_field_first = dirac_source_parameters_defaults[video_format].top_field_first;
  307. dsh->frame_rate_index = dirac_source_parameters_defaults[video_format].frame_rate_index;
  308. dsh->aspect_ratio_index = dirac_source_parameters_defaults[video_format].aspect_ratio_index;
  309. dsh->clean_width = dirac_source_parameters_defaults[video_format].clean_width;
  310. dsh->clean_height = dirac_source_parameters_defaults[video_format].clean_height;
  311. dsh->clean_left_offset = dirac_source_parameters_defaults[video_format].clean_left_offset;
  312. dsh->clean_right_offset = dirac_source_parameters_defaults[video_format].clean_right_offset;
  313. dsh->pixel_range_index = dirac_source_parameters_defaults[video_format].pixel_range_index;
  314. dsh->color_spec_index = dirac_source_parameters_defaults[video_format].color_spec_index;
  315. /* [DIRAC_STD] 10.3 Source Parameters
  316. * Override the defaults. */
  317. ret = parse_source_parameters(dsh, &bc, log_ctx);
  318. if (ret < 0)
  319. goto fail;
  320. /* [DIRAC_STD] picture_coding_mode shall be 0 for fields and 1 for frames
  321. * currently only used to signal field coding */
  322. picture_coding_mode = get_interleaved_ue_golomb(&bc);
  323. if (picture_coding_mode != 0) {
  324. if (log_ctx) {
  325. av_log(log_ctx, AV_LOG_ERROR, "Unsupported picture coding mode %d",
  326. picture_coding_mode);
  327. }
  328. ret = AVERROR_INVALIDDATA;
  329. goto fail;
  330. }
  331. *pdsh = dsh;
  332. return 0;
  333. fail:
  334. av_freep(&dsh);
  335. *pdsh = NULL;
  336. return ret;
  337. }
  338. #else
  339. int av_dirac_parse_sequence_header(AVDiracSeqHeader **pdsh,
  340. const uint8_t *buf, size_t buf_size,
  341. void *log_ctx)
  342. {
  343. return AVERROR(ENOSYS);
  344. }
  345. #endif