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.

390 lines
13KB

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