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.

352 lines
11KB

  1. /*
  2. * HEVC Annex B format parser
  3. *
  4. * Copyright (C) 2012 - 2013 Guillaume Martres
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 "parser.h"
  24. #include "hevc.h"
  25. #include "golomb.h"
  26. #define START_CODE 0x000001 ///< start_code_prefix_one_3bytes
  27. typedef struct HEVCParseContext {
  28. HEVCContext h;
  29. ParseContext pc;
  30. } HEVCParseContext;
  31. /**
  32. * Find the end of the current frame in the bitstream.
  33. * @return the position of the first byte of the next frame, or END_NOT_FOUND
  34. */
  35. static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
  36. int buf_size)
  37. {
  38. int i;
  39. ParseContext *pc = &((HEVCParseContext *)s->priv_data)->pc;
  40. for (i = 0; i < buf_size; i++) {
  41. int nut;
  42. pc->state64 = (pc->state64 << 8) | buf[i];
  43. if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
  44. continue;
  45. nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
  46. // Beginning of access unit
  47. if ((nut >= NAL_VPS && nut <= NAL_AUD) || nut == NAL_SEI_PREFIX ||
  48. (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
  49. if (pc->frame_start_found) {
  50. pc->frame_start_found = 0;
  51. return i - 5;
  52. }
  53. } else if (nut <= NAL_RASL_R ||
  54. (nut >= NAL_BLA_W_LP && nut <= NAL_CRA_NUT)) {
  55. int first_slice_segment_in_pic_flag = buf[i] >> 7;
  56. if (first_slice_segment_in_pic_flag) {
  57. if (!pc->frame_start_found) {
  58. pc->frame_start_found = 1;
  59. } else { // First slice of next frame found
  60. pc->frame_start_found = 0;
  61. return i - 5;
  62. }
  63. }
  64. }
  65. }
  66. return END_NOT_FOUND;
  67. }
  68. /**
  69. * Parse NAL units of found picture and decode some basic information.
  70. *
  71. * @param s parser context.
  72. * @param avctx codec context.
  73. * @param buf buffer with field/frame data.
  74. * @param buf_size size of the buffer.
  75. */
  76. static inline int parse_nal_units(AVCodecParserContext *s, AVCodecContext *avctx,
  77. const uint8_t *buf, int buf_size)
  78. {
  79. HEVCContext *h = &((HEVCParseContext *)s->priv_data)->h;
  80. GetBitContext *gb = &h->HEVClc->gb;
  81. SliceHeader *sh = &h->sh;
  82. const uint8_t *buf_end = buf + buf_size;
  83. int state = -1, i;
  84. HEVCNAL *nal;
  85. /* set some sane default values */
  86. s->pict_type = AV_PICTURE_TYPE_I;
  87. s->key_frame = 0;
  88. s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
  89. h->avctx = avctx;
  90. if (!buf_size)
  91. return 0;
  92. if (h->nals_allocated < 1) {
  93. HEVCNAL *tmp = av_realloc_array(h->nals, 1, sizeof(*tmp));
  94. if (!tmp)
  95. return AVERROR(ENOMEM);
  96. h->nals = tmp;
  97. memset(h->nals, 0, sizeof(*tmp));
  98. h->nals_allocated = 1;
  99. }
  100. nal = &h->nals[0];
  101. for (;;) {
  102. int src_length, consumed;
  103. buf = avpriv_find_start_code(buf, buf_end, &state);
  104. if (--buf + 2 >= buf_end)
  105. break;
  106. src_length = buf_end - buf;
  107. h->nal_unit_type = (*buf >> 1) & 0x3f;
  108. h->temporal_id = (*(buf + 1) & 0x07) - 1;
  109. if (h->nal_unit_type <= NAL_CRA_NUT) {
  110. // Do not walk the whole buffer just to decode slice segment header
  111. if (src_length > 20)
  112. src_length = 20;
  113. }
  114. consumed = ff_hevc_extract_rbsp(h, buf, src_length, nal);
  115. if (consumed < 0)
  116. return consumed;
  117. init_get_bits8(gb, nal->data + 2, nal->size);
  118. switch (h->nal_unit_type) {
  119. case NAL_VPS:
  120. ff_hevc_decode_nal_vps(h);
  121. break;
  122. case NAL_SPS:
  123. ff_hevc_decode_nal_sps(h);
  124. break;
  125. case NAL_PPS:
  126. ff_hevc_decode_nal_pps(h);
  127. break;
  128. case NAL_SEI_PREFIX:
  129. case NAL_SEI_SUFFIX:
  130. ff_hevc_decode_nal_sei(h);
  131. break;
  132. case NAL_TRAIL_N:
  133. case NAL_TRAIL_R:
  134. case NAL_TSA_N:
  135. case NAL_TSA_R:
  136. case NAL_STSA_N:
  137. case NAL_STSA_R:
  138. case NAL_RADL_N:
  139. case NAL_RADL_R:
  140. case NAL_RASL_N:
  141. case NAL_RASL_R:
  142. case NAL_BLA_W_LP:
  143. case NAL_BLA_W_RADL:
  144. case NAL_BLA_N_LP:
  145. case NAL_IDR_W_RADL:
  146. case NAL_IDR_N_LP:
  147. case NAL_CRA_NUT:
  148. sh->first_slice_in_pic_flag = get_bits1(gb);
  149. s->picture_structure = h->picture_struct;
  150. s->field_order = h->picture_struct;
  151. if (IS_IRAP(h)) {
  152. s->key_frame = 1;
  153. sh->no_output_of_prior_pics_flag = get_bits1(gb);
  154. }
  155. sh->pps_id = get_ue_golomb(gb);
  156. if (sh->pps_id >= MAX_PPS_COUNT || !h->pps_list[sh->pps_id]) {
  157. av_log(h->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
  158. return AVERROR_INVALIDDATA;
  159. }
  160. h->pps = (HEVCPPS*)h->pps_list[sh->pps_id]->data;
  161. if (h->pps->sps_id >= MAX_SPS_COUNT || !h->sps_list[h->pps->sps_id]) {
  162. av_log(h->avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", h->pps->sps_id);
  163. return AVERROR_INVALIDDATA;
  164. }
  165. if (h->sps != (HEVCSPS*)h->sps_list[h->pps->sps_id]->data) {
  166. h->sps = (HEVCSPS*)h->sps_list[h->pps->sps_id]->data;
  167. h->vps = (HEVCVPS*)h->vps_list[h->sps->vps_id]->data;
  168. }
  169. if (!sh->first_slice_in_pic_flag) {
  170. int slice_address_length;
  171. if (h->pps->dependent_slice_segments_enabled_flag)
  172. sh->dependent_slice_segment_flag = get_bits1(gb);
  173. else
  174. sh->dependent_slice_segment_flag = 0;
  175. slice_address_length = av_ceil_log2_c(h->sps->ctb_width *
  176. h->sps->ctb_height);
  177. sh->slice_segment_addr = slice_address_length ? get_bits(gb, slice_address_length) : 0;
  178. if (sh->slice_segment_addr >= h->sps->ctb_width * h->sps->ctb_height) {
  179. av_log(h->avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
  180. sh->slice_segment_addr);
  181. return AVERROR_INVALIDDATA;
  182. }
  183. } else
  184. sh->dependent_slice_segment_flag = 0;
  185. if (sh->dependent_slice_segment_flag)
  186. break;
  187. for (i = 0; i < h->pps->num_extra_slice_header_bits; i++)
  188. skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
  189. sh->slice_type = get_ue_golomb(gb);
  190. if (!(sh->slice_type == I_SLICE || sh->slice_type == P_SLICE ||
  191. sh->slice_type == B_SLICE)) {
  192. av_log(h->avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
  193. sh->slice_type);
  194. return AVERROR_INVALIDDATA;
  195. }
  196. s->pict_type = sh->slice_type == B_SLICE ? AV_PICTURE_TYPE_B :
  197. sh->slice_type == P_SLICE ? AV_PICTURE_TYPE_P :
  198. AV_PICTURE_TYPE_I;
  199. if (h->pps->output_flag_present_flag)
  200. sh->pic_output_flag = get_bits1(gb);
  201. if (h->sps->separate_colour_plane_flag)
  202. sh->colour_plane_id = get_bits(gb, 2);
  203. if (!IS_IDR(h)) {
  204. sh->pic_order_cnt_lsb = get_bits(gb, h->sps->log2_max_poc_lsb);
  205. s->output_picture_number = h->poc = ff_hevc_compute_poc(h, sh->pic_order_cnt_lsb);
  206. } else
  207. s->output_picture_number = h->poc = 0;
  208. if (h->temporal_id == 0 &&
  209. h->nal_unit_type != NAL_TRAIL_N &&
  210. h->nal_unit_type != NAL_TSA_N &&
  211. h->nal_unit_type != NAL_STSA_N &&
  212. h->nal_unit_type != NAL_RADL_N &&
  213. h->nal_unit_type != NAL_RASL_N &&
  214. h->nal_unit_type != NAL_RADL_R &&
  215. h->nal_unit_type != NAL_RASL_R)
  216. h->pocTid0 = h->poc;
  217. return 0; /* no need to evaluate the rest */
  218. }
  219. buf += consumed;
  220. }
  221. /* didn't find a picture! */
  222. av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
  223. return -1;
  224. }
  225. static int hevc_parse(AVCodecParserContext *s,
  226. AVCodecContext *avctx,
  227. const uint8_t **poutbuf, int *poutbuf_size,
  228. const uint8_t *buf, int buf_size)
  229. {
  230. int next;
  231. ParseContext *pc = &((HEVCParseContext *)s->priv_data)->pc;
  232. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  233. next = buf_size;
  234. } else {
  235. next = hevc_find_frame_end(s, buf, buf_size);
  236. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  237. *poutbuf = NULL;
  238. *poutbuf_size = 0;
  239. return buf_size;
  240. }
  241. }
  242. parse_nal_units(s, avctx, buf, buf_size);
  243. *poutbuf = buf;
  244. *poutbuf_size = buf_size;
  245. return next;
  246. }
  247. // Split after the parameter sets at the beginning of the stream if they exist.
  248. static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
  249. {
  250. const uint8_t *ptr = buf, *end = buf + buf_size;
  251. uint32_t state = -1;
  252. int has_ps = 0, nut;
  253. while (ptr < end) {
  254. ptr = avpriv_find_start_code(ptr, end, &state);
  255. if ((state >> 8) != START_CODE)
  256. break;
  257. nut = (state >> 1) & 0x3F;
  258. if (nut >= NAL_VPS && nut <= NAL_PPS)
  259. has_ps = 1;
  260. else if (has_ps)
  261. return ptr - 4 - buf;
  262. else // no parameter set at the beginning of the stream
  263. return 0;
  264. }
  265. return 0;
  266. }
  267. static int hevc_init(AVCodecParserContext *s)
  268. {
  269. HEVCContext *h = &((HEVCParseContext *)s->priv_data)->h;
  270. h->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
  271. if (!h->HEVClc)
  272. return AVERROR(ENOMEM);
  273. h->skipped_bytes_pos_size = INT_MAX;
  274. return 0;
  275. }
  276. static void hevc_close(AVCodecParserContext *s)
  277. {
  278. int i;
  279. HEVCContext *h = &((HEVCParseContext *)s->priv_data)->h;
  280. ParseContext *pc = &((HEVCParseContext *)s->priv_data)->pc;
  281. av_freep(&h->skipped_bytes_pos);
  282. av_freep(&h->HEVClc);
  283. av_freep(&pc->buffer);
  284. for (i = 0; i < FF_ARRAY_ELEMS(h->vps_list); i++)
  285. av_buffer_unref(&h->vps_list[i]);
  286. for (i = 0; i < FF_ARRAY_ELEMS(h->sps_list); i++)
  287. av_buffer_unref(&h->sps_list[i]);
  288. for (i = 0; i < FF_ARRAY_ELEMS(h->pps_list); i++)
  289. av_buffer_unref(&h->pps_list[i]);
  290. h->sps = NULL;
  291. for (i = 0; i < h->nals_allocated; i++)
  292. av_freep(&h->nals[i].rbsp_buffer);
  293. av_freep(&h->nals);
  294. h->nals_allocated = 0;
  295. }
  296. AVCodecParser ff_hevc_parser = {
  297. .codec_ids = { AV_CODEC_ID_HEVC },
  298. .priv_data_size = sizeof(HEVCParseContext),
  299. .parser_init = hevc_init,
  300. .parser_parse = hevc_parse,
  301. .parser_close = hevc_close,
  302. .split = hevc_split,
  303. };