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.

513 lines
17KB

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