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.

194 lines
6.5KB

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