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.

243 lines
7.3KB

  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. AV_CODEC_ID_HEVC);
  71. if (ret < 0)
  72. return ret;
  73. for (i = 0; i < ctx->pkt.nb_nals; i++) {
  74. H2645NAL *nal = &ctx->pkt.nals[i];
  75. /* ignore everything except parameter sets and VCL NALUs */
  76. switch (nal->type) {
  77. case NAL_VPS: ff_hevc_decode_nal_vps(&nal->gb, avctx, &ctx->ps); break;
  78. case NAL_SPS: ff_hevc_decode_nal_sps(&nal->gb, avctx, &ctx->ps, 1); break;
  79. case NAL_PPS: ff_hevc_decode_nal_pps(&nal->gb, avctx, &ctx->ps); break;
  80. case NAL_TRAIL_R:
  81. case NAL_TRAIL_N:
  82. case NAL_TSA_N:
  83. case NAL_TSA_R:
  84. case NAL_STSA_N:
  85. case NAL_STSA_R:
  86. case NAL_BLA_W_LP:
  87. case NAL_BLA_W_RADL:
  88. case NAL_BLA_N_LP:
  89. case NAL_IDR_W_RADL:
  90. case NAL_IDR_N_LP:
  91. case NAL_CRA_NUT:
  92. case NAL_RADL_N:
  93. case NAL_RADL_R:
  94. case NAL_RASL_N:
  95. case NAL_RASL_R: hevc_parse_slice_header(s, nal, avctx); break;
  96. }
  97. }
  98. return 0;
  99. }
  100. /**
  101. * Find the end of the current frame in the bitstream.
  102. * @return the position of the first byte of the next frame, or END_NOT_FOUND
  103. */
  104. static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
  105. int buf_size)
  106. {
  107. HEVCParserContext *ctx = s->priv_data;
  108. ParseContext *pc = &ctx->pc;
  109. int i;
  110. for (i = 0; i < buf_size; i++) {
  111. int nut;
  112. pc->state64 = (pc->state64 << 8) | buf[i];
  113. if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
  114. continue;
  115. nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
  116. // Beginning of access unit
  117. if ((nut >= NAL_VPS && nut <= NAL_AUD) || nut == NAL_SEI_PREFIX ||
  118. (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
  119. if (pc->frame_start_found) {
  120. pc->frame_start_found = 0;
  121. return i - 5;
  122. }
  123. } else if (nut <= NAL_RASL_R ||
  124. (nut >= NAL_BLA_W_LP && nut <= NAL_CRA_NUT)) {
  125. int first_slice_segment_in_pic_flag = buf[i] >> 7;
  126. if (first_slice_segment_in_pic_flag) {
  127. if (!pc->frame_start_found) {
  128. pc->frame_start_found = 1;
  129. s->key_frame = nut >= NAL_BLA_W_LP && nut <= NAL_CRA_NUT;
  130. } else { // First slice of next frame found
  131. pc->frame_start_found = 0;
  132. return i - 5;
  133. }
  134. }
  135. }
  136. }
  137. return END_NOT_FOUND;
  138. }
  139. static int hevc_parse(AVCodecParserContext *s, AVCodecContext *avctx,
  140. const uint8_t **poutbuf, int *poutbuf_size,
  141. const uint8_t *buf, int buf_size)
  142. {
  143. int next;
  144. HEVCParserContext *ctx = s->priv_data;
  145. ParseContext *pc = &ctx->pc;
  146. if (avctx->extradata && !ctx->parsed_extradata) {
  147. parse_nal_units(s, avctx->extradata, avctx->extradata_size, avctx);
  148. ctx->parsed_extradata = 1;
  149. }
  150. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  151. next = buf_size;
  152. } else {
  153. next = hevc_find_frame_end(s, buf, buf_size);
  154. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  155. *poutbuf = NULL;
  156. *poutbuf_size = 0;
  157. return buf_size;
  158. }
  159. }
  160. parse_nal_units(s, buf, buf_size, avctx);
  161. *poutbuf = buf;
  162. *poutbuf_size = buf_size;
  163. return next;
  164. }
  165. // Split after the parameter sets at the beginning of the stream if they exist.
  166. static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
  167. {
  168. int i;
  169. uint32_t state = -1;
  170. int has_ps = 0;
  171. for (i = 0; i < buf_size; i++) {
  172. state = (state << 8) | buf[i];
  173. if (((state >> 8) & 0xFFFFFF) == START_CODE) {
  174. int nut = (state >> 1) & 0x3F;
  175. if (nut >= NAL_VPS && nut <= NAL_PPS)
  176. has_ps = 1;
  177. else if (has_ps)
  178. return i - 3;
  179. else // no parameter set at the beginning of the stream
  180. return 0;
  181. }
  182. }
  183. return 0;
  184. }
  185. static void hevc_parser_close(AVCodecParserContext *s)
  186. {
  187. HEVCParserContext *ctx = s->priv_data;
  188. int i;
  189. for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.vps_list); i++)
  190. av_buffer_unref(&ctx->ps.vps_list[i]);
  191. for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.sps_list); i++)
  192. av_buffer_unref(&ctx->ps.sps_list[i]);
  193. for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.pps_list); i++)
  194. av_buffer_unref(&ctx->ps.pps_list[i]);
  195. ff_h2645_packet_uninit(&ctx->pkt);
  196. av_freep(&ctx->pc.buffer);
  197. }
  198. AVCodecParser ff_hevc_parser = {
  199. .codec_ids = { AV_CODEC_ID_HEVC },
  200. .priv_data_size = sizeof(HEVCParserContext),
  201. .parser_parse = hevc_parse,
  202. .parser_close = hevc_parser_close,
  203. .split = hevc_split,
  204. };