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.

491 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. if (!buf_size)
  180. return 0;
  181. if (pkt->nals_allocated < 1) {
  182. HEVCNAL *tmp = av_realloc_array(pkt->nals, 1, sizeof(*tmp));
  183. if (!tmp)
  184. return AVERROR(ENOMEM);
  185. pkt->nals = tmp;
  186. memset(pkt->nals, 0, sizeof(*tmp));
  187. pkt->nals_allocated = 1;
  188. }
  189. nal = &pkt->nals[0];
  190. for (;;) {
  191. int src_length, consumed;
  192. int ret;
  193. buf = avpriv_find_start_code(buf, buf_end, &state);
  194. if (--buf + 2 >= buf_end)
  195. break;
  196. src_length = buf_end - buf;
  197. h->nal_unit_type = (*buf >> 1) & 0x3f;
  198. h->temporal_id = (*(buf + 1) & 0x07) - 1;
  199. if (h->nal_unit_type <= NAL_CRA_NUT) {
  200. // Do not walk the whole buffer just to decode slice segment header
  201. if (src_length > 20)
  202. src_length = 20;
  203. }
  204. consumed = ff_hevc_extract_rbsp(buf, src_length, nal);
  205. if (consumed < 0)
  206. return consumed;
  207. ret = init_get_bits8(gb, nal->data + 2, nal->size);
  208. if (ret < 0)
  209. return ret;
  210. switch (h->nal_unit_type) {
  211. case NAL_VPS:
  212. ff_hevc_decode_nal_vps(gb, avctx, ps);
  213. break;
  214. case NAL_SPS:
  215. ff_hevc_decode_nal_sps(gb, avctx, ps, 1);
  216. break;
  217. case NAL_PPS:
  218. ff_hevc_decode_nal_pps(gb, avctx, ps);
  219. break;
  220. case NAL_SEI_PREFIX:
  221. case NAL_SEI_SUFFIX:
  222. ff_hevc_decode_nal_sei(h);
  223. break;
  224. case NAL_TRAIL_N:
  225. case NAL_TRAIL_R:
  226. case NAL_TSA_N:
  227. case NAL_TSA_R:
  228. case NAL_STSA_N:
  229. case NAL_STSA_R:
  230. case NAL_RADL_N:
  231. case NAL_RADL_R:
  232. case NAL_RASL_N:
  233. case NAL_RASL_R:
  234. case NAL_BLA_W_LP:
  235. case NAL_BLA_W_RADL:
  236. case NAL_BLA_N_LP:
  237. case NAL_IDR_W_RADL:
  238. case NAL_IDR_N_LP:
  239. case NAL_CRA_NUT:
  240. if (is_global) {
  241. av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit: %d\n", h->nal_unit_type);
  242. return AVERROR_INVALIDDATA;
  243. }
  244. sh->first_slice_in_pic_flag = get_bits1(gb);
  245. s->picture_structure = h->picture_struct;
  246. s->field_order = h->picture_struct;
  247. if (IS_IRAP(h)) {
  248. s->key_frame = 1;
  249. sh->no_output_of_prior_pics_flag = get_bits1(gb);
  250. }
  251. sh->pps_id = get_ue_golomb(gb);
  252. if (sh->pps_id >= MAX_PPS_COUNT || !ps->pps_list[sh->pps_id]) {
  253. av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
  254. return AVERROR_INVALIDDATA;
  255. }
  256. ps->pps = (HEVCPPS*)ps->pps_list[sh->pps_id]->data;
  257. if (ps->pps->sps_id >= MAX_SPS_COUNT || !ps->sps_list[ps->pps->sps_id]) {
  258. av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", ps->pps->sps_id);
  259. return AVERROR_INVALIDDATA;
  260. }
  261. if (ps->sps != (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data) {
  262. ps->sps = (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data;
  263. ps->vps = (HEVCVPS*)ps->vps_list[ps->sps->vps_id]->data;
  264. }
  265. if (!sh->first_slice_in_pic_flag) {
  266. int slice_address_length;
  267. if (ps->pps->dependent_slice_segments_enabled_flag)
  268. sh->dependent_slice_segment_flag = get_bits1(gb);
  269. else
  270. sh->dependent_slice_segment_flag = 0;
  271. slice_address_length = av_ceil_log2_c(ps->sps->ctb_width *
  272. ps->sps->ctb_height);
  273. sh->slice_segment_addr = get_bitsz(gb, slice_address_length);
  274. if (sh->slice_segment_addr >= ps->sps->ctb_width * ps->sps->ctb_height) {
  275. av_log(avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
  276. sh->slice_segment_addr);
  277. return AVERROR_INVALIDDATA;
  278. }
  279. } else
  280. sh->dependent_slice_segment_flag = 0;
  281. if (sh->dependent_slice_segment_flag)
  282. break;
  283. for (i = 0; i < ps->pps->num_extra_slice_header_bits; i++)
  284. skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
  285. sh->slice_type = get_ue_golomb(gb);
  286. if (!(sh->slice_type == I_SLICE || sh->slice_type == P_SLICE ||
  287. sh->slice_type == B_SLICE)) {
  288. av_log(avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
  289. sh->slice_type);
  290. return AVERROR_INVALIDDATA;
  291. }
  292. s->pict_type = sh->slice_type == B_SLICE ? AV_PICTURE_TYPE_B :
  293. sh->slice_type == P_SLICE ? AV_PICTURE_TYPE_P :
  294. AV_PICTURE_TYPE_I;
  295. if (ps->pps->output_flag_present_flag)
  296. sh->pic_output_flag = get_bits1(gb);
  297. if (ps->sps->separate_colour_plane_flag)
  298. sh->colour_plane_id = get_bits(gb, 2);
  299. if (!IS_IDR(h)) {
  300. sh->pic_order_cnt_lsb = get_bits(gb, ps->sps->log2_max_poc_lsb);
  301. s->output_picture_number = h->poc = ff_hevc_compute_poc(h, sh->pic_order_cnt_lsb);
  302. } else
  303. s->output_picture_number = h->poc = 0;
  304. if (h->temporal_id == 0 &&
  305. h->nal_unit_type != NAL_TRAIL_N &&
  306. h->nal_unit_type != NAL_TSA_N &&
  307. h->nal_unit_type != NAL_STSA_N &&
  308. h->nal_unit_type != NAL_RADL_N &&
  309. h->nal_unit_type != NAL_RASL_N &&
  310. h->nal_unit_type != NAL_RADL_R &&
  311. h->nal_unit_type != NAL_RASL_R)
  312. h->pocTid0 = h->poc;
  313. return 0; /* no need to evaluate the rest */
  314. }
  315. buf += consumed;
  316. }
  317. /* didn't find a picture! */
  318. if (!is_global)
  319. av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
  320. return -1;
  321. }
  322. #endif
  323. static int hevc_parse(AVCodecParserContext *s,
  324. AVCodecContext *avctx,
  325. const uint8_t **poutbuf, int *poutbuf_size,
  326. const uint8_t *buf, int buf_size)
  327. {
  328. int next;
  329. HEVCParserContext *ctx = s->priv_data;
  330. ParseContext *pc = &ctx->pc;
  331. if (avctx->extradata && !ctx->parsed_extradata) {
  332. parse_nal_units(s, avctx->extradata, avctx->extradata_size, avctx);
  333. ctx->parsed_extradata = 1;
  334. }
  335. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  336. next = buf_size;
  337. } else {
  338. next = hevc_find_frame_end(s, buf, buf_size);
  339. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  340. *poutbuf = NULL;
  341. *poutbuf_size = 0;
  342. return buf_size;
  343. }
  344. }
  345. parse_nal_units(s, buf, buf_size, avctx);
  346. *poutbuf = buf;
  347. *poutbuf_size = buf_size;
  348. return next;
  349. }
  350. // Split after the parameter sets at the beginning of the stream if they exist.
  351. static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
  352. {
  353. const uint8_t *ptr = buf, *end = buf + buf_size;
  354. uint32_t state = -1;
  355. int has_vps = 0;
  356. int has_sps = 0;
  357. int has_pps = 0;
  358. int nut;
  359. while (ptr < end) {
  360. ptr = avpriv_find_start_code(ptr, end, &state);
  361. if ((state >> 8) != START_CODE)
  362. break;
  363. nut = (state >> 1) & 0x3F;
  364. if (nut == NAL_VPS)
  365. has_vps = 1;
  366. else if (nut == NAL_SPS)
  367. has_sps = 1;
  368. else if (nut == NAL_PPS)
  369. has_pps = 1;
  370. else if ((nut != NAL_SEI_PREFIX || has_pps) &&
  371. nut != NAL_AUD) {
  372. if (has_vps && has_sps) {
  373. while (ptr - 4 > buf && ptr[-5] == 0)
  374. ptr--;
  375. return ptr - 4 - buf;
  376. }
  377. }
  378. }
  379. return 0;
  380. }
  381. static void hevc_parser_close(AVCodecParserContext *s)
  382. {
  383. HEVCParserContext *ctx = s->priv_data;
  384. int i;
  385. #if ADVANCED_PARSER
  386. HEVCContext *h = &ctx->h;
  387. for (i = 0; i < FF_ARRAY_ELEMS(h->ps.vps_list); i++)
  388. av_buffer_unref(&h->ps.vps_list[i]);
  389. for (i = 0; i < FF_ARRAY_ELEMS(h->ps.sps_list); i++)
  390. av_buffer_unref(&h->ps.sps_list[i]);
  391. for (i = 0; i < FF_ARRAY_ELEMS(h->ps.pps_list); i++)
  392. av_buffer_unref(&h->ps.pps_list[i]);
  393. h->ps.sps = NULL;
  394. av_freep(&h->HEVClc);
  395. #endif
  396. for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.vps_list); i++)
  397. av_buffer_unref(&ctx->ps.vps_list[i]);
  398. for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.sps_list); i++)
  399. av_buffer_unref(&ctx->ps.sps_list[i]);
  400. for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.pps_list); i++)
  401. av_buffer_unref(&ctx->ps.pps_list[i]);
  402. ctx->ps.sps = NULL;
  403. for (i = 0; i < ctx->pkt.nals_allocated; i++) {
  404. av_freep(&ctx->pkt.nals[i].rbsp_buffer);
  405. av_freep(&ctx->pkt.nals[i].skipped_bytes_pos);
  406. }
  407. av_freep(&ctx->pkt.nals);
  408. ctx->pkt.nals_allocated = 0;
  409. av_freep(&ctx->pc.buffer);
  410. }
  411. AVCodecParser ff_hevc_parser = {
  412. .codec_ids = { AV_CODEC_ID_HEVC },
  413. .priv_data_size = sizeof(HEVCParserContext),
  414. .parser_parse = hevc_parse,
  415. .parser_close = hevc_parser_close,
  416. .split = hevc_split,
  417. };