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.

343 lines
11KB

  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_frame_packing_arrangement(HEVCContext *s)
  77. {
  78. GetBitContext *gb = &s->HEVClc->gb;
  79. get_ue_golomb(gb); // frame_packing_arrangement_id
  80. s->sei_frame_packing_present = !get_bits1(gb);
  81. if (s->sei_frame_packing_present) {
  82. s->frame_packing_arrangement_type = get_bits(gb, 7);
  83. s->quincunx_subsampling = get_bits1(gb);
  84. s->content_interpretation_type = get_bits(gb, 6);
  85. // the following skips spatial_flipping_flag frame0_flipped_flag
  86. // field_views_flag current_frame_is_frame0_flag
  87. // frame0_self_contained_flag frame1_self_contained_flag
  88. skip_bits(gb, 6);
  89. if (!s->quincunx_subsampling && s->frame_packing_arrangement_type != 5)
  90. skip_bits(gb, 16); // frame[01]_grid_position_[xy]
  91. skip_bits(gb, 8); // frame_packing_arrangement_reserved_byte
  92. skip_bits1(gb); // frame_packing_arrangement_persistance_flag
  93. }
  94. skip_bits1(gb); // upsampled_aspect_ratio_flag
  95. return 0;
  96. }
  97. static int decode_nal_sei_display_orientation(HEVCContext *s)
  98. {
  99. GetBitContext *gb = &s->HEVClc->gb;
  100. s->sei_display_orientation_present = !get_bits1(gb);
  101. if (s->sei_display_orientation_present) {
  102. s->sei_hflip = get_bits1(gb); // hor_flip
  103. s->sei_vflip = get_bits1(gb); // ver_flip
  104. s->sei_anticlockwise_rotation = get_bits(gb, 16);
  105. skip_bits1(gb); // display_orientation_persistence_flag
  106. }
  107. return 0;
  108. }
  109. static int decode_pic_timing(HEVCContext *s)
  110. {
  111. GetBitContext *gb = &s->HEVClc->gb;
  112. HEVCSPS *sps;
  113. if (!s->ps.sps_list[s->active_seq_parameter_set_id])
  114. return(AVERROR(ENOMEM));
  115. sps = (HEVCSPS*)s->ps.sps_list[s->active_seq_parameter_set_id]->data;
  116. if (sps->vui.frame_field_info_present_flag) {
  117. int pic_struct = get_bits(gb, 4);
  118. s->picture_struct = AV_PICTURE_STRUCTURE_UNKNOWN;
  119. if (pic_struct == 2) {
  120. av_log(s->avctx, AV_LOG_DEBUG, "BOTTOM Field\n");
  121. s->picture_struct = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
  122. } else if (pic_struct == 1) {
  123. av_log(s->avctx, AV_LOG_DEBUG, "TOP Field\n");
  124. s->picture_struct = AV_PICTURE_STRUCTURE_TOP_FIELD;
  125. }
  126. get_bits(gb, 2); // source_scan_type
  127. get_bits(gb, 1); // duplicate_flag
  128. }
  129. return 1;
  130. }
  131. static int decode_registered_user_data_closed_caption(HEVCContext *s, int size)
  132. {
  133. int flag;
  134. int user_data_type_code;
  135. int cc_count;
  136. int i;
  137. GetBitContext *gb = &s->HEVClc->gb;
  138. if (size < 3)
  139. return AVERROR(EINVAL);
  140. user_data_type_code = get_bits(gb, 8);
  141. if (user_data_type_code == 0x3) {
  142. skip_bits(gb, 1); // reserved
  143. flag = get_bits(gb, 1); // process_cc_data_flag
  144. if (flag) {
  145. skip_bits(gb, 1);
  146. cc_count = get_bits(gb, 5);
  147. skip_bits(gb, 8); // reserved
  148. size -= 2;
  149. if (cc_count && size >= cc_count * 3) {
  150. av_freep(&s->a53_caption);
  151. s->a53_caption_size = cc_count * 3;
  152. s->a53_caption = av_malloc(s->a53_caption_size);
  153. if (!s->a53_caption)
  154. return(AVERROR(ENOMEM));
  155. for (i = 0; i < s->a53_caption_size; i++) {
  156. s->a53_caption[i++] = get_bits(gb, 8);
  157. }
  158. skip_bits(gb, 8); // marker_bits
  159. }
  160. }
  161. } else {
  162. for (i = 0; i < size - 1; i++)
  163. skip_bits(gb, 8);
  164. }
  165. return 0;
  166. }
  167. static int decode_nal_sei_user_data_registered_itu_t_t35(HEVCContext *s, int size)
  168. {
  169. uint32_t country_code;
  170. uint32_t user_identifier;
  171. GetBitContext *gb = &s->HEVClc->gb;
  172. if (size < 7)
  173. return AVERROR(EINVAL);
  174. size -= 7;
  175. country_code = get_bits(gb, 8);
  176. if (country_code == 0xFF) {
  177. skip_bits(gb, 8);
  178. size--;
  179. }
  180. skip_bits(gb, 8);
  181. skip_bits(gb, 8);
  182. user_identifier = get_bits_long(gb, 32);
  183. switch (user_identifier) {
  184. case MKBETAG('G', 'A', '9', '4'):
  185. return decode_registered_user_data_closed_caption(s, size);
  186. default:
  187. skip_bits_long(gb, size * 8);
  188. break;
  189. }
  190. return 0;
  191. }
  192. static int active_parameter_sets(HEVCContext *s)
  193. {
  194. GetBitContext *gb = &s->HEVClc->gb;
  195. int num_sps_ids_minus1;
  196. int i;
  197. unsigned active_seq_parameter_set_id;
  198. get_bits(gb, 4); // active_video_parameter_set_id
  199. get_bits(gb, 1); // self_contained_cvs_flag
  200. get_bits(gb, 1); // num_sps_ids_minus1
  201. num_sps_ids_minus1 = get_ue_golomb_long(gb); // num_sps_ids_minus1
  202. if (num_sps_ids_minus1 < 0 || num_sps_ids_minus1 > 15) {
  203. av_log(s->avctx, AV_LOG_ERROR, "num_sps_ids_minus1 %d invalid\n", num_sps_ids_minus1);
  204. return AVERROR_INVALIDDATA;
  205. }
  206. active_seq_parameter_set_id = get_ue_golomb_long(gb);
  207. if (active_seq_parameter_set_id >= MAX_SPS_COUNT) {
  208. av_log(s->avctx, AV_LOG_ERROR, "active_parameter_set_id %d invalid\n", active_seq_parameter_set_id);
  209. return AVERROR_INVALIDDATA;
  210. }
  211. s->active_seq_parameter_set_id = active_seq_parameter_set_id;
  212. for (i = 1; i <= num_sps_ids_minus1; i++)
  213. get_ue_golomb_long(gb); // active_seq_parameter_set_id[i]
  214. return 0;
  215. }
  216. static int decode_nal_sei_prefix(HEVCContext *s, int type, int size)
  217. {
  218. GetBitContext *gb = &s->HEVClc->gb;
  219. switch (type) {
  220. case 256: // Mismatched value from HM 8.1
  221. return decode_nal_sei_decoded_picture_hash(s);
  222. case SEI_TYPE_FRAME_PACKING:
  223. return decode_nal_sei_frame_packing_arrangement(s);
  224. case SEI_TYPE_DISPLAY_ORIENTATION:
  225. return decode_nal_sei_display_orientation(s);
  226. case SEI_TYPE_PICTURE_TIMING:
  227. {
  228. int ret = decode_pic_timing(s);
  229. av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", type);
  230. skip_bits(gb, 8 * size);
  231. return ret;
  232. }
  233. case SEI_TYPE_ACTIVE_PARAMETER_SETS:
  234. active_parameter_sets(s);
  235. av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", type);
  236. return 0;
  237. case SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35:
  238. return decode_nal_sei_user_data_registered_itu_t_t35(s, size);
  239. default:
  240. av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", type);
  241. skip_bits_long(gb, 8 * size);
  242. return 0;
  243. }
  244. }
  245. static int decode_nal_sei_suffix(HEVCContext *s, int type, int size)
  246. {
  247. GetBitContext *gb = &s->HEVClc->gb;
  248. switch (type) {
  249. case SEI_TYPE_DECODED_PICTURE_HASH:
  250. return decode_nal_sei_decoded_picture_hash(s);
  251. default:
  252. av_log(s->avctx, AV_LOG_DEBUG, "Skipped SUFFIX SEI %d\n", type);
  253. skip_bits_long(gb, 8 * size);
  254. return 0;
  255. }
  256. }
  257. static int decode_nal_sei_message(HEVCContext *s)
  258. {
  259. GetBitContext *gb = &s->HEVClc->gb;
  260. int payload_type = 0;
  261. int payload_size = 0;
  262. int byte = 0xFF;
  263. av_log(s->avctx, AV_LOG_DEBUG, "Decoding SEI\n");
  264. while (byte == 0xFF) {
  265. byte = get_bits(gb, 8);
  266. payload_type += byte;
  267. }
  268. byte = 0xFF;
  269. while (byte == 0xFF) {
  270. byte = get_bits(gb, 8);
  271. payload_size += byte;
  272. }
  273. if (s->nal_unit_type == NAL_SEI_PREFIX) {
  274. return decode_nal_sei_prefix(s, payload_type, payload_size);
  275. } else { /* nal_unit_type == NAL_SEI_SUFFIX */
  276. return decode_nal_sei_suffix(s, payload_type, payload_size);
  277. }
  278. return 1;
  279. }
  280. static int more_rbsp_data(GetBitContext *gb)
  281. {
  282. return get_bits_left(gb) > 0 && show_bits(gb, 8) != 0x80;
  283. }
  284. int ff_hevc_decode_nal_sei(HEVCContext *s)
  285. {
  286. int ret;
  287. do {
  288. ret = decode_nal_sei_message(s);
  289. if (ret < 0)
  290. return(AVERROR(ENOMEM));
  291. } while (more_rbsp_data(&s->HEVClc->gb));
  292. return 1;
  293. }