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.

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