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.

483 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 "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. int is_global = buf == avctx->extradata;
  169. int i, ret;
  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. ff_hevc_reset_sei(sei);
  181. ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx, 0, 0,
  182. AV_CODEC_ID_HEVC, 1);
  183. if (ret < 0)
  184. return ret;
  185. for (i = 0; i < ctx->pkt.nb_nals; i++) {
  186. H2645NAL *nal = &ctx->pkt.nals[i];
  187. int num = 0, den = 0;
  188. h->nal_unit_type = nal->type;
  189. h->temporal_id = nal->temporal_id;
  190. *gb = nal->gb;
  191. switch (h->nal_unit_type) {
  192. case HEVC_NAL_VPS:
  193. ff_hevc_decode_nal_vps(gb, avctx, ps);
  194. break;
  195. case HEVC_NAL_SPS:
  196. ff_hevc_decode_nal_sps(gb, avctx, ps, 1);
  197. break;
  198. case HEVC_NAL_PPS:
  199. ff_hevc_decode_nal_pps(gb, avctx, ps);
  200. break;
  201. case HEVC_NAL_SEI_PREFIX:
  202. case HEVC_NAL_SEI_SUFFIX:
  203. ff_hevc_decode_nal_sei(gb, avctx, sei, ps, h->nal_unit_type);
  204. break;
  205. case HEVC_NAL_TRAIL_N:
  206. case HEVC_NAL_TRAIL_R:
  207. case HEVC_NAL_TSA_N:
  208. case HEVC_NAL_TSA_R:
  209. case HEVC_NAL_STSA_N:
  210. case HEVC_NAL_STSA_R:
  211. case HEVC_NAL_RADL_N:
  212. case HEVC_NAL_RADL_R:
  213. case HEVC_NAL_RASL_N:
  214. case HEVC_NAL_RASL_R:
  215. case HEVC_NAL_BLA_W_LP:
  216. case HEVC_NAL_BLA_W_RADL:
  217. case HEVC_NAL_BLA_N_LP:
  218. case HEVC_NAL_IDR_W_RADL:
  219. case HEVC_NAL_IDR_N_LP:
  220. case HEVC_NAL_CRA_NUT:
  221. if (is_global) {
  222. av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit: %d\n", h->nal_unit_type);
  223. return AVERROR_INVALIDDATA;
  224. }
  225. sh->first_slice_in_pic_flag = get_bits1(gb);
  226. s->picture_structure = h->sei.picture_timing.picture_struct;
  227. s->field_order = h->sei.picture_timing.picture_struct;
  228. if (IS_IRAP(h)) {
  229. s->key_frame = 1;
  230. sh->no_output_of_prior_pics_flag = get_bits1(gb);
  231. }
  232. sh->pps_id = get_ue_golomb(gb);
  233. if (sh->pps_id >= HEVC_MAX_PPS_COUNT || !ps->pps_list[sh->pps_id]) {
  234. av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
  235. return AVERROR_INVALIDDATA;
  236. }
  237. ps->pps = (HEVCPPS*)ps->pps_list[sh->pps_id]->data;
  238. if (ps->pps->sps_id >= HEVC_MAX_SPS_COUNT || !ps->sps_list[ps->pps->sps_id]) {
  239. av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", ps->pps->sps_id);
  240. return AVERROR_INVALIDDATA;
  241. }
  242. if (ps->sps != (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data) {
  243. ps->sps = (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data;
  244. ps->vps = (HEVCVPS*)ps->vps_list[ps->sps->vps_id]->data;
  245. }
  246. s->coded_width = ps->sps->width;
  247. s->coded_height = ps->sps->height;
  248. s->width = ps->sps->output_width;
  249. s->height = ps->sps->output_height;
  250. s->format = ps->sps->pix_fmt;
  251. avctx->profile = ps->sps->ptl.general_ptl.profile_idc;
  252. avctx->level = ps->sps->ptl.general_ptl.level_idc;
  253. if (ps->vps->vps_timing_info_present_flag) {
  254. num = ps->vps->vps_num_units_in_tick;
  255. den = ps->vps->vps_time_scale;
  256. } else if (ps->sps->vui.vui_timing_info_present_flag) {
  257. num = ps->sps->vui.vui_num_units_in_tick;
  258. den = ps->sps->vui.vui_time_scale;
  259. }
  260. if (num != 0 && den != 0)
  261. av_reduce(&avctx->framerate.den, &avctx->framerate.num,
  262. num, den, 1 << 30);
  263. if (!sh->first_slice_in_pic_flag) {
  264. int slice_address_length;
  265. if (ps->pps->dependent_slice_segments_enabled_flag)
  266. sh->dependent_slice_segment_flag = get_bits1(gb);
  267. else
  268. sh->dependent_slice_segment_flag = 0;
  269. slice_address_length = av_ceil_log2_c(ps->sps->ctb_width *
  270. ps->sps->ctb_height);
  271. sh->slice_segment_addr = get_bitsz(gb, slice_address_length);
  272. if (sh->slice_segment_addr >= ps->sps->ctb_width * ps->sps->ctb_height) {
  273. av_log(avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
  274. sh->slice_segment_addr);
  275. return AVERROR_INVALIDDATA;
  276. }
  277. } else
  278. sh->dependent_slice_segment_flag = 0;
  279. if (sh->dependent_slice_segment_flag)
  280. break;
  281. for (i = 0; i < ps->pps->num_extra_slice_header_bits; i++)
  282. skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
  283. sh->slice_type = get_ue_golomb(gb);
  284. if (!(sh->slice_type == HEVC_SLICE_I || sh->slice_type == HEVC_SLICE_P ||
  285. sh->slice_type == HEVC_SLICE_B)) {
  286. av_log(avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
  287. sh->slice_type);
  288. return AVERROR_INVALIDDATA;
  289. }
  290. s->pict_type = sh->slice_type == HEVC_SLICE_B ? AV_PICTURE_TYPE_B :
  291. sh->slice_type == HEVC_SLICE_P ? AV_PICTURE_TYPE_P :
  292. AV_PICTURE_TYPE_I;
  293. if (ps->pps->output_flag_present_flag)
  294. sh->pic_output_flag = get_bits1(gb);
  295. if (ps->sps->separate_colour_plane_flag)
  296. sh->colour_plane_id = get_bits(gb, 2);
  297. if (!IS_IDR(h)) {
  298. sh->pic_order_cnt_lsb = get_bits(gb, ps->sps->log2_max_poc_lsb);
  299. s->output_picture_number = h->poc = ff_hevc_compute_poc(h->ps.sps, h->pocTid0, sh->pic_order_cnt_lsb, h->nal_unit_type);
  300. } else
  301. s->output_picture_number = h->poc = 0;
  302. if (h->temporal_id == 0 &&
  303. h->nal_unit_type != HEVC_NAL_TRAIL_N &&
  304. h->nal_unit_type != HEVC_NAL_TSA_N &&
  305. h->nal_unit_type != HEVC_NAL_STSA_N &&
  306. h->nal_unit_type != HEVC_NAL_RADL_N &&
  307. h->nal_unit_type != HEVC_NAL_RASL_N &&
  308. h->nal_unit_type != HEVC_NAL_RADL_R &&
  309. h->nal_unit_type != HEVC_NAL_RASL_R)
  310. h->pocTid0 = h->poc;
  311. return 0; /* no need to evaluate the rest */
  312. }
  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_vps = 0;
  353. int has_sps = 0;
  354. int has_pps = 0;
  355. int nut;
  356. while (ptr < end) {
  357. ptr = avpriv_find_start_code(ptr, end, &state);
  358. if ((state >> 8) != START_CODE)
  359. break;
  360. nut = (state >> 1) & 0x3F;
  361. if (nut == HEVC_NAL_VPS)
  362. has_vps = 1;
  363. else if (nut == HEVC_NAL_SPS)
  364. has_sps = 1;
  365. else if (nut == HEVC_NAL_PPS)
  366. has_pps = 1;
  367. else if ((nut != HEVC_NAL_SEI_PREFIX || has_pps) &&
  368. nut != HEVC_NAL_AUD) {
  369. if (has_vps && has_sps) {
  370. while (ptr - 4 > buf && ptr[-5] == 0)
  371. ptr--;
  372. return ptr - 4 - buf;
  373. }
  374. }
  375. }
  376. return 0;
  377. }
  378. static void hevc_parser_close(AVCodecParserContext *s)
  379. {
  380. HEVCParserContext *ctx = s->priv_data;
  381. int i;
  382. #if ADVANCED_PARSER
  383. HEVCContext *h = &ctx->h;
  384. for (i = 0; i < FF_ARRAY_ELEMS(h->ps.vps_list); i++)
  385. av_buffer_unref(&h->ps.vps_list[i]);
  386. for (i = 0; i < FF_ARRAY_ELEMS(h->ps.sps_list); i++)
  387. av_buffer_unref(&h->ps.sps_list[i]);
  388. for (i = 0; i < FF_ARRAY_ELEMS(h->ps.pps_list); i++)
  389. av_buffer_unref(&h->ps.pps_list[i]);
  390. h->ps.sps = NULL;
  391. av_freep(&h->HEVClc);
  392. #endif
  393. for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.vps_list); i++)
  394. av_buffer_unref(&ctx->ps.vps_list[i]);
  395. for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.sps_list); i++)
  396. av_buffer_unref(&ctx->ps.sps_list[i]);
  397. for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.pps_list); i++)
  398. av_buffer_unref(&ctx->ps.pps_list[i]);
  399. ctx->ps.sps = NULL;
  400. ff_h2645_packet_uninit(&ctx->pkt);
  401. av_freep(&ctx->pc.buffer);
  402. }
  403. AVCodecParser ff_hevc_parser = {
  404. .codec_ids = { AV_CODEC_ID_HEVC },
  405. .priv_data_size = sizeof(HEVCParserContext),
  406. .parser_parse = hevc_parse,
  407. .parser_close = hevc_parser_close,
  408. .split = hevc_split,
  409. };