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.

382 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 "hevc.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)
  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. }
  150. return 1;
  151. }
  152. static int decode_registered_user_data_closed_caption(HEVCContext *s, int size)
  153. {
  154. int flag;
  155. int user_data_type_code;
  156. int cc_count;
  157. GetBitContext *gb = &s->HEVClc->gb;
  158. if (size < 3)
  159. return AVERROR(EINVAL);
  160. user_data_type_code = get_bits(gb, 8);
  161. if (user_data_type_code == 0x3) {
  162. skip_bits(gb, 1); // reserved
  163. flag = get_bits(gb, 1); // process_cc_data_flag
  164. if (flag) {
  165. skip_bits(gb, 1);
  166. cc_count = get_bits(gb, 5);
  167. skip_bits(gb, 8); // reserved
  168. size -= 2;
  169. if (cc_count && size >= cc_count * 3) {
  170. const uint64_t new_size = (s->a53_caption_size + cc_count
  171. * UINT64_C(3));
  172. int i, ret;
  173. if (new_size > INT_MAX)
  174. return AVERROR(EINVAL);
  175. /* Allow merging of the cc data from two fields. */
  176. ret = av_reallocp(&s->a53_caption, new_size);
  177. if (ret < 0)
  178. return ret;
  179. for (i = 0; i < cc_count; i++) {
  180. s->a53_caption[s->a53_caption_size++] = get_bits(gb, 8);
  181. s->a53_caption[s->a53_caption_size++] = get_bits(gb, 8);
  182. s->a53_caption[s->a53_caption_size++] = get_bits(gb, 8);
  183. }
  184. skip_bits(gb, 8); // marker_bits
  185. }
  186. }
  187. } else {
  188. int i;
  189. for (i = 0; i < size - 1; i++)
  190. skip_bits(gb, 8);
  191. }
  192. return 0;
  193. }
  194. static int decode_nal_sei_user_data_registered_itu_t_t35(HEVCContext *s, int size)
  195. {
  196. uint32_t country_code;
  197. uint32_t user_identifier;
  198. GetBitContext *gb = &s->HEVClc->gb;
  199. if (size < 7)
  200. return AVERROR(EINVAL);
  201. size -= 7;
  202. country_code = get_bits(gb, 8);
  203. if (country_code == 0xFF) {
  204. skip_bits(gb, 8);
  205. size--;
  206. }
  207. skip_bits(gb, 8);
  208. skip_bits(gb, 8);
  209. user_identifier = get_bits_long(gb, 32);
  210. switch (user_identifier) {
  211. case MKBETAG('G', 'A', '9', '4'):
  212. return decode_registered_user_data_closed_caption(s, size);
  213. default:
  214. skip_bits_long(gb, size * 8);
  215. break;
  216. }
  217. return 0;
  218. }
  219. static int active_parameter_sets(HEVCContext *s)
  220. {
  221. GetBitContext *gb = &s->HEVClc->gb;
  222. int num_sps_ids_minus1;
  223. int i;
  224. unsigned active_seq_parameter_set_id;
  225. get_bits(gb, 4); // active_video_parameter_set_id
  226. get_bits(gb, 1); // self_contained_cvs_flag
  227. get_bits(gb, 1); // num_sps_ids_minus1
  228. num_sps_ids_minus1 = get_ue_golomb_long(gb); // num_sps_ids_minus1
  229. if (num_sps_ids_minus1 < 0 || num_sps_ids_minus1 > 15) {
  230. av_log(s->avctx, AV_LOG_ERROR, "num_sps_ids_minus1 %d invalid\n", num_sps_ids_minus1);
  231. return AVERROR_INVALIDDATA;
  232. }
  233. active_seq_parameter_set_id = get_ue_golomb_long(gb);
  234. if (active_seq_parameter_set_id >= MAX_SPS_COUNT) {
  235. av_log(s->avctx, AV_LOG_ERROR, "active_parameter_set_id %d invalid\n", active_seq_parameter_set_id);
  236. return AVERROR_INVALIDDATA;
  237. }
  238. s->active_seq_parameter_set_id = active_seq_parameter_set_id;
  239. for (i = 1; i <= num_sps_ids_minus1; i++)
  240. get_ue_golomb_long(gb); // active_seq_parameter_set_id[i]
  241. return 0;
  242. }
  243. static int decode_nal_sei_prefix(HEVCContext *s, int type, int size)
  244. {
  245. GetBitContext *gb = &s->HEVClc->gb;
  246. switch (type) {
  247. case 256: // Mismatched value from HM 8.1
  248. return decode_nal_sei_decoded_picture_hash(s);
  249. case SEI_TYPE_FRAME_PACKING:
  250. return decode_nal_sei_frame_packing_arrangement(s);
  251. case SEI_TYPE_DISPLAY_ORIENTATION:
  252. return decode_nal_sei_display_orientation(s);
  253. case SEI_TYPE_PICTURE_TIMING:
  254. {
  255. int ret = decode_pic_timing(s);
  256. av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", type);
  257. skip_bits(gb, 8 * size);
  258. return ret;
  259. }
  260. case SEI_TYPE_MASTERING_DISPLAY_INFO:
  261. return decode_nal_sei_mastering_display_info(s);
  262. case SEI_TYPE_ACTIVE_PARAMETER_SETS:
  263. active_parameter_sets(s);
  264. av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", type);
  265. return 0;
  266. case SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35:
  267. return decode_nal_sei_user_data_registered_itu_t_t35(s, size);
  268. default:
  269. av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", type);
  270. skip_bits_long(gb, 8 * size);
  271. return 0;
  272. }
  273. }
  274. static int decode_nal_sei_suffix(HEVCContext *s, int type, int size)
  275. {
  276. GetBitContext *gb = &s->HEVClc->gb;
  277. switch (type) {
  278. case SEI_TYPE_DECODED_PICTURE_HASH:
  279. return decode_nal_sei_decoded_picture_hash(s);
  280. default:
  281. av_log(s->avctx, AV_LOG_DEBUG, "Skipped SUFFIX SEI %d\n", type);
  282. skip_bits_long(gb, 8 * size);
  283. return 0;
  284. }
  285. }
  286. static int decode_nal_sei_message(HEVCContext *s)
  287. {
  288. GetBitContext *gb = &s->HEVClc->gb;
  289. int payload_type = 0;
  290. int payload_size = 0;
  291. int byte = 0xFF;
  292. av_log(s->avctx, AV_LOG_DEBUG, "Decoding SEI\n");
  293. while (byte == 0xFF) {
  294. byte = get_bits(gb, 8);
  295. payload_type += byte;
  296. }
  297. byte = 0xFF;
  298. while (byte == 0xFF) {
  299. byte = get_bits(gb, 8);
  300. payload_size += byte;
  301. }
  302. if (s->nal_unit_type == NAL_SEI_PREFIX) {
  303. return decode_nal_sei_prefix(s, payload_type, payload_size);
  304. } else { /* nal_unit_type == NAL_SEI_SUFFIX */
  305. return decode_nal_sei_suffix(s, payload_type, payload_size);
  306. }
  307. return 1;
  308. }
  309. static int more_rbsp_data(GetBitContext *gb)
  310. {
  311. return get_bits_left(gb) > 0 && show_bits(gb, 8) != 0x80;
  312. }
  313. int ff_hevc_decode_nal_sei(HEVCContext *s)
  314. {
  315. int ret;
  316. do {
  317. ret = decode_nal_sei_message(s);
  318. if (ret < 0)
  319. return(AVERROR(ENOMEM));
  320. } while (more_rbsp_data(&s->HEVClc->gb));
  321. return 1;
  322. }
  323. void ff_hevc_reset_sei(HEVCContext *s)
  324. {
  325. s->a53_caption_size = 0;
  326. av_freep(&s->a53_caption);
  327. }