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.

480 lines
16KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... parser
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * H.264 / AVC / MPEG4 part10 parser.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #define UNCHECKED_BITSTREAM_READER 1
  27. #include "libavutil/attributes.h"
  28. #include "parser.h"
  29. #include "h264data.h"
  30. #include "golomb.h"
  31. #include "internal.h"
  32. static int h264_find_frame_end(H264Context *h, const uint8_t *buf,
  33. int buf_size)
  34. {
  35. int i, j;
  36. uint32_t state;
  37. ParseContext *pc = &h->parse_context;
  38. int next_avc= h->is_avc ? 0 : buf_size;
  39. // mb_addr= pc->mb_addr - 1;
  40. state = pc->state;
  41. if (state > 13)
  42. state = 7;
  43. if (h->is_avc && !h->nal_length_size)
  44. av_log(h->avctx, AV_LOG_ERROR, "AVC-parser: nal length size invalid\n");
  45. for (i = 0; i < buf_size; i++) {
  46. if (i >= next_avc) {
  47. int nalsize = 0;
  48. i = next_avc;
  49. for (j = 0; j < h->nal_length_size; j++)
  50. nalsize = (nalsize << 8) | buf[i++];
  51. if (nalsize <= 0 || nalsize > buf_size - i) {
  52. av_log(h->avctx, AV_LOG_ERROR, "AVC-parser: nal size %d remaining %d\n", nalsize, buf_size - i);
  53. return buf_size;
  54. }
  55. next_avc = i + nalsize;
  56. state = 5;
  57. }
  58. if (state == 7) {
  59. #if HAVE_FAST_UNALIGNED
  60. /* we check i < buf_size instead of i + 3 / 7 because it is
  61. * simpler and there must be FF_INPUT_BUFFER_PADDING_SIZE
  62. * bytes at the end.
  63. */
  64. # if HAVE_FAST_64BIT
  65. while (i < next_avc &&
  66. !((~*(const uint64_t *)(buf + i) &
  67. (*(const uint64_t *)(buf + i) - 0x0101010101010101ULL)) &
  68. 0x8080808080808080ULL))
  69. i += 8;
  70. # else
  71. while (i < next_avc &&
  72. !((~*(const uint32_t *)(buf + i) &
  73. (*(const uint32_t *)(buf + i) - 0x01010101U)) &
  74. 0x80808080U))
  75. i += 4;
  76. # endif
  77. #endif
  78. for (; i < next_avc; i++)
  79. if (!buf[i]) {
  80. state = 2;
  81. break;
  82. }
  83. } else if (state <= 2) {
  84. if (buf[i] == 1)
  85. state ^= 5; // 2->7, 1->4, 0->5
  86. else if (buf[i])
  87. state = 7;
  88. else
  89. state >>= 1; // 2->1, 1->0, 0->0
  90. } else if (state <= 5) {
  91. int v = buf[i] & 0x1F;
  92. if (v == 6 || v == 7 || v == 8 || v == 9) {
  93. if (pc->frame_start_found) {
  94. i++;
  95. goto found;
  96. }
  97. } else if (v == 1 || v == 2 || v == 5) {
  98. state += 8;
  99. continue;
  100. }
  101. state = 7;
  102. } else {
  103. h->parse_history[h->parse_history_count++]= buf[i];
  104. if (h->parse_history_count>3) {
  105. unsigned int mb, last_mb= h->parse_last_mb;
  106. GetBitContext gb;
  107. init_get_bits(&gb, h->parse_history, 8*h->parse_history_count);
  108. h->parse_history_count=0;
  109. mb= get_ue_golomb_long(&gb);
  110. last_mb= h->parse_last_mb;
  111. h->parse_last_mb= mb;
  112. if (pc->frame_start_found) {
  113. if (mb <= last_mb)
  114. goto found;
  115. } else
  116. pc->frame_start_found = 1;
  117. state = 7;
  118. }
  119. }
  120. }
  121. pc->state = state;
  122. if (h->is_avc)
  123. return next_avc;
  124. return END_NOT_FOUND;
  125. found:
  126. pc->state = 7;
  127. pc->frame_start_found = 0;
  128. if (h->is_avc)
  129. return next_avc;
  130. return i - (state & 5) - 3 * (state > 7);
  131. }
  132. /**
  133. * Parse NAL units of found picture and decode some basic information.
  134. *
  135. * @param s parser context.
  136. * @param avctx codec context.
  137. * @param buf buffer with field/frame data.
  138. * @param buf_size size of the buffer.
  139. */
  140. static inline int parse_nal_units(AVCodecParserContext *s,
  141. AVCodecContext *avctx,
  142. const uint8_t *buf, int buf_size)
  143. {
  144. H264Context *h = s->priv_data;
  145. const uint8_t *buf_end = buf + buf_size;
  146. unsigned int pps_id;
  147. unsigned int slice_type;
  148. int state = -1;
  149. const uint8_t *ptr;
  150. int q264 = buf_size >=4 && !memcmp("Q264", buf, 4);
  151. int field_poc[2];
  152. /* set some sane default values */
  153. s->pict_type = AV_PICTURE_TYPE_I;
  154. s->key_frame = 0;
  155. s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
  156. h->avctx = avctx;
  157. h->sei_recovery_frame_cnt = -1;
  158. h->sei_dpb_output_delay = 0;
  159. h->sei_cpb_removal_delay = -1;
  160. h->sei_buffering_period_present = 0;
  161. h->sei_fpa.frame_packing_arrangement_cancel_flag = -1;
  162. if (!buf_size)
  163. return 0;
  164. for (;;) {
  165. int src_length, dst_length, consumed, nalsize = 0;
  166. if (h->is_avc) {
  167. int i;
  168. if (h->nal_length_size >= buf_end - buf) break;
  169. nalsize = 0;
  170. for (i = 0; i < h->nal_length_size; i++)
  171. nalsize = (nalsize << 8) | *buf++;
  172. if (nalsize <= 0 || nalsize > buf_end - buf) {
  173. av_log(h->avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  174. break;
  175. }
  176. src_length = nalsize;
  177. } else {
  178. buf = avpriv_find_start_code(buf, buf_end, &state);
  179. if (buf >= buf_end)
  180. break;
  181. --buf;
  182. src_length = buf_end - buf;
  183. }
  184. switch (state & 0x1f) {
  185. case NAL_SLICE:
  186. case NAL_IDR_SLICE:
  187. // Do not walk the whole buffer just to decode slice header
  188. if (src_length > 20)
  189. src_length = 20;
  190. break;
  191. }
  192. ptr = ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length);
  193. if (ptr == NULL || dst_length < 0)
  194. break;
  195. init_get_bits(&h->gb, ptr, 8 * dst_length);
  196. switch (h->nal_unit_type) {
  197. case NAL_SPS:
  198. ff_h264_decode_seq_parameter_set(h);
  199. break;
  200. case NAL_PPS:
  201. ff_h264_decode_picture_parameter_set(h, h->gb.size_in_bits);
  202. break;
  203. case NAL_SEI:
  204. ff_h264_decode_sei(h);
  205. break;
  206. case NAL_IDR_SLICE:
  207. s->key_frame = 1;
  208. h->prev_frame_num = 0;
  209. h->prev_frame_num_offset = 0;
  210. h->prev_poc_msb =
  211. h->prev_poc_lsb = 0;
  212. /* fall through */
  213. case NAL_SLICE:
  214. get_ue_golomb_long(&h->gb); // skip first_mb_in_slice
  215. slice_type = get_ue_golomb_31(&h->gb);
  216. s->pict_type = golomb_to_pict_type[slice_type % 5];
  217. if (h->sei_recovery_frame_cnt >= 0) {
  218. /* key frame, since recovery_frame_cnt is set */
  219. s->key_frame = 1;
  220. }
  221. pps_id = get_ue_golomb(&h->gb);
  222. if (pps_id >= MAX_PPS_COUNT) {
  223. av_log(h->avctx, AV_LOG_ERROR,
  224. "pps_id out of range\n");
  225. return -1;
  226. }
  227. if (!h->pps_buffers[pps_id]) {
  228. av_log(h->avctx, AV_LOG_ERROR,
  229. "non-existing PPS referenced\n");
  230. return -1;
  231. }
  232. h->pps = *h->pps_buffers[pps_id];
  233. if (!h->sps_buffers[h->pps.sps_id]) {
  234. av_log(h->avctx, AV_LOG_ERROR,
  235. "non-existing SPS referenced\n");
  236. return -1;
  237. }
  238. h->sps = *h->sps_buffers[h->pps.sps_id];
  239. h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num);
  240. avctx->profile = ff_h264_get_profile(&h->sps);
  241. avctx->level = h->sps.level_idc;
  242. if (h->sps.frame_mbs_only_flag) {
  243. h->picture_structure = PICT_FRAME;
  244. } else {
  245. if (get_bits1(&h->gb)) { // field_pic_flag
  246. h->picture_structure = PICT_TOP_FIELD + get_bits1(&h->gb); // bottom_field_flag
  247. } else {
  248. h->picture_structure = PICT_FRAME;
  249. }
  250. }
  251. if (h->nal_unit_type == NAL_IDR_SLICE)
  252. get_ue_golomb(&h->gb); /* idr_pic_id */
  253. if (h->sps.poc_type == 0) {
  254. h->poc_lsb = get_bits(&h->gb, h->sps.log2_max_poc_lsb);
  255. if (h->pps.pic_order_present == 1 &&
  256. h->picture_structure == PICT_FRAME)
  257. h->delta_poc_bottom = get_se_golomb(&h->gb);
  258. }
  259. if (h->sps.poc_type == 1 &&
  260. !h->sps.delta_pic_order_always_zero_flag) {
  261. h->delta_poc[0] = get_se_golomb(&h->gb);
  262. if (h->pps.pic_order_present == 1 &&
  263. h->picture_structure == PICT_FRAME)
  264. h->delta_poc[1] = get_se_golomb(&h->gb);
  265. }
  266. ff_init_poc(h, field_poc, NULL);
  267. if (h->sps.pic_struct_present_flag) {
  268. switch (h->sei_pic_struct) {
  269. case SEI_PIC_STRUCT_TOP_FIELD:
  270. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  271. s->repeat_pict = 0;
  272. break;
  273. case SEI_PIC_STRUCT_FRAME:
  274. case SEI_PIC_STRUCT_TOP_BOTTOM:
  275. case SEI_PIC_STRUCT_BOTTOM_TOP:
  276. s->repeat_pict = 1;
  277. break;
  278. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  279. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  280. s->repeat_pict = 2;
  281. break;
  282. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  283. s->repeat_pict = 3;
  284. break;
  285. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  286. s->repeat_pict = 5;
  287. break;
  288. default:
  289. s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
  290. break;
  291. }
  292. } else {
  293. s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
  294. }
  295. if (h->picture_structure == PICT_FRAME) {
  296. s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
  297. if (h->sps.pic_struct_present_flag) {
  298. switch (h->sei_pic_struct) {
  299. case SEI_PIC_STRUCT_TOP_BOTTOM:
  300. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  301. s->field_order = AV_FIELD_TT;
  302. break;
  303. case SEI_PIC_STRUCT_BOTTOM_TOP:
  304. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  305. s->field_order = AV_FIELD_BB;
  306. break;
  307. default:
  308. s->field_order = AV_FIELD_PROGRESSIVE;
  309. break;
  310. }
  311. } else {
  312. if (field_poc[0] < field_poc[1])
  313. s->field_order = AV_FIELD_TT;
  314. else if (field_poc[0] > field_poc[1])
  315. s->field_order = AV_FIELD_BB;
  316. else
  317. s->field_order = AV_FIELD_PROGRESSIVE;
  318. }
  319. } else {
  320. if (h->picture_structure == PICT_TOP_FIELD)
  321. s->picture_structure = AV_PICTURE_STRUCTURE_TOP_FIELD;
  322. else
  323. s->picture_structure = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
  324. s->field_order = AV_FIELD_UNKNOWN;
  325. }
  326. return 0; /* no need to evaluate the rest */
  327. }
  328. buf += h->is_avc ? nalsize : consumed;
  329. }
  330. if (q264)
  331. return 0;
  332. /* didn't find a picture! */
  333. av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
  334. return -1;
  335. }
  336. static int h264_parse(AVCodecParserContext *s,
  337. AVCodecContext *avctx,
  338. const uint8_t **poutbuf, int *poutbuf_size,
  339. const uint8_t *buf, int buf_size)
  340. {
  341. H264Context *h = s->priv_data;
  342. ParseContext *pc = &h->parse_context;
  343. int next;
  344. if (!h->got_first) {
  345. h->got_first = 1;
  346. if (avctx->extradata_size) {
  347. h->avctx = avctx;
  348. // must be done like in decoder, otherwise opening the parser,
  349. // letting it create extradata and then closing and opening again
  350. // will cause has_b_frames to be always set.
  351. // Note that estimate_timings_from_pts does exactly this.
  352. if (!avctx->has_b_frames)
  353. h->low_delay = 1;
  354. ff_h264_decode_extradata(h, avctx->extradata, avctx->extradata_size);
  355. }
  356. }
  357. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  358. next = buf_size;
  359. } else {
  360. next = h264_find_frame_end(h, buf, buf_size);
  361. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  362. *poutbuf = NULL;
  363. *poutbuf_size = 0;
  364. return buf_size;
  365. }
  366. if (next < 0 && next != END_NOT_FOUND) {
  367. av_assert1(pc->last_index + next >= 0);
  368. h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next); // update state
  369. }
  370. }
  371. parse_nal_units(s, avctx, buf, buf_size);
  372. if (h->sei_cpb_removal_delay >= 0) {
  373. s->dts_sync_point = h->sei_buffering_period_present;
  374. s->dts_ref_dts_delta = h->sei_cpb_removal_delay;
  375. s->pts_dts_delta = h->sei_dpb_output_delay;
  376. } else {
  377. s->dts_sync_point = INT_MIN;
  378. s->dts_ref_dts_delta = INT_MIN;
  379. s->pts_dts_delta = INT_MIN;
  380. }
  381. if (s->flags & PARSER_FLAG_ONCE) {
  382. s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
  383. }
  384. *poutbuf = buf;
  385. *poutbuf_size = buf_size;
  386. return next;
  387. }
  388. static int h264_split(AVCodecContext *avctx,
  389. const uint8_t *buf, int buf_size)
  390. {
  391. int i;
  392. uint32_t state = -1;
  393. int has_sps = 0;
  394. for (i = 0; i <= buf_size; i++) {
  395. if ((state & 0xFFFFFF1F) == 0x107)
  396. has_sps = 1;
  397. /* if ((state&0xFFFFFF1F) == 0x101 ||
  398. * (state&0xFFFFFF1F) == 0x102 ||
  399. * (state&0xFFFFFF1F) == 0x105) {
  400. * }
  401. */
  402. if ((state & 0xFFFFFF00) == 0x100 && (state & 0xFFFFFF1F) != 0x107 &&
  403. (state & 0xFFFFFF1F) != 0x108 && (state & 0xFFFFFF1F) != 0x109) {
  404. if (has_sps) {
  405. while (i > 4 && buf[i - 5] == 0)
  406. i--;
  407. return i - 4;
  408. }
  409. }
  410. if (i < buf_size)
  411. state = (state << 8) | buf[i];
  412. }
  413. return 0;
  414. }
  415. static void close(AVCodecParserContext *s)
  416. {
  417. H264Context *h = s->priv_data;
  418. ParseContext *pc = &h->parse_context;
  419. av_free(pc->buffer);
  420. ff_h264_free_context(h);
  421. }
  422. static av_cold int init(AVCodecParserContext *s)
  423. {
  424. H264Context *h = s->priv_data;
  425. h->thread_context[0] = h;
  426. h->slice_context_count = 1;
  427. return 0;
  428. }
  429. AVCodecParser ff_h264_parser = {
  430. .codec_ids = { AV_CODEC_ID_H264 },
  431. .priv_data_size = sizeof(H264Context),
  432. .parser_init = init,
  433. .parser_parse = h264_parse,
  434. .parser_close = close,
  435. .split = h264_split,
  436. };