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.

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