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.

206 lines
6.9KB

  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. static void decode_nal_sei_decoded_picture_hash(HEVCContext *s)
  27. {
  28. int cIdx, i;
  29. uint8_t hash_type;
  30. //uint16_t picture_crc;
  31. //uint32_t picture_checksum;
  32. GetBitContext *gb = &s->HEVClc->gb;
  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. }
  48. static void decode_nal_sei_frame_packing_arrangement(HEVCContext *s)
  49. {
  50. GetBitContext *gb = &s->HEVClc->gb;
  51. get_ue_golomb(gb); // frame_packing_arrangement_id
  52. s->sei_frame_packing_present = !get_bits1(gb);
  53. if (s->sei_frame_packing_present) {
  54. s->frame_packing_arrangement_type = get_bits(gb, 7);
  55. s->quincunx_subsampling = get_bits1(gb);
  56. s->content_interpretation_type = get_bits(gb, 6);
  57. // the following skips spatial_flipping_flag frame0_flipped_flag
  58. // field_views_flag current_frame_is_frame0_flag
  59. // frame0_self_contained_flag frame1_self_contained_flag
  60. skip_bits(gb, 6);
  61. if (!s->quincunx_subsampling && s->frame_packing_arrangement_type != 5)
  62. skip_bits(gb, 16); // frame[01]_grid_position_[xy]
  63. skip_bits(gb, 8); // frame_packing_arrangement_reserved_byte
  64. skip_bits1(gb); // frame_packing_arrangement_persistance_flag
  65. }
  66. skip_bits1(gb); // upsampled_aspect_ratio_flag
  67. }
  68. static void decode_nal_sei_display_orientation(HEVCContext *s)
  69. {
  70. GetBitContext *gb = &s->HEVClc->gb;
  71. s->sei_display_orientation_present = !get_bits1(gb);
  72. if (s->sei_display_orientation_present) {
  73. s->sei_hflip = get_bits1(gb); // hor_flip
  74. s->sei_vflip = get_bits1(gb); // ver_flip
  75. s->sei_anticlockwise_rotation = get_bits(gb, 16);
  76. skip_bits1(gb); // display_orientation_persistence_flag
  77. }
  78. }
  79. static int decode_pic_timing(HEVCContext *s)
  80. {
  81. GetBitContext *gb = &s->HEVClc->gb;
  82. HEVCSPS *sps;
  83. if (!s->sps_list[s->active_seq_parameter_set_id])
  84. return(AVERROR(ENOMEM));
  85. sps = (HEVCSPS*)s->sps_list[s->active_seq_parameter_set_id]->data;
  86. if (sps->vui.frame_field_info_present_flag) {
  87. int pic_struct = get_bits(gb, 4);
  88. s->picture_struct = AV_PICTURE_STRUCTURE_UNKNOWN;
  89. if (pic_struct == 2) {
  90. av_log(s->avctx, AV_LOG_DEBUG, "BOTTOM Field\n");
  91. s->picture_struct = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
  92. } else if (pic_struct == 1) {
  93. av_log(s->avctx, AV_LOG_DEBUG, "TOP Field\n");
  94. s->picture_struct = AV_PICTURE_STRUCTURE_TOP_FIELD;
  95. }
  96. get_bits(gb, 2); // source_scan_type
  97. get_bits(gb, 1); // duplicate_flag
  98. }
  99. return 1;
  100. }
  101. static int active_parameter_sets(HEVCContext *s)
  102. {
  103. GetBitContext *gb = &s->HEVClc->gb;
  104. int num_sps_ids_minus1;
  105. int i;
  106. unsigned active_seq_parameter_set_id;
  107. get_bits(gb, 4); // active_video_parameter_set_id
  108. get_bits(gb, 1); // self_contained_cvs_flag
  109. get_bits(gb, 1); // num_sps_ids_minus1
  110. num_sps_ids_minus1 = get_ue_golomb_long(gb); // num_sps_ids_minus1
  111. active_seq_parameter_set_id = get_ue_golomb_long(gb);
  112. if (active_seq_parameter_set_id >= MAX_SPS_COUNT) {
  113. av_log(s->avctx, AV_LOG_ERROR, "active_parameter_set_id %d invalid\n", active_seq_parameter_set_id);
  114. return AVERROR_INVALIDDATA;
  115. }
  116. s->active_seq_parameter_set_id = active_seq_parameter_set_id;
  117. for (i = 1; i <= num_sps_ids_minus1; i++)
  118. get_ue_golomb_long(gb); // active_seq_parameter_set_id[i]
  119. return 0;
  120. }
  121. static int decode_nal_sei_message(HEVCContext *s)
  122. {
  123. GetBitContext *gb = &s->HEVClc->gb;
  124. int payload_type = 0;
  125. int payload_size = 0;
  126. int byte = 0xFF;
  127. av_log(s->avctx, AV_LOG_DEBUG, "Decoding SEI\n");
  128. while (byte == 0xFF) {
  129. byte = get_bits(gb, 8);
  130. payload_type += byte;
  131. }
  132. byte = 0xFF;
  133. while (byte == 0xFF) {
  134. byte = get_bits(gb, 8);
  135. payload_size += byte;
  136. }
  137. if (s->nal_unit_type == NAL_SEI_PREFIX) {
  138. if (payload_type == 256 /*&& s->decode_checksum_sei*/) {
  139. decode_nal_sei_decoded_picture_hash(s);
  140. } else if (payload_type == 45) {
  141. decode_nal_sei_frame_packing_arrangement(s);
  142. } else if (payload_type == 47) {
  143. decode_nal_sei_display_orientation(s);
  144. } else if (payload_type == 1){
  145. int ret = decode_pic_timing(s);
  146. av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", payload_type);
  147. skip_bits(gb, 8 * payload_size);
  148. return ret;
  149. } else if (payload_type == 129){
  150. active_parameter_sets(s);
  151. av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", payload_type);
  152. } else {
  153. av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", payload_type);
  154. skip_bits(gb, 8*payload_size);
  155. }
  156. } else { /* nal_unit_type == NAL_SEI_SUFFIX */
  157. if (payload_type == 132 /* && s->decode_checksum_sei */)
  158. decode_nal_sei_decoded_picture_hash(s);
  159. else {
  160. av_log(s->avctx, AV_LOG_DEBUG, "Skipped SUFFIX SEI %d\n", payload_type);
  161. skip_bits(gb, 8 * payload_size);
  162. }
  163. }
  164. return 1;
  165. }
  166. static int more_rbsp_data(GetBitContext *gb)
  167. {
  168. return get_bits_left(gb) > 0 && show_bits(gb, 8) != 0x80;
  169. }
  170. int ff_hevc_decode_nal_sei(HEVCContext *s)
  171. {
  172. int ret;
  173. do {
  174. ret = decode_nal_sei_message(s);
  175. if (ret < 0)
  176. return(AVERROR(ENOMEM));
  177. } while (more_rbsp_data(&s->HEVClc->gb));
  178. return 1;
  179. }