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.

246 lines
7.6KB

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