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.

346 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, int buf_size)
  36. {
  37. int i;
  38. ParseContext *pc = &((HEVCParseContext *)s->priv_data)->pc;
  39. for (i = 0; i < buf_size; i++) {
  40. int nut;
  41. pc->state64 = (pc->state64 << 8) | buf[i];
  42. if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
  43. continue;
  44. nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
  45. // Beginning of access unit
  46. if ((nut >= NAL_VPS && nut <= NAL_AUD) || nut == NAL_SEI_PREFIX ||
  47. (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
  48. if (pc->frame_start_found) {
  49. pc->frame_start_found = 0;
  50. return i - 5;
  51. }
  52. } else if (nut <= NAL_RASL_R ||
  53. (nut >= NAL_BLA_W_LP && nut <= NAL_CRA_NUT)) {
  54. int first_slice_segment_in_pic_flag = buf[i] >> 7;
  55. if (first_slice_segment_in_pic_flag) {
  56. if (!pc->frame_start_found) {
  57. pc->frame_start_found = 1;
  58. } else { // First slice of next frame found
  59. pc->frame_start_found = 0;
  60. return i - 5;
  61. }
  62. }
  63. }
  64. }
  65. return END_NOT_FOUND;
  66. }
  67. /**
  68. * Parse NAL units of found picture and decode some basic information.
  69. *
  70. * @param s parser context.
  71. * @param avctx codec context.
  72. * @param buf buffer with field/frame data.
  73. * @param buf_size size of the buffer.
  74. */
  75. static inline int parse_nal_units(AVCodecParserContext *s,
  76. 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. if (h->nal_unit_type >= 16 && h->nal_unit_type <= 23) {
  150. s->key_frame = 1;
  151. sh->no_output_of_prior_pics_flag = get_bits1(gb);
  152. }
  153. sh->pps_id = get_ue_golomb(gb);
  154. if (sh->pps_id >= MAX_PPS_COUNT || !h->pps_list[sh->pps_id]) {
  155. av_log(h->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
  156. return AVERROR_INVALIDDATA;
  157. }
  158. h->pps = (HEVCPPS*)h->pps_list[sh->pps_id]->data;
  159. if (h->pps->sps_id >= MAX_SPS_COUNT || !h->sps_list[h->pps->sps_id]) {
  160. av_log(h->avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", h->pps->sps_id);
  161. return AVERROR_INVALIDDATA;
  162. }
  163. if (h->sps != (HEVCSPS*)h->sps_list[h->pps->sps_id]->data) {
  164. h->sps = (HEVCSPS*)h->sps_list[h->pps->sps_id]->data;
  165. h->vps = h->vps_list[h->sps->vps_id];
  166. }
  167. if (!sh->first_slice_in_pic_flag) {
  168. int slice_address_length;
  169. if (h->pps->dependent_slice_segments_enabled_flag)
  170. sh->dependent_slice_segment_flag = get_bits1(gb);
  171. else
  172. sh->dependent_slice_segment_flag = 0;
  173. slice_address_length = av_ceil_log2_c(h->sps->ctb_width *
  174. h->sps->ctb_height);
  175. sh->slice_segment_addr = get_bits(gb, slice_address_length);
  176. if (sh->slice_segment_addr >= h->sps->ctb_width * h->sps->ctb_height) {
  177. av_log(h->avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
  178. sh->slice_segment_addr);
  179. return AVERROR_INVALIDDATA;
  180. }
  181. } else
  182. sh->dependent_slice_segment_flag = 0;
  183. if (sh->dependent_slice_segment_flag)
  184. break;
  185. for (i = 0; i < h->pps->num_extra_slice_header_bits; i++)
  186. skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
  187. sh->slice_type = get_ue_golomb(gb);
  188. if (!(sh->slice_type == I_SLICE || sh->slice_type == P_SLICE ||
  189. sh->slice_type == B_SLICE)) {
  190. av_log(h->avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
  191. sh->slice_type);
  192. return AVERROR_INVALIDDATA;
  193. }
  194. s->pict_type = sh->slice_type == B_SLICE ? AV_PICTURE_TYPE_B :
  195. sh->slice_type == P_SLICE ? AV_PICTURE_TYPE_P :
  196. AV_PICTURE_TYPE_I;
  197. if (h->pps->output_flag_present_flag)
  198. sh->pic_output_flag = get_bits1(gb);
  199. if (h->sps->separate_colour_plane_flag)
  200. sh->colour_plane_id = get_bits(gb, 2);
  201. if (!IS_IDR(h)) {
  202. sh->pic_order_cnt_lsb = get_bits(gb, h->sps->log2_max_poc_lsb);
  203. s->output_picture_number = h->poc = ff_hevc_compute_poc(h, sh->pic_order_cnt_lsb);
  204. } else
  205. s->output_picture_number = h->poc = 0;
  206. if (h->temporal_id == 0 &&
  207. h->nal_unit_type != NAL_TRAIL_N &&
  208. h->nal_unit_type != NAL_TSA_N &&
  209. h->nal_unit_type != NAL_STSA_N &&
  210. h->nal_unit_type != NAL_RADL_N &&
  211. h->nal_unit_type != NAL_RASL_N &&
  212. h->nal_unit_type != NAL_RADL_R &&
  213. h->nal_unit_type != NAL_RASL_R)
  214. h->pocTid0 = h->poc;
  215. return 0; /* no need to evaluate the rest */
  216. }
  217. buf += consumed;
  218. }
  219. /* didn't find a picture! */
  220. av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
  221. return -1;
  222. }
  223. static int hevc_parse(AVCodecParserContext *s,
  224. AVCodecContext *avctx,
  225. const uint8_t **poutbuf, int *poutbuf_size,
  226. const uint8_t *buf, int buf_size)
  227. {
  228. int next;
  229. ParseContext *pc = &((HEVCParseContext *)s->priv_data)->pc;
  230. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  231. next = buf_size;
  232. } else {
  233. next = hevc_find_frame_end(s, buf, buf_size);
  234. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  235. *poutbuf = NULL;
  236. *poutbuf_size = 0;
  237. return buf_size;
  238. }
  239. }
  240. parse_nal_units(s, avctx, buf, buf_size);
  241. *poutbuf = buf;
  242. *poutbuf_size = buf_size;
  243. return next;
  244. }
  245. // Split after the parameter sets at the beginning of the stream if they exist.
  246. static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
  247. {
  248. int i;
  249. uint32_t state = -1;
  250. int has_ps = 0;
  251. for (i = 0; i < buf_size; i++) {
  252. state = (state << 8) | buf[i];
  253. if (((state >> 8) & 0xFFFFFF) == START_CODE) {
  254. int nut = (state >> 1) & 0x3F;
  255. if (nut >= NAL_VPS && nut <= NAL_PPS) {
  256. has_ps = 1;
  257. } else if (has_ps) {
  258. return i - 3;
  259. } else { // no parameter set at the beginning of the stream
  260. return 0;
  261. }
  262. }
  263. }
  264. return 0;
  265. }
  266. static int hevc_init(AVCodecParserContext *s)
  267. {
  268. HEVCContext *h = &((HEVCParseContext *)s->priv_data)->h;
  269. h->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
  270. h->skipped_bytes_pos_size = INT_MAX;
  271. return 0;
  272. }
  273. static void hevc_close(AVCodecParserContext *s)
  274. {
  275. int i;
  276. HEVCContext *h = &((HEVCParseContext *)s->priv_data)->h;
  277. ParseContext *pc = &((HEVCParseContext *)s->priv_data)->pc;
  278. av_freep(&h->skipped_bytes_pos);
  279. av_freep(&h->HEVClc);
  280. av_freep(&pc->buffer);
  281. for (i = 0; i < FF_ARRAY_ELEMS(h->vps_list); i++)
  282. av_freep(&h->vps_list[i]);
  283. for (i = 0; i < FF_ARRAY_ELEMS(h->sps_list); i++)
  284. av_buffer_unref(&h->sps_list[i]);
  285. for (i = 0; i < FF_ARRAY_ELEMS(h->pps_list); i++)
  286. av_buffer_unref(&h->pps_list[i]);
  287. for (i = 0; i < h->nals_allocated; i++)
  288. av_freep(&h->nals[i].rbsp_buffer);
  289. av_freep(&h->nals);
  290. h->nals_allocated = 0;
  291. }
  292. AVCodecParser ff_hevc_parser = {
  293. .codec_ids = { AV_CODEC_ID_HEVC },
  294. .priv_data_size = sizeof(HEVCParseContext),
  295. .parser_init = hevc_init,
  296. .parser_parse = hevc_parse,
  297. .parser_close = hevc_close,
  298. .split = hevc_split,
  299. };