2
0

dirac.c 15 KB

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