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.

487 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 "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_split_packet(&ctx->pkt, buf, buf_size, avctx, 0, 0);
  75. if (ret < 0)
  76. return ret;
  77. for (i = 0; i < ctx->pkt.nb_nals; i++) {
  78. H2645NAL *nal = &ctx->pkt.nals[i];
  79. /* ignore everything except parameter sets and VCL NALUs */
  80. switch (nal->type) {
  81. case NAL_VPS: ff_hevc_decode_nal_vps(&nal->gb, avctx, &ctx->ps); break;
  82. case NAL_SPS: ff_hevc_decode_nal_sps(&nal->gb, avctx, &ctx->ps, 1); break;
  83. case NAL_PPS: ff_hevc_decode_nal_pps(&nal->gb, avctx, &ctx->ps); break;
  84. case NAL_TRAIL_R:
  85. case NAL_TRAIL_N:
  86. case NAL_TSA_N:
  87. case NAL_TSA_R:
  88. case NAL_STSA_N:
  89. case NAL_STSA_R:
  90. case NAL_BLA_W_LP:
  91. case NAL_BLA_W_RADL:
  92. case NAL_BLA_N_LP:
  93. case NAL_IDR_W_RADL:
  94. case NAL_IDR_N_LP:
  95. case NAL_CRA_NUT:
  96. case NAL_RADL_N:
  97. case NAL_RADL_R:
  98. case NAL_RASL_N:
  99. case NAL_RASL_R:
  100. if (buf == avctx->extradata) {
  101. av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit: %d\n", nal->type);
  102. return AVERROR_INVALIDDATA;
  103. }
  104. hevc_parse_slice_header(s, nal, avctx);
  105. break;
  106. }
  107. }
  108. return 0;
  109. }
  110. #endif
  111. /**
  112. * Find the end of the current frame in the bitstream.
  113. * @return the position of the first byte of the next frame, or END_NOT_FOUND
  114. */
  115. static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
  116. int buf_size)
  117. {
  118. int i;
  119. ParseContext *pc = s->priv_data;
  120. for (i = 0; i < buf_size; i++) {
  121. int nut;
  122. pc->state64 = (pc->state64 << 8) | buf[i];
  123. if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
  124. continue;
  125. nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
  126. // Beginning of access unit
  127. if ((nut >= NAL_VPS && nut <= NAL_AUD) || nut == NAL_SEI_PREFIX ||
  128. (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
  129. if (pc->frame_start_found) {
  130. pc->frame_start_found = 0;
  131. return i - 5;
  132. }
  133. } else if (nut <= NAL_RASL_R ||
  134. (nut >= NAL_BLA_W_LP && nut <= NAL_CRA_NUT)) {
  135. int first_slice_segment_in_pic_flag = buf[i] >> 7;
  136. if (first_slice_segment_in_pic_flag) {
  137. if (!pc->frame_start_found) {
  138. pc->frame_start_found = 1;
  139. } else { // First slice of next frame found
  140. pc->frame_start_found = 0;
  141. return i - 5;
  142. }
  143. }
  144. }
  145. }
  146. return END_NOT_FOUND;
  147. }
  148. #if ADVANCED_PARSER
  149. /**
  150. * Parse NAL units of found picture and decode some basic information.
  151. *
  152. * @param s parser context.
  153. * @param avctx codec context.
  154. * @param buf buffer with field/frame data.
  155. * @param buf_size size of the buffer.
  156. */
  157. static inline int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
  158. int buf_size, AVCodecContext *avctx)
  159. {
  160. HEVCParserContext *ctx = s->priv_data;
  161. HEVCContext *h = &ctx->h;
  162. GetBitContext *gb;
  163. SliceHeader *sh = &h->sh;
  164. HEVCParamSets *ps = &h->ps;
  165. H2645Packet *pkt = &ctx->pkt;
  166. const uint8_t *buf_end = buf + buf_size;
  167. int state = -1, i;
  168. H2645NAL *nal;
  169. int is_global = buf == avctx->extradata;
  170. if (!h->HEVClc)
  171. h->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
  172. if (!h->HEVClc)
  173. return AVERROR(ENOMEM);
  174. gb = &h->HEVClc->gb;
  175. /* set some sane default values */
  176. s->pict_type = AV_PICTURE_TYPE_I;
  177. s->key_frame = 0;
  178. s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
  179. h->avctx = avctx;
  180. if (!buf_size)
  181. return 0;
  182. if (pkt->nals_allocated < 1) {
  183. H2645NAL *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_h2645_extract_rbsp(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. ff_h2645_packet_uninit(&ctx->pkt);
  405. av_freep(&ctx->pc.buffer);
  406. }
  407. AVCodecParser ff_hevc_parser = {
  408. .codec_ids = { AV_CODEC_ID_HEVC },
  409. .priv_data_size = sizeof(HEVCParserContext),
  410. .parser_parse = hevc_parse,
  411. .parser_close = hevc_parser_close,
  412. .split = hevc_split,
  413. };