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.

374 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_parse.h"
  26. #include "hevc_ps.h"
  27. #include "hevc_sei.h"
  28. #include "h2645_parse.h"
  29. #include "internal.h"
  30. #include "parser.h"
  31. #define START_CODE 0x000001 ///< start_code_prefix_one_3bytes
  32. #define IS_IRAP_NAL(nal) (nal->type >= 16 && nal->type <= 23)
  33. #define IS_IDR_NAL(nal) (nal->type == HEVC_NAL_IDR_W_RADL || nal->type == HEVC_NAL_IDR_N_LP)
  34. typedef struct HEVCParserContext {
  35. ParseContext pc;
  36. H2645Packet pkt;
  37. HEVCParamSets ps;
  38. HEVCSEI sei;
  39. SliceHeader sh;
  40. int is_avc;
  41. int nal_length_size;
  42. int parsed_extradata;
  43. int poc;
  44. int pocTid0;
  45. } HEVCParserContext;
  46. static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal,
  47. AVCodecContext *avctx)
  48. {
  49. HEVCParserContext *ctx = s->priv_data;
  50. HEVCParamSets *ps = &ctx->ps;
  51. HEVCSEI *sei = &ctx->sei;
  52. SliceHeader *sh = &ctx->sh;
  53. GetBitContext *gb = &nal->gb;
  54. const HEVCWindow *ow;
  55. int i, num = 0, den = 0;
  56. sh->first_slice_in_pic_flag = get_bits1(gb);
  57. s->picture_structure = sei->picture_timing.picture_struct;
  58. s->field_order = sei->picture_timing.picture_struct;
  59. if (IS_IRAP_NAL(nal)) {
  60. s->key_frame = 1;
  61. sh->no_output_of_prior_pics_flag = get_bits1(gb);
  62. }
  63. sh->pps_id = get_ue_golomb(gb);
  64. if (sh->pps_id >= HEVC_MAX_PPS_COUNT || !ps->pps_list[sh->pps_id]) {
  65. av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
  66. return AVERROR_INVALIDDATA;
  67. }
  68. ps->pps = (HEVCPPS*)ps->pps_list[sh->pps_id]->data;
  69. if (ps->pps->sps_id >= HEVC_MAX_SPS_COUNT || !ps->sps_list[ps->pps->sps_id]) {
  70. av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", ps->pps->sps_id);
  71. return AVERROR_INVALIDDATA;
  72. }
  73. if (ps->sps != (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data) {
  74. ps->sps = (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data;
  75. ps->vps = (HEVCVPS*)ps->vps_list[ps->sps->vps_id]->data;
  76. }
  77. ow = &ps->sps->output_window;
  78. s->coded_width = ps->sps->width;
  79. s->coded_height = ps->sps->height;
  80. s->width = ps->sps->width - ow->left_offset - ow->right_offset;
  81. s->height = ps->sps->height - ow->top_offset - ow->bottom_offset;
  82. s->format = ps->sps->pix_fmt;
  83. avctx->profile = ps->sps->ptl.general_ptl.profile_idc;
  84. avctx->level = ps->sps->ptl.general_ptl.level_idc;
  85. if (ps->vps->vps_timing_info_present_flag) {
  86. num = ps->vps->vps_num_units_in_tick;
  87. den = ps->vps->vps_time_scale;
  88. } else if (ps->sps->vui.vui_timing_info_present_flag) {
  89. num = ps->sps->vui.vui_num_units_in_tick;
  90. den = ps->sps->vui.vui_time_scale;
  91. }
  92. if (num != 0 && den != 0)
  93. av_reduce(&avctx->framerate.den, &avctx->framerate.num,
  94. num, den, 1 << 30);
  95. if (!sh->first_slice_in_pic_flag) {
  96. int slice_address_length;
  97. if (ps->pps->dependent_slice_segments_enabled_flag)
  98. sh->dependent_slice_segment_flag = get_bits1(gb);
  99. else
  100. sh->dependent_slice_segment_flag = 0;
  101. slice_address_length = av_ceil_log2_c(ps->sps->ctb_width *
  102. ps->sps->ctb_height);
  103. sh->slice_segment_addr = get_bitsz(gb, slice_address_length);
  104. if (sh->slice_segment_addr >= ps->sps->ctb_width * ps->sps->ctb_height) {
  105. av_log(avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
  106. sh->slice_segment_addr);
  107. return AVERROR_INVALIDDATA;
  108. }
  109. } else
  110. sh->dependent_slice_segment_flag = 0;
  111. if (sh->dependent_slice_segment_flag)
  112. return 0; /* break; */
  113. for (i = 0; i < ps->pps->num_extra_slice_header_bits; i++)
  114. skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
  115. sh->slice_type = get_ue_golomb(gb);
  116. if (!(sh->slice_type == HEVC_SLICE_I || sh->slice_type == HEVC_SLICE_P ||
  117. sh->slice_type == HEVC_SLICE_B)) {
  118. av_log(avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
  119. sh->slice_type);
  120. return AVERROR_INVALIDDATA;
  121. }
  122. s->pict_type = sh->slice_type == HEVC_SLICE_B ? AV_PICTURE_TYPE_B :
  123. sh->slice_type == HEVC_SLICE_P ? AV_PICTURE_TYPE_P :
  124. AV_PICTURE_TYPE_I;
  125. if (ps->pps->output_flag_present_flag)
  126. sh->pic_output_flag = get_bits1(gb);
  127. if (ps->sps->separate_colour_plane_flag)
  128. sh->colour_plane_id = get_bits(gb, 2);
  129. if (!IS_IDR_NAL(nal)) {
  130. sh->pic_order_cnt_lsb = get_bits(gb, ps->sps->log2_max_poc_lsb);
  131. s->output_picture_number = ctx->poc = ff_hevc_compute_poc(ps->sps, ctx->pocTid0, sh->pic_order_cnt_lsb, nal->type);
  132. } else
  133. s->output_picture_number = ctx->poc = 0;
  134. if (nal->temporal_id == 0 &&
  135. nal->type != HEVC_NAL_TRAIL_N &&
  136. nal->type != HEVC_NAL_TSA_N &&
  137. nal->type != HEVC_NAL_STSA_N &&
  138. nal->type != HEVC_NAL_RADL_N &&
  139. nal->type != HEVC_NAL_RASL_N &&
  140. nal->type != HEVC_NAL_RADL_R &&
  141. nal->type != HEVC_NAL_RASL_R)
  142. ctx->pocTid0 = ctx->poc;
  143. return 1; /* no need to evaluate the rest */
  144. }
  145. /**
  146. * Parse NAL units of found picture and decode some basic information.
  147. *
  148. * @param s parser context.
  149. * @param avctx codec context.
  150. * @param buf buffer with field/frame data.
  151. * @param buf_size size of the buffer.
  152. */
  153. static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
  154. int buf_size, AVCodecContext *avctx)
  155. {
  156. HEVCParserContext *ctx = s->priv_data;
  157. HEVCParamSets *ps = &ctx->ps;
  158. HEVCSEI *sei = &ctx->sei;
  159. int ret, i;
  160. /* set some sane default values */
  161. s->pict_type = AV_PICTURE_TYPE_I;
  162. s->key_frame = 0;
  163. s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
  164. ff_hevc_reset_sei(sei);
  165. ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx, ctx->is_avc,
  166. ctx->nal_length_size, AV_CODEC_ID_HEVC, 1);
  167. if (ret < 0)
  168. return ret;
  169. for (i = 0; i < ctx->pkt.nb_nals; i++) {
  170. H2645NAL *nal = &ctx->pkt.nals[i];
  171. GetBitContext *gb = &nal->gb;
  172. switch (nal->type) {
  173. case HEVC_NAL_VPS:
  174. ff_hevc_decode_nal_vps(gb, avctx, ps);
  175. break;
  176. case HEVC_NAL_SPS:
  177. ff_hevc_decode_nal_sps(gb, avctx, ps, 1);
  178. break;
  179. case HEVC_NAL_PPS:
  180. ff_hevc_decode_nal_pps(gb, avctx, ps);
  181. break;
  182. case HEVC_NAL_SEI_PREFIX:
  183. case HEVC_NAL_SEI_SUFFIX:
  184. ff_hevc_decode_nal_sei(gb, avctx, sei, ps, nal->type);
  185. break;
  186. case HEVC_NAL_TRAIL_N:
  187. case HEVC_NAL_TRAIL_R:
  188. case HEVC_NAL_TSA_N:
  189. case HEVC_NAL_TSA_R:
  190. case HEVC_NAL_STSA_N:
  191. case HEVC_NAL_STSA_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. case HEVC_NAL_RADL_N:
  199. case HEVC_NAL_RADL_R:
  200. case HEVC_NAL_RASL_N:
  201. case HEVC_NAL_RASL_R:
  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. av_log(avctx, AV_LOG_ERROR, "missing picture in access unit\n");
  210. return -1;
  211. }
  212. /**
  213. * Find the end of the current frame in the bitstream.
  214. * @return the position of the first byte of the next frame, or END_NOT_FOUND
  215. */
  216. static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
  217. int buf_size)
  218. {
  219. HEVCParserContext *ctx = s->priv_data;
  220. ParseContext *pc = &ctx->pc;
  221. int i;
  222. for (i = 0; i < buf_size; i++) {
  223. int nut;
  224. pc->state64 = (pc->state64 << 8) | buf[i];
  225. if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
  226. continue;
  227. nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
  228. // Beginning of access unit
  229. if ((nut >= HEVC_NAL_VPS && nut <= HEVC_NAL_EOB_NUT) || nut == HEVC_NAL_SEI_PREFIX ||
  230. (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
  231. if (pc->frame_start_found) {
  232. pc->frame_start_found = 0;
  233. return i - 5;
  234. }
  235. } else if (nut <= HEVC_NAL_RASL_R ||
  236. (nut >= HEVC_NAL_BLA_W_LP && nut <= HEVC_NAL_CRA_NUT)) {
  237. int first_slice_segment_in_pic_flag = buf[i] >> 7;
  238. if (first_slice_segment_in_pic_flag) {
  239. if (!pc->frame_start_found) {
  240. pc->frame_start_found = 1;
  241. } else { // First slice of next frame found
  242. pc->frame_start_found = 0;
  243. return i - 5;
  244. }
  245. }
  246. }
  247. }
  248. return END_NOT_FOUND;
  249. }
  250. static int hevc_parse(AVCodecParserContext *s, AVCodecContext *avctx,
  251. const uint8_t **poutbuf, int *poutbuf_size,
  252. const uint8_t *buf, int buf_size)
  253. {
  254. int next;
  255. HEVCParserContext *ctx = s->priv_data;
  256. ParseContext *pc = &ctx->pc;
  257. if (avctx->extradata && !ctx->parsed_extradata) {
  258. ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size, &ctx->ps, &ctx->sei,
  259. &ctx->is_avc, &ctx->nal_length_size, avctx->err_recognition,
  260. 1, 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. ff_hevc_ps_uninit(&ctx->ps);
  313. ff_h2645_packet_uninit(&ctx->pkt);
  314. ff_hevc_reset_sei(&ctx->sei);
  315. av_freep(&ctx->pc.buffer);
  316. }
  317. AVCodecParser ff_hevc_parser = {
  318. .codec_ids = { AV_CODEC_ID_HEVC },
  319. .priv_data_size = sizeof(HEVCParserContext),
  320. .parser_parse = hevc_parse,
  321. .parser_close = hevc_parser_close,
  322. .split = hevc_split,
  323. };