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.

166 lines
5.4KB

  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 "hevc.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 = get_bits(gb, 8);
  31. for (cIdx = 0; cIdx < 3; cIdx++) {
  32. if (hash_type == 0) {
  33. s->is_md5 = 1;
  34. for (i = 0; i < 16; i++)
  35. s->md5[cIdx][i] = get_bits(gb, 8);
  36. } else if (hash_type == 1) {
  37. // picture_crc = get_bits(gb, 16);
  38. skip_bits(gb, 16);
  39. } else if (hash_type == 2) {
  40. // picture_checksum = get_bits(gb, 32);
  41. skip_bits(gb, 32);
  42. }
  43. }
  44. return 0;
  45. }
  46. static int decode_nal_sei_frame_packing_arrangement(HEVCSEIFramePacking *s, GetBitContext *gb)
  47. {
  48. get_ue_golomb(gb); // frame_packing_arrangement_id
  49. s->present = !get_bits1(gb);
  50. if (s->present) {
  51. s->arrangement_type = get_bits(gb, 7);
  52. s->quincunx_subsampling = get_bits1(gb);
  53. s->content_interpretation_type = get_bits(gb, 6);
  54. // spatial_flipping_flag, frame0_flipped_flag, field_views_flag
  55. skip_bits(gb, 3);
  56. s->current_frame_is_frame0_flag = get_bits1(gb);
  57. // frame0_self_contained_flag, frame1_self_contained_flag
  58. skip_bits(gb, 2);
  59. if (!s->quincunx_subsampling && s->arrangement_type != 5)
  60. skip_bits(gb, 16); // frame[01]_grid_position_[xy]
  61. skip_bits(gb, 8); // frame_packing_arrangement_reserved_byte
  62. skip_bits1(gb); // frame_packing_arrangement_persistence_flag
  63. }
  64. skip_bits1(gb); // upsampled_aspect_ratio_flag
  65. return 0;
  66. }
  67. static int decode_nal_sei_display_orientation(HEVCSEIDisplayOrientation *s, GetBitContext *gb)
  68. {
  69. s->present = !get_bits1(gb);
  70. if (s->present) {
  71. s->hflip = get_bits1(gb); // hor_flip
  72. s->vflip = get_bits1(gb); // ver_flip
  73. s->anticlockwise_rotation = get_bits(gb, 16);
  74. skip_bits1(gb); // display_orientation_persistence_flag
  75. }
  76. return 0;
  77. }
  78. static int decode_nal_sei_alternative_transfer(HEVCSEIAlternativeTransfer *s, GetBitContext *gb)
  79. {
  80. s->present = 1;
  81. s->preferred_transfer_characteristics = get_bits(gb, 8);
  82. return 0;
  83. }
  84. static int decode_nal_sei_prefix(GetBitContext *gb, void *logctx, HEVCSEI *s,
  85. int type, int size)
  86. {
  87. switch (type) {
  88. case 256: // Mismatched value from HM 8.1
  89. return decode_nal_sei_decoded_picture_hash(&s->picture_hash, gb);
  90. case HEVC_SEI_TYPE_FRAME_PACKING:
  91. return decode_nal_sei_frame_packing_arrangement(&s->frame_packing, gb);
  92. case HEVC_SEI_TYPE_DISPLAY_ORIENTATION:
  93. return decode_nal_sei_display_orientation(&s->display_orientation, gb);
  94. case HEVC_SEI_TYPE_ALTERNATIVE_TRANSFER_CHARACTERISTICS:
  95. return decode_nal_sei_alternative_transfer(&s->alternative_transfer, gb);
  96. default:
  97. av_log(logctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", type);
  98. skip_bits_long(gb, 8 * size);
  99. return 0;
  100. }
  101. }
  102. static int decode_nal_sei_suffix(GetBitContext *gb, void *logctx, HEVCSEI *s,
  103. int type, int size)
  104. {
  105. switch (type) {
  106. case HEVC_SEI_TYPE_DECODED_PICTURE_HASH:
  107. return decode_nal_sei_decoded_picture_hash(&s->picture_hash, gb);
  108. default:
  109. av_log(logctx, AV_LOG_DEBUG, "Skipped SUFFIX SEI %d\n", type);
  110. skip_bits_long(gb, 8 * size);
  111. return 0;
  112. }
  113. }
  114. static int decode_nal_sei_message(GetBitContext *gb, void *logctx, HEVCSEI *s,
  115. int type)
  116. {
  117. int payload_type = 0;
  118. int payload_size = 0;
  119. int byte = 0xFF;
  120. av_log(logctx, AV_LOG_DEBUG, "Decoding SEI\n");
  121. while (byte == 0xFF) {
  122. byte = get_bits(gb, 8);
  123. payload_type += byte;
  124. }
  125. byte = 0xFF;
  126. while (byte == 0xFF) {
  127. byte = get_bits(gb, 8);
  128. payload_size += byte;
  129. }
  130. if (type == HEVC_NAL_SEI_PREFIX) {
  131. return decode_nal_sei_prefix(gb, logctx, s, payload_type, payload_size);
  132. } else { /* nal_unit_type == NAL_SEI_SUFFIX */
  133. return decode_nal_sei_suffix(gb, logctx, s, payload_type, payload_size);
  134. }
  135. }
  136. static int more_rbsp_data(GetBitContext *gb)
  137. {
  138. return get_bits_left(gb) > 0 && show_bits(gb, 8) != 0x80;
  139. }
  140. int ff_hevc_decode_nal_sei(GetBitContext *gb, void *logctx, HEVCSEI *s,
  141. int type)
  142. {
  143. do {
  144. decode_nal_sei_message(gb, logctx, s, type);
  145. } while (more_rbsp_data(gb));
  146. return 0;
  147. }