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