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.

244 lines
7.4KB

  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 "hevcdec.h"
  26. #include "h2645_parse.h"
  27. #include "parser.h"
  28. #define START_CODE 0x000001 ///< start_code_prefix_one_3bytes
  29. #define IS_IRAP_NAL(nal) (nal->type >= 16 && nal->type <= 23)
  30. typedef struct HEVCParserContext {
  31. ParseContext pc;
  32. H2645Packet pkt;
  33. HEVCParamSets ps;
  34. int parsed_extradata;
  35. } HEVCParserContext;
  36. static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal,
  37. AVCodecContext *avctx)
  38. {
  39. HEVCParserContext *ctx = s->priv_data;
  40. GetBitContext *gb = &nal->gb;
  41. HEVCPPS *pps;
  42. HEVCSPS *sps;
  43. unsigned int pps_id;
  44. get_bits1(gb); // first slice in pic
  45. if (IS_IRAP_NAL(nal))
  46. get_bits1(gb); // no output of prior pics
  47. pps_id = get_ue_golomb_long(gb);
  48. if (pps_id >= HEVC_MAX_PPS_COUNT || !ctx->ps.pps_list[pps_id]) {
  49. av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
  50. return AVERROR_INVALIDDATA;
  51. }
  52. pps = (HEVCPPS*)ctx->ps.pps_list[pps_id]->data;
  53. sps = (HEVCSPS*)ctx->ps.sps_list[pps->sps_id]->data;
  54. /* export the stream parameters */
  55. s->coded_width = sps->width;
  56. s->coded_height = sps->height;
  57. s->width = sps->output_width;
  58. s->height = sps->output_height;
  59. s->format = sps->pix_fmt;
  60. avctx->profile = sps->ptl.general_ptl.profile_idc;
  61. avctx->level = sps->ptl.general_ptl.level_idc;
  62. /* ignore the rest for now*/
  63. return 0;
  64. }
  65. static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
  66. int buf_size, AVCodecContext *avctx)
  67. {
  68. HEVCParserContext *ctx = s->priv_data;
  69. int ret, i;
  70. ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx, 0, 0,
  71. AV_CODEC_ID_HEVC);
  72. if (ret < 0)
  73. return ret;
  74. for (i = 0; i < ctx->pkt.nb_nals; i++) {
  75. H2645NAL *nal = &ctx->pkt.nals[i];
  76. /* ignore everything except parameter sets and VCL NALUs */
  77. switch (nal->type) {
  78. case HEVC_NAL_VPS: ff_hevc_decode_nal_vps(&nal->gb, avctx, &ctx->ps); break;
  79. case HEVC_NAL_SPS: ff_hevc_decode_nal_sps(&nal->gb, avctx, &ctx->ps, 1); break;
  80. case HEVC_NAL_PPS: ff_hevc_decode_nal_pps(&nal->gb, avctx, &ctx->ps); break;
  81. case HEVC_NAL_TRAIL_R:
  82. case HEVC_NAL_TRAIL_N:
  83. case HEVC_NAL_TSA_N:
  84. case HEVC_NAL_TSA_R:
  85. case HEVC_NAL_STSA_N:
  86. case HEVC_NAL_STSA_R:
  87. case HEVC_NAL_BLA_W_LP:
  88. case HEVC_NAL_BLA_W_RADL:
  89. case HEVC_NAL_BLA_N_LP:
  90. case HEVC_NAL_IDR_W_RADL:
  91. case HEVC_NAL_IDR_N_LP:
  92. case HEVC_NAL_CRA_NUT:
  93. case HEVC_NAL_RADL_N:
  94. case HEVC_NAL_RADL_R:
  95. case HEVC_NAL_RASL_N:
  96. case HEVC_NAL_RASL_R: hevc_parse_slice_header(s, nal, avctx); break;
  97. }
  98. }
  99. return 0;
  100. }
  101. /**
  102. * Find the end of the current frame in the bitstream.
  103. * @return the position of the first byte of the next frame, or END_NOT_FOUND
  104. */
  105. static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
  106. int buf_size)
  107. {
  108. HEVCParserContext *ctx = s->priv_data;
  109. ParseContext *pc = &ctx->pc;
  110. int i;
  111. for (i = 0; i < buf_size; i++) {
  112. int nut;
  113. pc->state64 = (pc->state64 << 8) | buf[i];
  114. if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
  115. continue;
  116. nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
  117. // Beginning of access unit
  118. if ((nut >= HEVC_NAL_VPS && nut <= HEVC_NAL_AUD) || nut == HEVC_NAL_SEI_PREFIX ||
  119. (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
  120. if (pc->frame_start_found) {
  121. pc->frame_start_found = 0;
  122. return i - 5;
  123. }
  124. } else if (nut <= HEVC_NAL_RASL_R ||
  125. (nut >= HEVC_NAL_BLA_W_LP && nut <= HEVC_NAL_CRA_NUT)) {
  126. int first_slice_segment_in_pic_flag = buf[i] >> 7;
  127. if (first_slice_segment_in_pic_flag) {
  128. if (!pc->frame_start_found) {
  129. pc->frame_start_found = 1;
  130. s->key_frame = nut >= HEVC_NAL_BLA_W_LP && nut <= HEVC_NAL_CRA_NUT;
  131. } else { // First slice of next frame found
  132. pc->frame_start_found = 0;
  133. return i - 5;
  134. }
  135. }
  136. }
  137. }
  138. return END_NOT_FOUND;
  139. }
  140. static int hevc_parse(AVCodecParserContext *s, AVCodecContext *avctx,
  141. const uint8_t **poutbuf, int *poutbuf_size,
  142. const uint8_t *buf, int buf_size)
  143. {
  144. int next;
  145. HEVCParserContext *ctx = s->priv_data;
  146. ParseContext *pc = &ctx->pc;
  147. if (avctx->extradata && !ctx->parsed_extradata) {
  148. parse_nal_units(s, avctx->extradata, avctx->extradata_size, avctx);
  149. ctx->parsed_extradata = 1;
  150. }
  151. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  152. next = buf_size;
  153. } else {
  154. next = hevc_find_frame_end(s, buf, buf_size);
  155. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  156. *poutbuf = NULL;
  157. *poutbuf_size = 0;
  158. return buf_size;
  159. }
  160. }
  161. parse_nal_units(s, buf, buf_size, avctx);
  162. *poutbuf = buf;
  163. *poutbuf_size = buf_size;
  164. return next;
  165. }
  166. // Split after the parameter sets at the beginning of the stream if they exist.
  167. static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
  168. {
  169. int i;
  170. uint32_t state = -1;
  171. int has_ps = 0;
  172. for (i = 0; i < buf_size; i++) {
  173. state = (state << 8) | buf[i];
  174. if (((state >> 8) & 0xFFFFFF) == START_CODE) {
  175. int nut = (state >> 1) & 0x3F;
  176. if (nut >= HEVC_NAL_VPS && nut <= HEVC_NAL_PPS)
  177. has_ps = 1;
  178. else if (has_ps)
  179. return i - 3;
  180. else // no parameter set at the beginning of the stream
  181. return 0;
  182. }
  183. }
  184. return 0;
  185. }
  186. static void hevc_parser_close(AVCodecParserContext *s)
  187. {
  188. HEVCParserContext *ctx = s->priv_data;
  189. int i;
  190. for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.vps_list); i++)
  191. av_buffer_unref(&ctx->ps.vps_list[i]);
  192. for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.sps_list); i++)
  193. av_buffer_unref(&ctx->ps.sps_list[i]);
  194. for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.pps_list); i++)
  195. av_buffer_unref(&ctx->ps.pps_list[i]);
  196. ff_h2645_packet_uninit(&ctx->pkt);
  197. av_freep(&ctx->pc.buffer);
  198. }
  199. AVCodecParser ff_hevc_parser = {
  200. .codec_ids = { AV_CODEC_ID_HEVC },
  201. .priv_data_size = sizeof(HEVCParserContext),
  202. .parser_parse = hevc_parse,
  203. .parser_close = hevc_parser_close,
  204. .split = hevc_split,
  205. };