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.

365 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_ps.h"
  26. #include "hevc_sei.h"
  27. static int decode_nal_sei_decoded_picture_hash(HEVCSEIPictureHash *s, GetBitContext *gb)
  28. {
  29. int cIdx, i;
  30. uint8_t hash_type;
  31. //uint16_t picture_crc;
  32. //uint32_t picture_checksum;
  33. hash_type = get_bits(gb, 8);
  34. for (cIdx = 0; cIdx < 3/*((s->sps->chroma_format_idc == 0) ? 1 : 3)*/; cIdx++) {
  35. if (hash_type == 0) {
  36. s->is_md5 = 1;
  37. for (i = 0; i < 16; i++)
  38. s->md5[cIdx][i] = get_bits(gb, 8);
  39. } else if (hash_type == 1) {
  40. // picture_crc = get_bits(gb, 16);
  41. skip_bits(gb, 16);
  42. } else if (hash_type == 2) {
  43. // picture_checksum = get_bits_long(gb, 32);
  44. skip_bits(gb, 32);
  45. }
  46. }
  47. return 0;
  48. }
  49. static int decode_nal_sei_mastering_display_info(HEVCSEIMasteringDisplay *s, GetBitContext *gb)
  50. {
  51. int i;
  52. // Mastering primaries
  53. for (i = 0; i < 3; i++) {
  54. s->display_primaries[i][0] = get_bits(gb, 16);
  55. s->display_primaries[i][1] = get_bits(gb, 16);
  56. }
  57. // White point (x, y)
  58. s->white_point[0] = get_bits(gb, 16);
  59. s->white_point[1] = get_bits(gb, 16);
  60. // Max and min luminance of mastering display
  61. s->max_luminance = get_bits_long(gb, 32);
  62. s->min_luminance = get_bits_long(gb, 32);
  63. // As this SEI message comes before the first frame that references it,
  64. // initialize the flag to 2 and decrement on IRAP access unit so it
  65. // persists for the coded video sequence (e.g., between two IRAPs)
  66. s->present = 2;
  67. return 0;
  68. }
  69. static int decode_nal_sei_content_light_info(HEVCSEIContentLight *s, GetBitContext *gb)
  70. {
  71. // Max and average light levels
  72. s->max_content_light_level = get_bits_long(gb, 16);
  73. s->max_pic_average_light_level = get_bits_long(gb, 16);
  74. // As this SEI message comes before the first frame that references it,
  75. // initialize the flag to 2 and decrement on IRAP access unit so it
  76. // persists for the coded video sequence (e.g., between two IRAPs)
  77. s->present = 2;
  78. return 0;
  79. }
  80. static int decode_nal_sei_frame_packing_arrangement(HEVCSEIFramePacking *s, GetBitContext *gb)
  81. {
  82. get_ue_golomb_long(gb); // frame_packing_arrangement_id
  83. s->present = !get_bits1(gb);
  84. if (s->present) {
  85. s->arrangement_type = get_bits(gb, 7);
  86. s->quincunx_subsampling = get_bits1(gb);
  87. s->content_interpretation_type = get_bits(gb, 6);
  88. // the following skips spatial_flipping_flag frame0_flipped_flag
  89. // field_views_flag current_frame_is_frame0_flag
  90. // frame0_self_contained_flag frame1_self_contained_flag
  91. skip_bits(gb, 6);
  92. if (!s->quincunx_subsampling && s->arrangement_type != 5)
  93. skip_bits(gb, 16); // frame[01]_grid_position_[xy]
  94. skip_bits(gb, 8); // frame_packing_arrangement_reserved_byte
  95. skip_bits1(gb); // frame_packing_arrangement_persistence_flag
  96. }
  97. skip_bits1(gb); // upsampled_aspect_ratio_flag
  98. return 0;
  99. }
  100. static int decode_nal_sei_display_orientation(HEVCSEIDisplayOrientation *s, GetBitContext *gb)
  101. {
  102. s->present = !get_bits1(gb);
  103. if (s->present) {
  104. s->hflip = get_bits1(gb); // hor_flip
  105. s->vflip = get_bits1(gb); // ver_flip
  106. s->anticlockwise_rotation = get_bits(gb, 16);
  107. skip_bits1(gb); // display_orientation_persistence_flag
  108. }
  109. return 0;
  110. }
  111. static int decode_nal_sei_pic_timing(HEVCSEI *s, GetBitContext *gb, const HEVCParamSets *ps,
  112. void *logctx, int size)
  113. {
  114. HEVCSEIPictureTiming *h = &s->picture_timing;
  115. HEVCSPS *sps;
  116. if (!ps->sps_list[s->active_seq_parameter_set_id])
  117. return(AVERROR(ENOMEM));
  118. sps = (HEVCSPS*)ps->sps_list[s->active_seq_parameter_set_id]->data;
  119. if (sps->vui.frame_field_info_present_flag) {
  120. int pic_struct = get_bits(gb, 4);
  121. h->picture_struct = AV_PICTURE_STRUCTURE_UNKNOWN;
  122. if (pic_struct == 2 || pic_struct == 10 || pic_struct == 12) {
  123. av_log(logctx, AV_LOG_DEBUG, "BOTTOM Field\n");
  124. h->picture_struct = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
  125. } else if (pic_struct == 1 || pic_struct == 9 || pic_struct == 11) {
  126. av_log(logctx, AV_LOG_DEBUG, "TOP Field\n");
  127. h->picture_struct = AV_PICTURE_STRUCTURE_TOP_FIELD;
  128. }
  129. get_bits(gb, 2); // source_scan_type
  130. get_bits(gb, 1); // duplicate_flag
  131. skip_bits1(gb);
  132. size--;
  133. }
  134. skip_bits_long(gb, 8 * size);
  135. return 0;
  136. }
  137. static int decode_registered_user_data_closed_caption(HEVCSEIA53Caption *s, GetBitContext *gb,
  138. int size)
  139. {
  140. int flag;
  141. int user_data_type_code;
  142. int cc_count;
  143. if (size < 3)
  144. return AVERROR(EINVAL);
  145. user_data_type_code = get_bits(gb, 8);
  146. if (user_data_type_code == 0x3) {
  147. skip_bits(gb, 1); // reserved
  148. flag = get_bits(gb, 1); // process_cc_data_flag
  149. if (flag) {
  150. skip_bits(gb, 1);
  151. cc_count = get_bits(gb, 5);
  152. skip_bits(gb, 8); // reserved
  153. size -= 2;
  154. if (cc_count && size >= cc_count * 3) {
  155. const uint64_t new_size = (s->a53_caption_size + cc_count
  156. * UINT64_C(3));
  157. int i, ret;
  158. if (new_size > INT_MAX)
  159. return AVERROR(EINVAL);
  160. /* Allow merging of the cc data from two fields. */
  161. ret = av_reallocp(&s->a53_caption, new_size);
  162. if (ret < 0)
  163. return ret;
  164. for (i = 0; i < cc_count; i++) {
  165. s->a53_caption[s->a53_caption_size++] = get_bits(gb, 8);
  166. s->a53_caption[s->a53_caption_size++] = get_bits(gb, 8);
  167. s->a53_caption[s->a53_caption_size++] = get_bits(gb, 8);
  168. }
  169. skip_bits(gb, 8); // marker_bits
  170. }
  171. }
  172. } else {
  173. int i;
  174. for (i = 0; i < size - 1; i++)
  175. skip_bits(gb, 8);
  176. }
  177. return 0;
  178. }
  179. static int decode_nal_sei_user_data_registered_itu_t_t35(HEVCSEI *s, GetBitContext *gb,
  180. int size)
  181. {
  182. uint32_t country_code;
  183. uint32_t user_identifier;
  184. if (size < 7)
  185. return AVERROR(EINVAL);
  186. size -= 7;
  187. country_code = get_bits(gb, 8);
  188. if (country_code == 0xFF) {
  189. skip_bits(gb, 8);
  190. size--;
  191. }
  192. skip_bits(gb, 8);
  193. skip_bits(gb, 8);
  194. user_identifier = get_bits_long(gb, 32);
  195. switch (user_identifier) {
  196. case MKBETAG('G', 'A', '9', '4'):
  197. return decode_registered_user_data_closed_caption(&s->a53_caption, gb, size);
  198. default:
  199. skip_bits_long(gb, size * 8);
  200. break;
  201. }
  202. return 0;
  203. }
  204. static int decode_nal_sei_active_parameter_sets(HEVCSEI *s, GetBitContext *gb, void *logctx)
  205. {
  206. int num_sps_ids_minus1;
  207. int i;
  208. unsigned active_seq_parameter_set_id;
  209. get_bits(gb, 4); // active_video_parameter_set_id
  210. get_bits(gb, 1); // self_contained_cvs_flag
  211. get_bits(gb, 1); // num_sps_ids_minus1
  212. num_sps_ids_minus1 = get_ue_golomb_long(gb); // num_sps_ids_minus1
  213. if (num_sps_ids_minus1 < 0 || num_sps_ids_minus1 > 15) {
  214. av_log(logctx, AV_LOG_ERROR, "num_sps_ids_minus1 %d invalid\n", num_sps_ids_minus1);
  215. return AVERROR_INVALIDDATA;
  216. }
  217. active_seq_parameter_set_id = get_ue_golomb_long(gb);
  218. if (active_seq_parameter_set_id >= HEVC_MAX_SPS_COUNT) {
  219. av_log(logctx, AV_LOG_ERROR, "active_parameter_set_id %d invalid\n", active_seq_parameter_set_id);
  220. return AVERROR_INVALIDDATA;
  221. }
  222. s->active_seq_parameter_set_id = active_seq_parameter_set_id;
  223. for (i = 1; i <= num_sps_ids_minus1; i++)
  224. get_ue_golomb_long(gb); // active_seq_parameter_set_id[i]
  225. return 0;
  226. }
  227. static int decode_nal_sei_alternative_transfer(HEVCSEIAlternativeTransfer *s, GetBitContext *gb)
  228. {
  229. s->present = 1;
  230. s->preferred_transfer_characteristics = get_bits(gb, 8);
  231. return 0;
  232. }
  233. static int decode_nal_sei_prefix(GetBitContext *gb, HEVCSEI *s, const HEVCParamSets *ps,
  234. int type, int size, void *logctx)
  235. {
  236. switch (type) {
  237. case 256: // Mismatched value from HM 8.1
  238. return decode_nal_sei_decoded_picture_hash(&s->picture_hash, gb);
  239. case HEVC_SEI_TYPE_FRAME_PACKING:
  240. return decode_nal_sei_frame_packing_arrangement(&s->frame_packing, gb);
  241. case HEVC_SEI_TYPE_DISPLAY_ORIENTATION:
  242. return decode_nal_sei_display_orientation(&s->display_orientation, gb);
  243. case HEVC_SEI_TYPE_PICTURE_TIMING:
  244. return decode_nal_sei_pic_timing(s, gb, ps, logctx, size);
  245. case HEVC_SEI_TYPE_MASTERING_DISPLAY_INFO:
  246. return decode_nal_sei_mastering_display_info(&s->mastering_display, gb);
  247. case HEVC_SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO:
  248. return decode_nal_sei_content_light_info(&s->content_light, gb);
  249. case HEVC_SEI_TYPE_ACTIVE_PARAMETER_SETS:
  250. return decode_nal_sei_active_parameter_sets(s, gb, logctx);
  251. case HEVC_SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35:
  252. return decode_nal_sei_user_data_registered_itu_t_t35(s, gb, size);
  253. case HEVC_SEI_TYPE_ALTERNATIVE_TRANSFER_CHARACTERISTICS:
  254. return decode_nal_sei_alternative_transfer(&s->alternative_transfer, gb);
  255. default:
  256. av_log(logctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", type);
  257. skip_bits_long(gb, 8 * size);
  258. return 0;
  259. }
  260. }
  261. static int decode_nal_sei_suffix(GetBitContext *gb, HEVCSEI *s,
  262. int type, int size, void *logctx)
  263. {
  264. switch (type) {
  265. case HEVC_SEI_TYPE_DECODED_PICTURE_HASH:
  266. return decode_nal_sei_decoded_picture_hash(&s->picture_hash, gb);
  267. default:
  268. av_log(logctx, AV_LOG_DEBUG, "Skipped SUFFIX SEI %d\n", type);
  269. skip_bits_long(gb, 8 * size);
  270. return 0;
  271. }
  272. }
  273. static int decode_nal_sei_message(GetBitContext *gb, HEVCSEI *s,
  274. const HEVCParamSets *ps, int nal_unit_type,
  275. void *logctx)
  276. {
  277. int payload_type = 0;
  278. int payload_size = 0;
  279. int byte = 0xFF;
  280. av_log(logctx, AV_LOG_DEBUG, "Decoding SEI\n");
  281. while (byte == 0xFF) {
  282. byte = get_bits(gb, 8);
  283. payload_type += byte;
  284. }
  285. byte = 0xFF;
  286. while (byte == 0xFF) {
  287. byte = get_bits(gb, 8);
  288. payload_size += byte;
  289. }
  290. if (nal_unit_type == HEVC_NAL_SEI_PREFIX) {
  291. return decode_nal_sei_prefix(gb, s, ps, payload_type, payload_size, logctx);
  292. } else { /* nal_unit_type == NAL_SEI_SUFFIX */
  293. return decode_nal_sei_suffix(gb, s, payload_type, payload_size, logctx);
  294. }
  295. }
  296. static int more_rbsp_data(GetBitContext *gb)
  297. {
  298. return get_bits_left(gb) > 0 && show_bits(gb, 8) != 0x80;
  299. }
  300. int ff_hevc_decode_nal_sei(GetBitContext *gb, void *logctx, HEVCSEI *s,
  301. const HEVCParamSets *ps, int type)
  302. {
  303. int ret;
  304. do {
  305. ret = decode_nal_sei_message(gb, s, ps, type, logctx);
  306. if (ret < 0)
  307. return ret;
  308. } while (more_rbsp_data(gb));
  309. return 1;
  310. }
  311. void ff_hevc_reset_sei(HEVCSEI *s)
  312. {
  313. s->a53_caption.a53_caption_size = 0;
  314. av_freep(&s->a53_caption.a53_caption);
  315. }