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.

429 lines
14KB

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