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.

242 lines
7.2KB

  1. /*
  2. * HEVC Annex B format parser
  3. *
  4. * Copyright (C) 2012 - 2013 Guillaume Martres
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/common.h"
  23. #include "golomb.h"
  24. #include "hevc.h"
  25. #include "h2645_parse.h"
  26. #include "parser.h"
  27. #define START_CODE 0x000001 ///< start_code_prefix_one_3bytes
  28. #define IS_IRAP_NAL(nal) (nal->type >= 16 && nal->type <= 23)
  29. typedef struct HEVCParserContext {
  30. ParseContext pc;
  31. H2645Packet pkt;
  32. HEVCParamSets ps;
  33. int parsed_extradata;
  34. } HEVCParserContext;
  35. static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal,
  36. AVCodecContext *avctx)
  37. {
  38. HEVCParserContext *ctx = s->priv_data;
  39. GetBitContext *gb = &nal->gb;
  40. HEVCPPS *pps;
  41. HEVCSPS *sps;
  42. unsigned int pps_id;
  43. get_bits1(gb); // first slice in pic
  44. if (IS_IRAP_NAL(nal))
  45. get_bits1(gb); // no output of prior pics
  46. pps_id = get_ue_golomb_long(gb);
  47. if (pps_id >= MAX_PPS_COUNT || !ctx->ps.pps_list[pps_id]) {
  48. av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
  49. return AVERROR_INVALIDDATA;
  50. }
  51. pps = (HEVCPPS*)ctx->ps.pps_list[pps_id]->data;
  52. sps = (HEVCSPS*)ctx->ps.sps_list[pps->sps_id]->data;
  53. /* export the stream parameters */
  54. s->coded_width = sps->width;
  55. s->coded_height = sps->height;
  56. s->width = sps->output_width;
  57. s->height = sps->output_height;
  58. s->format = sps->pix_fmt;
  59. avctx->profile = sps->ptl.general_ptl.profile_idc;
  60. avctx->level = sps->ptl.general_ptl.level_idc;
  61. /* ignore the rest for now*/
  62. return 0;
  63. }
  64. static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
  65. int buf_size, AVCodecContext *avctx)
  66. {
  67. HEVCParserContext *ctx = s->priv_data;
  68. int ret, i;
  69. ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx, 0, 0);
  70. if (ret < 0)
  71. return ret;
  72. for (i = 0; i < ctx->pkt.nb_nals; i++) {
  73. H2645NAL *nal = &ctx->pkt.nals[i];
  74. /* ignore everything except parameter sets and VCL NALUs */
  75. switch (nal->type) {
  76. case NAL_VPS: ff_hevc_decode_nal_vps(&nal->gb, avctx, &ctx->ps); break;
  77. case NAL_SPS: ff_hevc_decode_nal_sps(&nal->gb, avctx, &ctx->ps, 1); break;
  78. case NAL_PPS: ff_hevc_decode_nal_pps(&nal->gb, avctx, &ctx->ps); break;
  79. case NAL_TRAIL_R:
  80. case NAL_TRAIL_N:
  81. case NAL_TSA_N:
  82. case NAL_TSA_R:
  83. case NAL_STSA_N:
  84. case NAL_STSA_R:
  85. case NAL_BLA_W_LP:
  86. case NAL_BLA_W_RADL:
  87. case NAL_BLA_N_LP:
  88. case NAL_IDR_W_RADL:
  89. case NAL_IDR_N_LP:
  90. case NAL_CRA_NUT:
  91. case NAL_RADL_N:
  92. case NAL_RADL_R:
  93. case NAL_RASL_N:
  94. case NAL_RASL_R: hevc_parse_slice_header(s, nal, avctx); break;
  95. }
  96. }
  97. return 0;
  98. }
  99. /**
  100. * Find the end of the current frame in the bitstream.
  101. * @return the position of the first byte of the next frame, or END_NOT_FOUND
  102. */
  103. static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
  104. int buf_size)
  105. {
  106. HEVCParserContext *ctx = s->priv_data;
  107. ParseContext *pc = &ctx->pc;
  108. int i;
  109. for (i = 0; i < buf_size; i++) {
  110. int nut;
  111. pc->state64 = (pc->state64 << 8) | buf[i];
  112. if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
  113. continue;
  114. nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
  115. // Beginning of access unit
  116. if ((nut >= NAL_VPS && nut <= NAL_AUD) || nut == NAL_SEI_PREFIX ||
  117. (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
  118. if (pc->frame_start_found) {
  119. pc->frame_start_found = 0;
  120. return i - 5;
  121. }
  122. } else if (nut <= NAL_RASL_R ||
  123. (nut >= NAL_BLA_W_LP && nut <= NAL_CRA_NUT)) {
  124. int first_slice_segment_in_pic_flag = buf[i] >> 7;
  125. if (first_slice_segment_in_pic_flag) {
  126. if (!pc->frame_start_found) {
  127. pc->frame_start_found = 1;
  128. s->key_frame = nut >= NAL_BLA_W_LP && nut <= NAL_CRA_NUT;
  129. } else { // First slice of next frame found
  130. pc->frame_start_found = 0;
  131. return i - 5;
  132. }
  133. }
  134. }
  135. }
  136. return END_NOT_FOUND;
  137. }
  138. static int hevc_parse(AVCodecParserContext *s, AVCodecContext *avctx,
  139. const uint8_t **poutbuf, int *poutbuf_size,
  140. const uint8_t *buf, int buf_size)
  141. {
  142. int next;
  143. HEVCParserContext *ctx = s->priv_data;
  144. ParseContext *pc = &ctx->pc;
  145. if (avctx->extradata && !ctx->parsed_extradata) {
  146. parse_nal_units(s, avctx->extradata, avctx->extradata_size, avctx);
  147. ctx->parsed_extradata = 1;
  148. }
  149. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  150. next = buf_size;
  151. } else {
  152. next = hevc_find_frame_end(s, buf, buf_size);
  153. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  154. *poutbuf = NULL;
  155. *poutbuf_size = 0;
  156. return buf_size;
  157. }
  158. }
  159. parse_nal_units(s, buf, buf_size, avctx);
  160. *poutbuf = buf;
  161. *poutbuf_size = buf_size;
  162. return next;
  163. }
  164. // Split after the parameter sets at the beginning of the stream if they exist.
  165. static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
  166. {
  167. int i;
  168. uint32_t state = -1;
  169. int has_ps = 0;
  170. for (i = 0; i < buf_size; i++) {
  171. state = (state << 8) | buf[i];
  172. if (((state >> 8) & 0xFFFFFF) == START_CODE) {
  173. int nut = (state >> 1) & 0x3F;
  174. if (nut >= NAL_VPS && nut <= NAL_PPS)
  175. has_ps = 1;
  176. else if (has_ps)
  177. return i - 3;
  178. else // no parameter set at the beginning of the stream
  179. return 0;
  180. }
  181. }
  182. return 0;
  183. }
  184. static void hevc_parser_close(AVCodecParserContext *s)
  185. {
  186. HEVCParserContext *ctx = s->priv_data;
  187. int i;
  188. for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.vps_list); i++)
  189. av_buffer_unref(&ctx->ps.vps_list[i]);
  190. for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.sps_list); i++)
  191. av_buffer_unref(&ctx->ps.sps_list[i]);
  192. for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.pps_list); i++)
  193. av_buffer_unref(&ctx->ps.pps_list[i]);
  194. ff_h2645_packet_uninit(&ctx->pkt);
  195. av_freep(&ctx->pc.buffer);
  196. }
  197. AVCodecParser ff_hevc_parser = {
  198. .codec_ids = { AV_CODEC_ID_HEVC },
  199. .priv_data_size = sizeof(HEVCParserContext),
  200. .parser_parse = hevc_parse,
  201. .parser_close = hevc_parser_close,
  202. .split = hevc_split,
  203. };