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.

190 lines
6.3KB

  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 Libav.
  9. *
  10. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include "golomb_legacy.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. GetBitContext *gb = &s->HEVClc.gb;
  57. uint8_t hash_type = get_bits(gb, 8);
  58. for (cIdx = 0; cIdx < 3; cIdx++) {
  59. if (hash_type == 0) {
  60. s->is_md5 = 1;
  61. for (i = 0; i < 16; i++)
  62. s->md5[cIdx][i] = get_bits(gb, 8);
  63. } else if (hash_type == 1) {
  64. // picture_crc = get_bits(gb, 16);
  65. skip_bits(gb, 16);
  66. } else if (hash_type == 2) {
  67. // picture_checksum = get_bits(gb, 32);
  68. skip_bits(gb, 32);
  69. }
  70. }
  71. return 0;
  72. }
  73. static int decode_nal_sei_frame_packing_arrangement(HEVCContext *s)
  74. {
  75. GetBitContext *gb = &s->HEVClc.gb;
  76. get_ue_golomb(gb); // frame_packing_arrangement_id
  77. s->sei_frame_packing_present = !get_bits1(gb);
  78. if (s->sei_frame_packing_present) {
  79. s->frame_packing_arrangement_type = get_bits(gb, 7);
  80. s->quincunx_subsampling = get_bits1(gb);
  81. s->content_interpretation_type = get_bits(gb, 6);
  82. // the following skips spatial_flipping_flag frame0_flipped_flag
  83. // field_views_flag current_frame_is_frame0_flag
  84. // frame0_self_contained_flag frame1_self_contained_flag
  85. skip_bits(gb, 6);
  86. if (!s->quincunx_subsampling && s->frame_packing_arrangement_type != 5)
  87. skip_bits(gb, 16); // frame[01]_grid_position_[xy]
  88. skip_bits(gb, 8); // frame_packing_arrangement_reserved_byte
  89. skip_bits1(gb); // frame_packing_arrangement_persistence_flag
  90. }
  91. skip_bits1(gb); // upsampled_aspect_ratio_flag
  92. return 0;
  93. }
  94. static int decode_nal_sei_display_orientation(HEVCContext *s)
  95. {
  96. GetBitContext *gb = &s->HEVClc.gb;
  97. s->sei_display_orientation_present = !get_bits1(gb);
  98. if (s->sei_display_orientation_present) {
  99. s->sei_hflip = get_bits1(gb); // hor_flip
  100. s->sei_vflip = get_bits1(gb); // ver_flip
  101. s->sei_anticlockwise_rotation = get_bits(gb, 16);
  102. skip_bits1(gb); // display_orientation_persistence_flag
  103. }
  104. return 0;
  105. }
  106. static int decode_nal_sei_prefix(HEVCContext *s, int type, int size)
  107. {
  108. GetBitContext *gb = &s->HEVClc.gb;
  109. switch (type) {
  110. case 256: // Mismatched value from HM 8.1
  111. return decode_nal_sei_decoded_picture_hash(s);
  112. case SEI_TYPE_FRAME_PACKING:
  113. return decode_nal_sei_frame_packing_arrangement(s);
  114. case SEI_TYPE_DISPLAY_ORIENTATION:
  115. return decode_nal_sei_display_orientation(s);
  116. default:
  117. av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", type);
  118. skip_bits_long(gb, 8 * size);
  119. return 0;
  120. }
  121. }
  122. static int decode_nal_sei_suffix(HEVCContext *s, int type, int size)
  123. {
  124. GetBitContext *gb = &s->HEVClc.gb;
  125. switch (type) {
  126. case SEI_TYPE_DECODED_PICTURE_HASH:
  127. return decode_nal_sei_decoded_picture_hash(s);
  128. default:
  129. av_log(s->avctx, AV_LOG_DEBUG, "Skipped SUFFIX SEI %d\n", type);
  130. skip_bits_long(gb, 8 * size);
  131. return 0;
  132. }
  133. }
  134. static int decode_nal_sei_message(HEVCContext *s)
  135. {
  136. GetBitContext *gb = &s->HEVClc.gb;
  137. int payload_type = 0;
  138. int payload_size = 0;
  139. int byte = 0xFF;
  140. av_log(s->avctx, AV_LOG_DEBUG, "Decoding SEI\n");
  141. while (byte == 0xFF) {
  142. byte = get_bits(gb, 8);
  143. payload_type += byte;
  144. }
  145. byte = 0xFF;
  146. while (byte == 0xFF) {
  147. byte = get_bits(gb, 8);
  148. payload_size += byte;
  149. }
  150. if (s->nal_unit_type == HEVC_NAL_SEI_PREFIX) {
  151. return decode_nal_sei_prefix(s, payload_type, payload_size);
  152. } else { /* nal_unit_type == NAL_SEI_SUFFIX */
  153. return decode_nal_sei_suffix(s, payload_type, payload_size);
  154. }
  155. }
  156. static int more_rbsp_data(GetBitContext *gb)
  157. {
  158. return get_bits_left(gb) > 0 && show_bits(gb, 8) != 0x80;
  159. }
  160. int ff_hevc_decode_nal_sei(HEVCContext *s)
  161. {
  162. do {
  163. decode_nal_sei_message(s);
  164. } while (more_rbsp_data(&s->HEVClc.gb));
  165. return 0;
  166. }