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.

384 lines
12KB

  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 "hevc_ps.h"
  26. #include "hevc_sei.h"
  27. #include "h2645_parse.h"
  28. #include "internal.h"
  29. #include "parser.h"
  30. #define START_CODE 0x000001 ///< start_code_prefix_one_3bytes
  31. #define IS_IRAP_NAL(nal) (nal->type >= 16 && nal->type <= 23)
  32. #define IS_IDR_NAL(nal) (nal->type == HEVC_NAL_IDR_W_RADL || nal->type == HEVC_NAL_IDR_N_LP)
  33. typedef struct HEVCParserContext {
  34. ParseContext pc;
  35. H2645Packet pkt;
  36. HEVCParamSets ps;
  37. HEVCSEIContext sei;
  38. SliceHeader sh;
  39. int parsed_extradata;
  40. int poc;
  41. int pocTid0;
  42. } HEVCParserContext;
  43. static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal,
  44. AVCodecContext *avctx)
  45. {
  46. HEVCParserContext *ctx = s->priv_data;
  47. HEVCParamSets *ps = &ctx->ps;
  48. HEVCSEIContext *sei = &ctx->sei;
  49. SliceHeader *sh = &ctx->sh;
  50. GetBitContext *gb = &nal->gb;
  51. int i, num = 0, den = 0;
  52. sh->first_slice_in_pic_flag = get_bits1(gb);
  53. s->picture_structure = sei->picture_timing.picture_struct;
  54. s->field_order = sei->picture_timing.picture_struct;
  55. if (IS_IRAP_NAL(nal)) {
  56. s->key_frame = 1;
  57. sh->no_output_of_prior_pics_flag = get_bits1(gb);
  58. }
  59. sh->pps_id = get_ue_golomb(gb);
  60. if (sh->pps_id >= HEVC_MAX_PPS_COUNT || !ps->pps_list[sh->pps_id]) {
  61. av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
  62. return AVERROR_INVALIDDATA;
  63. }
  64. ps->pps = (HEVCPPS*)ps->pps_list[sh->pps_id]->data;
  65. if (ps->pps->sps_id >= HEVC_MAX_SPS_COUNT || !ps->sps_list[ps->pps->sps_id]) {
  66. av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", ps->pps->sps_id);
  67. return AVERROR_INVALIDDATA;
  68. }
  69. if (ps->sps != (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data) {
  70. ps->sps = (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data;
  71. ps->vps = (HEVCVPS*)ps->vps_list[ps->sps->vps_id]->data;
  72. }
  73. s->coded_width = ps->sps->width;
  74. s->coded_height = ps->sps->height;
  75. s->width = ps->sps->output_width;
  76. s->height = ps->sps->output_height;
  77. s->format = ps->sps->pix_fmt;
  78. avctx->profile = ps->sps->ptl.general_ptl.profile_idc;
  79. avctx->level = ps->sps->ptl.general_ptl.level_idc;
  80. if (ps->vps->vps_timing_info_present_flag) {
  81. num = ps->vps->vps_num_units_in_tick;
  82. den = ps->vps->vps_time_scale;
  83. } else if (ps->sps->vui.vui_timing_info_present_flag) {
  84. num = ps->sps->vui.vui_num_units_in_tick;
  85. den = ps->sps->vui.vui_time_scale;
  86. }
  87. if (num != 0 && den != 0)
  88. av_reduce(&avctx->framerate.den, &avctx->framerate.num,
  89. num, den, 1 << 30);
  90. if (!sh->first_slice_in_pic_flag) {
  91. int slice_address_length;
  92. if (ps->pps->dependent_slice_segments_enabled_flag)
  93. sh->dependent_slice_segment_flag = get_bits1(gb);
  94. else
  95. sh->dependent_slice_segment_flag = 0;
  96. slice_address_length = av_ceil_log2_c(ps->sps->ctb_width *
  97. ps->sps->ctb_height);
  98. sh->slice_segment_addr = get_bitsz(gb, slice_address_length);
  99. if (sh->slice_segment_addr >= ps->sps->ctb_width * ps->sps->ctb_height) {
  100. av_log(avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
  101. sh->slice_segment_addr);
  102. return AVERROR_INVALIDDATA;
  103. }
  104. } else
  105. sh->dependent_slice_segment_flag = 0;
  106. if (sh->dependent_slice_segment_flag)
  107. return 0; /* break; */
  108. for (i = 0; i < ps->pps->num_extra_slice_header_bits; i++)
  109. skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
  110. sh->slice_type = get_ue_golomb(gb);
  111. if (!(sh->slice_type == HEVC_SLICE_I || sh->slice_type == HEVC_SLICE_P ||
  112. sh->slice_type == HEVC_SLICE_B)) {
  113. av_log(avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
  114. sh->slice_type);
  115. return AVERROR_INVALIDDATA;
  116. }
  117. s->pict_type = sh->slice_type == HEVC_SLICE_B ? AV_PICTURE_TYPE_B :
  118. sh->slice_type == HEVC_SLICE_P ? AV_PICTURE_TYPE_P :
  119. AV_PICTURE_TYPE_I;
  120. if (ps->pps->output_flag_present_flag)
  121. sh->pic_output_flag = get_bits1(gb);
  122. if (ps->sps->separate_colour_plane_flag)
  123. sh->colour_plane_id = get_bits(gb, 2);
  124. if (!IS_IDR_NAL(nal)) {
  125. sh->pic_order_cnt_lsb = get_bits(gb, ps->sps->log2_max_poc_lsb);
  126. s->output_picture_number = ctx->poc = ff_hevc_compute_poc(ps->sps, ctx->pocTid0, sh->pic_order_cnt_lsb, nal->type);
  127. } else
  128. s->output_picture_number = ctx->poc = 0;
  129. if (nal->temporal_id == 0 &&
  130. nal->type != HEVC_NAL_TRAIL_N &&
  131. nal->type != HEVC_NAL_TSA_N &&
  132. nal->type != HEVC_NAL_STSA_N &&
  133. nal->type != HEVC_NAL_RADL_N &&
  134. nal->type != HEVC_NAL_RASL_N &&
  135. nal->type != HEVC_NAL_RADL_R &&
  136. nal->type != HEVC_NAL_RASL_R)
  137. ctx->pocTid0 = ctx->poc;
  138. return 1; /* no need to evaluate the rest */
  139. }
  140. /**
  141. * Parse NAL units of found picture and decode some basic information.
  142. *
  143. * @param s parser context.
  144. * @param avctx codec context.
  145. * @param buf buffer with field/frame data.
  146. * @param buf_size size of the buffer.
  147. */
  148. static inline int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
  149. int buf_size, AVCodecContext *avctx)
  150. {
  151. HEVCParserContext *ctx = s->priv_data;
  152. HEVCParamSets *ps = &ctx->ps;
  153. HEVCSEIContext *sei = &ctx->sei;
  154. int is_global = buf == avctx->extradata;
  155. int i, ret;
  156. /* set some sane default values */
  157. s->pict_type = AV_PICTURE_TYPE_I;
  158. s->key_frame = 0;
  159. s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
  160. ff_hevc_reset_sei(sei);
  161. ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx, 0, 0,
  162. AV_CODEC_ID_HEVC, 1);
  163. if (ret < 0)
  164. return ret;
  165. for (i = 0; i < ctx->pkt.nb_nals; i++) {
  166. H2645NAL *nal = &ctx->pkt.nals[i];
  167. GetBitContext *gb = &nal->gb;
  168. switch (nal->type) {
  169. case HEVC_NAL_VPS:
  170. ff_hevc_decode_nal_vps(gb, avctx, ps);
  171. break;
  172. case HEVC_NAL_SPS:
  173. ff_hevc_decode_nal_sps(gb, avctx, ps, 1);
  174. break;
  175. case HEVC_NAL_PPS:
  176. ff_hevc_decode_nal_pps(gb, avctx, ps);
  177. break;
  178. case HEVC_NAL_SEI_PREFIX:
  179. case HEVC_NAL_SEI_SUFFIX:
  180. ff_hevc_decode_nal_sei(gb, avctx, sei, ps, nal->type);
  181. break;
  182. case HEVC_NAL_TRAIL_N:
  183. case HEVC_NAL_TRAIL_R:
  184. case HEVC_NAL_TSA_N:
  185. case HEVC_NAL_TSA_R:
  186. case HEVC_NAL_STSA_N:
  187. case HEVC_NAL_STSA_R:
  188. case HEVC_NAL_RADL_N:
  189. case HEVC_NAL_RADL_R:
  190. case HEVC_NAL_RASL_N:
  191. case HEVC_NAL_RASL_R:
  192. case HEVC_NAL_BLA_W_LP:
  193. case HEVC_NAL_BLA_W_RADL:
  194. case HEVC_NAL_BLA_N_LP:
  195. case HEVC_NAL_IDR_W_RADL:
  196. case HEVC_NAL_IDR_N_LP:
  197. case HEVC_NAL_CRA_NUT:
  198. if (is_global) {
  199. av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit: %d\n", nal->type);
  200. return AVERROR_INVALIDDATA;
  201. }
  202. ret = hevc_parse_slice_header(s, nal, avctx);
  203. if (ret)
  204. return ret;
  205. break;
  206. }
  207. }
  208. /* didn't find a picture! */
  209. if (!is_global)
  210. av_log(avctx, AV_LOG_ERROR, "missing picture in access unit\n");
  211. return -1;
  212. }
  213. /**
  214. * Find the end of the current frame in the bitstream.
  215. * @return the position of the first byte of the next frame, or END_NOT_FOUND
  216. */
  217. static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
  218. int buf_size)
  219. {
  220. HEVCParserContext *ctx = s->priv_data;
  221. ParseContext *pc = &ctx->pc;
  222. int i;
  223. for (i = 0; i < buf_size; i++) {
  224. int nut;
  225. pc->state64 = (pc->state64 << 8) | buf[i];
  226. if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
  227. continue;
  228. nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
  229. // Beginning of access unit
  230. if ((nut >= HEVC_NAL_VPS && nut <= HEVC_NAL_AUD) || nut == HEVC_NAL_SEI_PREFIX ||
  231. (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
  232. if (pc->frame_start_found) {
  233. pc->frame_start_found = 0;
  234. return i - 5;
  235. }
  236. } else if (nut <= HEVC_NAL_RASL_R ||
  237. (nut >= HEVC_NAL_BLA_W_LP && nut <= HEVC_NAL_CRA_NUT)) {
  238. int first_slice_segment_in_pic_flag = buf[i] >> 7;
  239. if (first_slice_segment_in_pic_flag) {
  240. if (!pc->frame_start_found) {
  241. pc->frame_start_found = 1;
  242. } else { // First slice of next frame found
  243. pc->frame_start_found = 0;
  244. return i - 5;
  245. }
  246. }
  247. }
  248. }
  249. return END_NOT_FOUND;
  250. }
  251. static int hevc_parse(AVCodecParserContext *s,
  252. AVCodecContext *avctx,
  253. const uint8_t **poutbuf, int *poutbuf_size,
  254. const uint8_t *buf, int buf_size)
  255. {
  256. int next;
  257. HEVCParserContext *ctx = s->priv_data;
  258. ParseContext *pc = &ctx->pc;
  259. if (avctx->extradata && !ctx->parsed_extradata) {
  260. parse_nal_units(s, avctx->extradata, avctx->extradata_size, avctx);
  261. ctx->parsed_extradata = 1;
  262. }
  263. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  264. next = buf_size;
  265. } else {
  266. next = hevc_find_frame_end(s, buf, buf_size);
  267. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  268. *poutbuf = NULL;
  269. *poutbuf_size = 0;
  270. return buf_size;
  271. }
  272. }
  273. parse_nal_units(s, buf, buf_size, avctx);
  274. *poutbuf = buf;
  275. *poutbuf_size = buf_size;
  276. return next;
  277. }
  278. // Split after the parameter sets at the beginning of the stream if they exist.
  279. static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
  280. {
  281. const uint8_t *ptr = buf, *end = buf + buf_size;
  282. uint32_t state = -1;
  283. int has_vps = 0;
  284. int has_sps = 0;
  285. int has_pps = 0;
  286. int nut;
  287. while (ptr < end) {
  288. ptr = avpriv_find_start_code(ptr, end, &state);
  289. if ((state >> 8) != START_CODE)
  290. break;
  291. nut = (state >> 1) & 0x3F;
  292. if (nut == HEVC_NAL_VPS)
  293. has_vps = 1;
  294. else if (nut == HEVC_NAL_SPS)
  295. has_sps = 1;
  296. else if (nut == HEVC_NAL_PPS)
  297. has_pps = 1;
  298. else if ((nut != HEVC_NAL_SEI_PREFIX || has_pps) &&
  299. nut != HEVC_NAL_AUD) {
  300. if (has_vps && has_sps) {
  301. while (ptr - 4 > buf && ptr[-5] == 0)
  302. ptr--;
  303. return ptr - 4 - buf;
  304. }
  305. }
  306. }
  307. return 0;
  308. }
  309. static void hevc_parser_close(AVCodecParserContext *s)
  310. {
  311. HEVCParserContext *ctx = s->priv_data;
  312. int i;
  313. for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.vps_list); i++)
  314. av_buffer_unref(&ctx->ps.vps_list[i]);
  315. for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.sps_list); i++)
  316. av_buffer_unref(&ctx->ps.sps_list[i]);
  317. for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.pps_list); i++)
  318. av_buffer_unref(&ctx->ps.pps_list[i]);
  319. ctx->ps.sps = NULL;
  320. ff_h2645_packet_uninit(&ctx->pkt);
  321. av_freep(&ctx->pc.buffer);
  322. }
  323. AVCodecParser ff_hevc_parser = {
  324. .codec_ids = { AV_CODEC_ID_HEVC },
  325. .priv_data_size = sizeof(HEVCParserContext),
  326. .parser_parse = hevc_parse,
  327. .parser_close = hevc_parser_close,
  328. .split = hevc_split,
  329. };