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.

498 lines
16KB

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