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.

388 lines
12KB

  1. /*
  2. * HEVC Supplementary Enhancement Information messages
  3. *
  4. * Copyright (C) 2012 - 2013 Guillaume Martres
  5. * Copyright (C) 2012 - 2013 Gildas Cocherel
  6. * Copyright (C) 2013 Vittorio Giovara
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include "golomb.h"
  25. #include "hevcdec.h"
  26. enum HEVC_SEI_TYPE {
  27. SEI_TYPE_BUFFERING_PERIOD = 0,
  28. SEI_TYPE_PICTURE_TIMING = 1,
  29. SEI_TYPE_PAN_SCAN_RECT = 2,
  30. SEI_TYPE_FILLER_PAYLOAD = 3,
  31. SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35 = 4,
  32. SEI_TYPE_USER_DATA_UNREGISTERED = 5,
  33. SEI_TYPE_RECOVERY_POINT = 6,
  34. SEI_TYPE_SCENE_INFO = 9,
  35. SEI_TYPE_FULL_FRAME_SNAPSHOT = 15,
  36. SEI_TYPE_PROGRESSIVE_REFINEMENT_SEGMENT_START = 16,
  37. SEI_TYPE_PROGRESSIVE_REFINEMENT_SEGMENT_END = 17,
  38. SEI_TYPE_FILM_GRAIN_CHARACTERISTICS = 19,
  39. SEI_TYPE_POST_FILTER_HINT = 22,
  40. SEI_TYPE_TONE_MAPPING_INFO = 23,
  41. SEI_TYPE_FRAME_PACKING = 45,
  42. SEI_TYPE_DISPLAY_ORIENTATION = 47,
  43. SEI_TYPE_SOP_DESCRIPTION = 128,
  44. SEI_TYPE_ACTIVE_PARAMETER_SETS = 129,
  45. SEI_TYPE_DECODING_UNIT_INFO = 130,
  46. SEI_TYPE_TEMPORAL_LEVEL0_INDEX = 131,
  47. SEI_TYPE_DECODED_PICTURE_HASH = 132,
  48. SEI_TYPE_SCALABLE_NESTING = 133,
  49. SEI_TYPE_REGION_REFRESH_INFO = 134,
  50. SEI_TYPE_MASTERING_DISPLAY_INFO = 137,
  51. SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO = 144,
  52. };
  53. static int decode_nal_sei_decoded_picture_hash(HEVCContext *s)
  54. {
  55. int cIdx, i;
  56. uint8_t hash_type;
  57. //uint16_t picture_crc;
  58. //uint32_t picture_checksum;
  59. GetBitContext *gb = &s->HEVClc->gb;
  60. hash_type = get_bits(gb, 8);
  61. for (cIdx = 0; cIdx < 3/*((s->sps->chroma_format_idc == 0) ? 1 : 3)*/; cIdx++) {
  62. if (hash_type == 0) {
  63. s->is_md5 = 1;
  64. for (i = 0; i < 16; i++)
  65. s->md5[cIdx][i] = get_bits(gb, 8);
  66. } else if (hash_type == 1) {
  67. // picture_crc = get_bits(gb, 16);
  68. skip_bits(gb, 16);
  69. } else if (hash_type == 2) {
  70. // picture_checksum = get_bits_long(gb, 32);
  71. skip_bits(gb, 32);
  72. }
  73. }
  74. return 0;
  75. }
  76. static int decode_nal_sei_mastering_display_info(HEVCContext *s)
  77. {
  78. GetBitContext *gb = &s->HEVClc->gb;
  79. int i;
  80. // Mastering primaries
  81. for (i = 0; i < 3; i++) {
  82. s->display_primaries[i][0] = get_bits(gb, 16);
  83. s->display_primaries[i][1] = get_bits(gb, 16);
  84. }
  85. // White point (x, y)
  86. s->white_point[0] = get_bits(gb, 16);
  87. s->white_point[1] = get_bits(gb, 16);
  88. // Max and min luminance of mastering display
  89. s->max_mastering_luminance = get_bits_long(gb, 32);
  90. s->min_mastering_luminance = get_bits_long(gb, 32);
  91. // As this SEI message comes before the first frame that references it,
  92. // initialize the flag to 2 and decrement on IRAP access unit so it
  93. // persists for the coded video sequence (e.g., between two IRAPs)
  94. s->sei_mastering_display_info_present = 2;
  95. return 0;
  96. }
  97. static int decode_nal_sei_frame_packing_arrangement(HEVCContext *s)
  98. {
  99. GetBitContext *gb = &s->HEVClc->gb;
  100. get_ue_golomb_long(gb); // frame_packing_arrangement_id
  101. s->sei_frame_packing_present = !get_bits1(gb);
  102. if (s->sei_frame_packing_present) {
  103. s->frame_packing_arrangement_type = get_bits(gb, 7);
  104. s->quincunx_subsampling = get_bits1(gb);
  105. s->content_interpretation_type = get_bits(gb, 6);
  106. // the following skips spatial_flipping_flag frame0_flipped_flag
  107. // field_views_flag current_frame_is_frame0_flag
  108. // frame0_self_contained_flag frame1_self_contained_flag
  109. skip_bits(gb, 6);
  110. if (!s->quincunx_subsampling && s->frame_packing_arrangement_type != 5)
  111. skip_bits(gb, 16); // frame[01]_grid_position_[xy]
  112. skip_bits(gb, 8); // frame_packing_arrangement_reserved_byte
  113. skip_bits1(gb); // frame_packing_arrangement_persistence_flag
  114. }
  115. skip_bits1(gb); // upsampled_aspect_ratio_flag
  116. return 0;
  117. }
  118. static int decode_nal_sei_display_orientation(HEVCContext *s)
  119. {
  120. GetBitContext *gb = &s->HEVClc->gb;
  121. s->sei_display_orientation_present = !get_bits1(gb);
  122. if (s->sei_display_orientation_present) {
  123. s->sei_hflip = get_bits1(gb); // hor_flip
  124. s->sei_vflip = get_bits1(gb); // ver_flip
  125. s->sei_anticlockwise_rotation = get_bits(gb, 16);
  126. skip_bits1(gb); // display_orientation_persistence_flag
  127. }
  128. return 0;
  129. }
  130. static int decode_pic_timing(HEVCContext *s, int size)
  131. {
  132. GetBitContext *gb = &s->HEVClc->gb;
  133. HEVCSPS *sps;
  134. if (!s->ps.sps_list[s->active_seq_parameter_set_id])
  135. return(AVERROR(ENOMEM));
  136. sps = (HEVCSPS*)s->ps.sps_list[s->active_seq_parameter_set_id]->data;
  137. if (sps->vui.frame_field_info_present_flag) {
  138. int pic_struct = get_bits(gb, 4);
  139. s->picture_struct = AV_PICTURE_STRUCTURE_UNKNOWN;
  140. if (pic_struct == 2) {
  141. av_log(s->avctx, AV_LOG_DEBUG, "BOTTOM Field\n");
  142. s->picture_struct = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
  143. } else if (pic_struct == 1) {
  144. av_log(s->avctx, AV_LOG_DEBUG, "TOP Field\n");
  145. s->picture_struct = AV_PICTURE_STRUCTURE_TOP_FIELD;
  146. }
  147. get_bits(gb, 2); // source_scan_type
  148. get_bits(gb, 1); // duplicate_flag
  149. skip_bits1(gb);
  150. size--;
  151. }
  152. skip_bits_long(gb, 8 * size);
  153. return 0;
  154. }
  155. static int decode_registered_user_data_closed_caption(HEVCContext *s, int size)
  156. {
  157. int flag;
  158. int user_data_type_code;
  159. int cc_count;
  160. GetBitContext *gb = &s->HEVClc->gb;
  161. if (size < 3)
  162. return AVERROR(EINVAL);
  163. user_data_type_code = get_bits(gb, 8);
  164. if (user_data_type_code == 0x3) {
  165. skip_bits(gb, 1); // reserved
  166. flag = get_bits(gb, 1); // process_cc_data_flag
  167. if (flag) {
  168. skip_bits(gb, 1);
  169. cc_count = get_bits(gb, 5);
  170. skip_bits(gb, 8); // reserved
  171. size -= 2;
  172. if (cc_count && size >= cc_count * 3) {
  173. const uint64_t new_size = (s->a53_caption_size + cc_count
  174. * UINT64_C(3));
  175. int i, ret;
  176. if (new_size > INT_MAX)
  177. return AVERROR(EINVAL);
  178. /* Allow merging of the cc data from two fields. */
  179. ret = av_reallocp(&s->a53_caption, new_size);
  180. if (ret < 0)
  181. return ret;
  182. for (i = 0; i < cc_count; i++) {
  183. s->a53_caption[s->a53_caption_size++] = get_bits(gb, 8);
  184. s->a53_caption[s->a53_caption_size++] = get_bits(gb, 8);
  185. s->a53_caption[s->a53_caption_size++] = get_bits(gb, 8);
  186. }
  187. skip_bits(gb, 8); // marker_bits
  188. }
  189. }
  190. } else {
  191. int i;
  192. for (i = 0; i < size - 1; i++)
  193. skip_bits(gb, 8);
  194. }
  195. return 0;
  196. }
  197. static int decode_nal_sei_user_data_registered_itu_t_t35(HEVCContext *s, int size)
  198. {
  199. uint32_t country_code;
  200. uint32_t user_identifier;
  201. GetBitContext *gb = &s->HEVClc->gb;
  202. if (size < 7)
  203. return AVERROR(EINVAL);
  204. size -= 7;
  205. country_code = get_bits(gb, 8);
  206. if (country_code == 0xFF) {
  207. skip_bits(gb, 8);
  208. size--;
  209. }
  210. skip_bits(gb, 8);
  211. skip_bits(gb, 8);
  212. user_identifier = get_bits_long(gb, 32);
  213. switch (user_identifier) {
  214. case MKBETAG('G', 'A', '9', '4'):
  215. return decode_registered_user_data_closed_caption(s, size);
  216. default:
  217. skip_bits_long(gb, size * 8);
  218. break;
  219. }
  220. return 0;
  221. }
  222. static int active_parameter_sets(HEVCContext *s)
  223. {
  224. GetBitContext *gb = &s->HEVClc->gb;
  225. int num_sps_ids_minus1;
  226. int i;
  227. unsigned active_seq_parameter_set_id;
  228. get_bits(gb, 4); // active_video_parameter_set_id
  229. get_bits(gb, 1); // self_contained_cvs_flag
  230. get_bits(gb, 1); // num_sps_ids_minus1
  231. num_sps_ids_minus1 = get_ue_golomb_long(gb); // num_sps_ids_minus1
  232. if (num_sps_ids_minus1 < 0 || num_sps_ids_minus1 > 15) {
  233. av_log(s->avctx, AV_LOG_ERROR, "num_sps_ids_minus1 %d invalid\n", num_sps_ids_minus1);
  234. return AVERROR_INVALIDDATA;
  235. }
  236. active_seq_parameter_set_id = get_ue_golomb_long(gb);
  237. if (active_seq_parameter_set_id >= HEVC_MAX_SPS_COUNT) {
  238. av_log(s->avctx, AV_LOG_ERROR, "active_parameter_set_id %d invalid\n", active_seq_parameter_set_id);
  239. return AVERROR_INVALIDDATA;
  240. }
  241. s->active_seq_parameter_set_id = active_seq_parameter_set_id;
  242. for (i = 1; i <= num_sps_ids_minus1; i++)
  243. get_ue_golomb_long(gb); // active_seq_parameter_set_id[i]
  244. return 0;
  245. }
  246. static int decode_nal_sei_prefix(HEVCContext *s, int type, int size)
  247. {
  248. GetBitContext *gb = &s->HEVClc->gb;
  249. switch (type) {
  250. case 256: // Mismatched value from HM 8.1
  251. return decode_nal_sei_decoded_picture_hash(s);
  252. case SEI_TYPE_FRAME_PACKING:
  253. return decode_nal_sei_frame_packing_arrangement(s);
  254. case SEI_TYPE_DISPLAY_ORIENTATION:
  255. return decode_nal_sei_display_orientation(s);
  256. case SEI_TYPE_PICTURE_TIMING:
  257. {
  258. int ret = decode_pic_timing(s, size);
  259. av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", type);
  260. return ret;
  261. }
  262. case SEI_TYPE_MASTERING_DISPLAY_INFO:
  263. return decode_nal_sei_mastering_display_info(s);
  264. case SEI_TYPE_ACTIVE_PARAMETER_SETS:
  265. active_parameter_sets(s);
  266. av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", type);
  267. return 0;
  268. case SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35:
  269. return decode_nal_sei_user_data_registered_itu_t_t35(s, size);
  270. default:
  271. av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", type);
  272. skip_bits_long(gb, 8 * size);
  273. return 0;
  274. }
  275. }
  276. static int decode_nal_sei_suffix(HEVCContext *s, int type, int size)
  277. {
  278. GetBitContext *gb = &s->HEVClc->gb;
  279. switch (type) {
  280. case SEI_TYPE_DECODED_PICTURE_HASH:
  281. return decode_nal_sei_decoded_picture_hash(s);
  282. default:
  283. av_log(s->avctx, AV_LOG_DEBUG, "Skipped SUFFIX SEI %d\n", type);
  284. skip_bits_long(gb, 8 * size);
  285. return 0;
  286. }
  287. }
  288. static int decode_nal_sei_message(HEVCContext *s)
  289. {
  290. GetBitContext *gb = &s->HEVClc->gb;
  291. int payload_type = 0;
  292. int payload_size = 0;
  293. int byte = 0xFF;
  294. av_log(s->avctx, AV_LOG_DEBUG, "Decoding SEI\n");
  295. while (byte == 0xFF) {
  296. if (get_bits_left(gb) < 16 || payload_type > INT_MAX - 255)
  297. return AVERROR_INVALIDDATA;
  298. byte = get_bits(gb, 8);
  299. payload_type += byte;
  300. }
  301. byte = 0xFF;
  302. while (byte == 0xFF) {
  303. if (get_bits_left(gb) < 8 + 8LL*payload_size)
  304. return AVERROR_INVALIDDATA;
  305. byte = get_bits(gb, 8);
  306. payload_size += byte;
  307. }
  308. if (s->nal_unit_type == HEVC_NAL_SEI_PREFIX) {
  309. return decode_nal_sei_prefix(s, payload_type, payload_size);
  310. } else { /* nal_unit_type == NAL_SEI_SUFFIX */
  311. return decode_nal_sei_suffix(s, payload_type, payload_size);
  312. }
  313. }
  314. static int more_rbsp_data(GetBitContext *gb)
  315. {
  316. return get_bits_left(gb) > 0 && show_bits(gb, 8) != 0x80;
  317. }
  318. int ff_hevc_decode_nal_sei(HEVCContext *s)
  319. {
  320. int ret;
  321. do {
  322. ret = decode_nal_sei_message(s);
  323. if (ret < 0)
  324. return(AVERROR(ENOMEM));
  325. } while (more_rbsp_data(&s->HEVClc->gb));
  326. return 1;
  327. }
  328. void ff_hevc_reset_sei(HEVCContext *s)
  329. {
  330. s->a53_caption_size = 0;
  331. av_freep(&s->a53_caption);
  332. }