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.

493 lines
15KB

  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 "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. #define ADVANCED_PARSER CONFIG_HEVC_DECODER
  29. typedef struct HEVCParserContext {
  30. ParseContext pc;
  31. HEVCPacket pkt;
  32. HEVCParamSets ps;
  33. int parsed_extradata;
  34. #if ADVANCED_PARSER
  35. HEVCContext h;
  36. #endif
  37. } HEVCParserContext;
  38. #if !ADVANCED_PARSER
  39. static int hevc_parse_slice_header(AVCodecParserContext *s, HEVCNAL *nal,
  40. AVCodecContext *avctx)
  41. {
  42. HEVCParserContext *ctx = s->priv_data;
  43. GetBitContext *gb = &nal->gb;
  44. HEVCPPS *pps;
  45. HEVCSPS *sps;
  46. unsigned int pps_id;
  47. get_bits1(gb); // first slice in pic
  48. if (IS_IRAP_NAL(nal))
  49. get_bits1(gb); // no output of prior pics
  50. pps_id = get_ue_golomb_long(gb);
  51. if (pps_id >= MAX_PPS_COUNT || !ctx->ps.pps_list[pps_id]) {
  52. av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
  53. return AVERROR_INVALIDDATA;
  54. }
  55. pps = (HEVCPPS*)ctx->ps.pps_list[pps_id]->data;
  56. sps = (HEVCSPS*)ctx->ps.sps_list[pps->sps_id]->data;
  57. /* export the stream parameters */
  58. s->coded_width = sps->width;
  59. s->coded_height = sps->height;
  60. s->width = sps->output_width;
  61. s->height = sps->output_height;
  62. s->format = sps->pix_fmt;
  63. avctx->profile = sps->ptl.general_ptl.profile_idc;
  64. avctx->level = sps->ptl.general_ptl.level_idc;
  65. /* ignore the rest for now*/
  66. return 0;
  67. }
  68. static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
  69. int buf_size, AVCodecContext *avctx)
  70. {
  71. HEVCParserContext *ctx = s->priv_data;
  72. int ret, i;
  73. ret = ff_hevc_split_packet(NULL, &ctx->pkt, buf, buf_size, avctx, 0, 0);
  74. if (ret < 0)
  75. return ret;
  76. for (i = 0; i < ctx->pkt.nb_nals; i++) {
  77. HEVCNAL *nal = &ctx->pkt.nals[i];
  78. /* ignore everything except parameter sets and VCL NALUs */
  79. switch (nal->type) {
  80. case NAL_VPS: ff_hevc_decode_nal_vps(&nal->gb, avctx, &ctx->ps); break;
  81. case NAL_SPS: ff_hevc_decode_nal_sps(&nal->gb, avctx, &ctx->ps, 1); break;
  82. case NAL_PPS: ff_hevc_decode_nal_pps(&nal->gb, avctx, &ctx->ps); break;
  83. case NAL_TRAIL_R:
  84. case NAL_TRAIL_N:
  85. case NAL_TSA_N:
  86. case NAL_TSA_R:
  87. case NAL_STSA_N:
  88. case NAL_STSA_R:
  89. case NAL_BLA_W_LP:
  90. case NAL_BLA_W_RADL:
  91. case NAL_BLA_N_LP:
  92. case NAL_IDR_W_RADL:
  93. case NAL_IDR_N_LP:
  94. case NAL_CRA_NUT:
  95. case NAL_RADL_N:
  96. case NAL_RADL_R:
  97. case NAL_RASL_N:
  98. case NAL_RASL_R:
  99. if (buf == avctx->extradata) {
  100. av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit: %d\n", nal->type);
  101. return AVERROR_INVALIDDATA;
  102. }
  103. hevc_parse_slice_header(s, nal, avctx);
  104. break;
  105. }
  106. }
  107. return 0;
  108. }
  109. #endif
  110. /**
  111. * Find the end of the current frame in the bitstream.
  112. * @return the position of the first byte of the next frame, or END_NOT_FOUND
  113. */
  114. static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
  115. int buf_size)
  116. {
  117. int i;
  118. ParseContext *pc = s->priv_data;
  119. for (i = 0; i < buf_size; i++) {
  120. int nut;
  121. pc->state64 = (pc->state64 << 8) | buf[i];
  122. if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
  123. continue;
  124. nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
  125. // Beginning of access unit
  126. if ((nut >= NAL_VPS && nut <= NAL_AUD) || nut == NAL_SEI_PREFIX ||
  127. (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
  128. if (pc->frame_start_found) {
  129. pc->frame_start_found = 0;
  130. return i - 5;
  131. }
  132. } else if (nut <= NAL_RASL_R ||
  133. (nut >= NAL_BLA_W_LP && nut <= NAL_CRA_NUT)) {
  134. int first_slice_segment_in_pic_flag = buf[i] >> 7;
  135. if (first_slice_segment_in_pic_flag) {
  136. if (!pc->frame_start_found) {
  137. pc->frame_start_found = 1;
  138. } else { // First slice of next frame found
  139. pc->frame_start_found = 0;
  140. return i - 5;
  141. }
  142. }
  143. }
  144. }
  145. return END_NOT_FOUND;
  146. }
  147. #if ADVANCED_PARSER
  148. /**
  149. * Parse NAL units of found picture and decode some basic information.
  150. *
  151. * @param s parser context.
  152. * @param avctx codec context.
  153. * @param buf buffer with field/frame data.
  154. * @param buf_size size of the buffer.
  155. */
  156. static inline int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
  157. int buf_size, AVCodecContext *avctx)
  158. {
  159. HEVCParserContext *ctx = s->priv_data;
  160. HEVCContext *h = &ctx->h;
  161. GetBitContext *gb;
  162. SliceHeader *sh = &h->sh;
  163. HEVCParamSets *ps = &h->ps;
  164. HEVCPacket *pkt = &ctx->pkt;
  165. const uint8_t *buf_end = buf + buf_size;
  166. int state = -1, i;
  167. HEVCNAL *nal;
  168. int is_global = buf == avctx->extradata;
  169. if (!h->HEVClc)
  170. h->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
  171. if (!h->HEVClc)
  172. return AVERROR(ENOMEM);
  173. gb = &h->HEVClc->gb;
  174. /* set some sane default values */
  175. s->pict_type = AV_PICTURE_TYPE_I;
  176. s->key_frame = 0;
  177. s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
  178. h->avctx = avctx;
  179. ff_hevc_reset_sei(h);
  180. if (!buf_size)
  181. return 0;
  182. if (pkt->nals_allocated < 1) {
  183. HEVCNAL *tmp = av_realloc_array(pkt->nals, 1, sizeof(*tmp));
  184. if (!tmp)
  185. return AVERROR(ENOMEM);
  186. pkt->nals = tmp;
  187. memset(pkt->nals, 0, sizeof(*tmp));
  188. pkt->nals_allocated = 1;
  189. }
  190. nal = &pkt->nals[0];
  191. for (;;) {
  192. int src_length, consumed;
  193. int ret;
  194. buf = avpriv_find_start_code(buf, buf_end, &state);
  195. if (--buf + 2 >= buf_end)
  196. break;
  197. src_length = buf_end - buf;
  198. h->nal_unit_type = (*buf >> 1) & 0x3f;
  199. h->temporal_id = (*(buf + 1) & 0x07) - 1;
  200. if (h->nal_unit_type <= NAL_CRA_NUT) {
  201. // Do not walk the whole buffer just to decode slice segment header
  202. if (src_length > 20)
  203. src_length = 20;
  204. }
  205. consumed = ff_hevc_extract_rbsp(NULL, buf, src_length, nal);
  206. if (consumed < 0)
  207. return consumed;
  208. ret = init_get_bits8(gb, nal->data + 2, nal->size);
  209. if (ret < 0)
  210. return ret;
  211. switch (h->nal_unit_type) {
  212. case NAL_VPS:
  213. ff_hevc_decode_nal_vps(gb, avctx, ps);
  214. break;
  215. case NAL_SPS:
  216. ff_hevc_decode_nal_sps(gb, avctx, ps, 1);
  217. break;
  218. case NAL_PPS:
  219. ff_hevc_decode_nal_pps(gb, avctx, ps);
  220. break;
  221. case NAL_SEI_PREFIX:
  222. case NAL_SEI_SUFFIX:
  223. ff_hevc_decode_nal_sei(h);
  224. break;
  225. case NAL_TRAIL_N:
  226. case NAL_TRAIL_R:
  227. case NAL_TSA_N:
  228. case NAL_TSA_R:
  229. case NAL_STSA_N:
  230. case NAL_STSA_R:
  231. case NAL_RADL_N:
  232. case NAL_RADL_R:
  233. case NAL_RASL_N:
  234. case NAL_RASL_R:
  235. case NAL_BLA_W_LP:
  236. case NAL_BLA_W_RADL:
  237. case NAL_BLA_N_LP:
  238. case NAL_IDR_W_RADL:
  239. case NAL_IDR_N_LP:
  240. case NAL_CRA_NUT:
  241. if (is_global) {
  242. av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit: %d\n", h->nal_unit_type);
  243. return AVERROR_INVALIDDATA;
  244. }
  245. sh->first_slice_in_pic_flag = get_bits1(gb);
  246. s->picture_structure = h->picture_struct;
  247. s->field_order = h->picture_struct;
  248. if (IS_IRAP(h)) {
  249. s->key_frame = 1;
  250. sh->no_output_of_prior_pics_flag = get_bits1(gb);
  251. }
  252. sh->pps_id = get_ue_golomb(gb);
  253. if (sh->pps_id >= MAX_PPS_COUNT || !ps->pps_list[sh->pps_id]) {
  254. av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
  255. return AVERROR_INVALIDDATA;
  256. }
  257. ps->pps = (HEVCPPS*)ps->pps_list[sh->pps_id]->data;
  258. if (ps->pps->sps_id >= MAX_SPS_COUNT || !ps->sps_list[ps->pps->sps_id]) {
  259. av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", ps->pps->sps_id);
  260. return AVERROR_INVALIDDATA;
  261. }
  262. if (ps->sps != (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data) {
  263. ps->sps = (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data;
  264. ps->vps = (HEVCVPS*)ps->vps_list[ps->sps->vps_id]->data;
  265. }
  266. if (!sh->first_slice_in_pic_flag) {
  267. int slice_address_length;
  268. if (ps->pps->dependent_slice_segments_enabled_flag)
  269. sh->dependent_slice_segment_flag = get_bits1(gb);
  270. else
  271. sh->dependent_slice_segment_flag = 0;
  272. slice_address_length = av_ceil_log2_c(ps->sps->ctb_width *
  273. ps->sps->ctb_height);
  274. sh->slice_segment_addr = get_bitsz(gb, slice_address_length);
  275. if (sh->slice_segment_addr >= ps->sps->ctb_width * ps->sps->ctb_height) {
  276. av_log(avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
  277. sh->slice_segment_addr);
  278. return AVERROR_INVALIDDATA;
  279. }
  280. } else
  281. sh->dependent_slice_segment_flag = 0;
  282. if (sh->dependent_slice_segment_flag)
  283. break;
  284. for (i = 0; i < ps->pps->num_extra_slice_header_bits; i++)
  285. skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
  286. sh->slice_type = get_ue_golomb(gb);
  287. if (!(sh->slice_type == I_SLICE || sh->slice_type == P_SLICE ||
  288. sh->slice_type == B_SLICE)) {
  289. av_log(avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
  290. sh->slice_type);
  291. return AVERROR_INVALIDDATA;
  292. }
  293. s->pict_type = sh->slice_type == B_SLICE ? AV_PICTURE_TYPE_B :
  294. sh->slice_type == P_SLICE ? AV_PICTURE_TYPE_P :
  295. AV_PICTURE_TYPE_I;
  296. if (ps->pps->output_flag_present_flag)
  297. sh->pic_output_flag = get_bits1(gb);
  298. if (ps->sps->separate_colour_plane_flag)
  299. sh->colour_plane_id = get_bits(gb, 2);
  300. if (!IS_IDR(h)) {
  301. sh->pic_order_cnt_lsb = get_bits(gb, ps->sps->log2_max_poc_lsb);
  302. s->output_picture_number = h->poc = ff_hevc_compute_poc(h, sh->pic_order_cnt_lsb);
  303. } else
  304. s->output_picture_number = h->poc = 0;
  305. if (h->temporal_id == 0 &&
  306. h->nal_unit_type != NAL_TRAIL_N &&
  307. h->nal_unit_type != NAL_TSA_N &&
  308. h->nal_unit_type != NAL_STSA_N &&
  309. h->nal_unit_type != NAL_RADL_N &&
  310. h->nal_unit_type != NAL_RASL_N &&
  311. h->nal_unit_type != NAL_RADL_R &&
  312. h->nal_unit_type != NAL_RASL_R)
  313. h->pocTid0 = h->poc;
  314. return 0; /* no need to evaluate the rest */
  315. }
  316. buf += consumed;
  317. }
  318. /* didn't find a picture! */
  319. if (!is_global)
  320. av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
  321. return -1;
  322. }
  323. #endif
  324. static int hevc_parse(AVCodecParserContext *s,
  325. AVCodecContext *avctx,
  326. const uint8_t **poutbuf, int *poutbuf_size,
  327. const uint8_t *buf, int buf_size)
  328. {
  329. int next;
  330. HEVCParserContext *ctx = s->priv_data;
  331. ParseContext *pc = &ctx->pc;
  332. if (avctx->extradata && !ctx->parsed_extradata) {
  333. parse_nal_units(s, avctx->extradata, avctx->extradata_size, avctx);
  334. ctx->parsed_extradata = 1;
  335. }
  336. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  337. next = buf_size;
  338. } else {
  339. next = hevc_find_frame_end(s, buf, buf_size);
  340. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  341. *poutbuf = NULL;
  342. *poutbuf_size = 0;
  343. return buf_size;
  344. }
  345. }
  346. parse_nal_units(s, buf, buf_size, avctx);
  347. *poutbuf = buf;
  348. *poutbuf_size = buf_size;
  349. return next;
  350. }
  351. // Split after the parameter sets at the beginning of the stream if they exist.
  352. static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
  353. {
  354. const uint8_t *ptr = buf, *end = buf + buf_size;
  355. uint32_t state = -1;
  356. int has_vps = 0;
  357. int has_sps = 0;
  358. int has_pps = 0;
  359. int nut;
  360. while (ptr < end) {
  361. ptr = avpriv_find_start_code(ptr, end, &state);
  362. if ((state >> 8) != START_CODE)
  363. break;
  364. nut = (state >> 1) & 0x3F;
  365. if (nut == NAL_VPS)
  366. has_vps = 1;
  367. else if (nut == NAL_SPS)
  368. has_sps = 1;
  369. else if (nut == NAL_PPS)
  370. has_pps = 1;
  371. else if ((nut != NAL_SEI_PREFIX || has_pps) &&
  372. nut != NAL_AUD) {
  373. if (has_vps && has_sps) {
  374. while (ptr - 4 > buf && ptr[-5] == 0)
  375. ptr--;
  376. return ptr - 4 - buf;
  377. }
  378. }
  379. }
  380. return 0;
  381. }
  382. static void hevc_parser_close(AVCodecParserContext *s)
  383. {
  384. HEVCParserContext *ctx = s->priv_data;
  385. int i;
  386. #if ADVANCED_PARSER
  387. HEVCContext *h = &ctx->h;
  388. for (i = 0; i < FF_ARRAY_ELEMS(h->ps.vps_list); i++)
  389. av_buffer_unref(&h->ps.vps_list[i]);
  390. for (i = 0; i < FF_ARRAY_ELEMS(h->ps.sps_list); i++)
  391. av_buffer_unref(&h->ps.sps_list[i]);
  392. for (i = 0; i < FF_ARRAY_ELEMS(h->ps.pps_list); i++)
  393. av_buffer_unref(&h->ps.pps_list[i]);
  394. h->ps.sps = NULL;
  395. av_freep(&h->HEVClc);
  396. #endif
  397. for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.vps_list); i++)
  398. av_buffer_unref(&ctx->ps.vps_list[i]);
  399. for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.sps_list); i++)
  400. av_buffer_unref(&ctx->ps.sps_list[i]);
  401. for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.pps_list); i++)
  402. av_buffer_unref(&ctx->ps.pps_list[i]);
  403. ctx->ps.sps = NULL;
  404. for (i = 0; i < ctx->pkt.nals_allocated; i++) {
  405. av_freep(&ctx->pkt.nals[i].rbsp_buffer);
  406. av_freep(&ctx->pkt.nals[i].skipped_bytes_pos);
  407. }
  408. av_freep(&ctx->pkt.nals);
  409. ctx->pkt.nals_allocated = 0;
  410. av_freep(&ctx->pc.buffer);
  411. }
  412. AVCodecParser ff_hevc_parser = {
  413. .codec_ids = { AV_CODEC_ID_HEVC },
  414. .priv_data_size = sizeof(HEVCParserContext),
  415. .parser_parse = hevc_parse,
  416. .parser_close = hevc_parser_close,
  417. .split = hevc_split,
  418. };