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.

479 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. if (!buf_size)
  162. return 0;
  163. for (;;) {
  164. int src_length, dst_length, consumed, nalsize = 0;
  165. if (h->is_avc) {
  166. int i;
  167. if (h->nal_length_size >= buf_end - buf) break;
  168. nalsize = 0;
  169. for (i = 0; i < h->nal_length_size; i++)
  170. nalsize = (nalsize << 8) | *buf++;
  171. if (nalsize <= 0 || nalsize > buf_end - buf) {
  172. av_log(h->avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  173. break;
  174. }
  175. src_length = nalsize;
  176. } else {
  177. buf = avpriv_find_start_code(buf, buf_end, &state);
  178. if (buf >= buf_end)
  179. break;
  180. --buf;
  181. src_length = buf_end - buf;
  182. }
  183. switch (state & 0x1f) {
  184. case NAL_SLICE:
  185. case NAL_IDR_SLICE:
  186. // Do not walk the whole buffer just to decode slice header
  187. if (src_length > 20)
  188. src_length = 20;
  189. break;
  190. }
  191. ptr = ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length);
  192. if (ptr == NULL || dst_length < 0)
  193. break;
  194. init_get_bits(&h->gb, ptr, 8 * dst_length);
  195. switch (h->nal_unit_type) {
  196. case NAL_SPS:
  197. ff_h264_decode_seq_parameter_set(h);
  198. break;
  199. case NAL_PPS:
  200. ff_h264_decode_picture_parameter_set(h, h->gb.size_in_bits);
  201. break;
  202. case NAL_SEI:
  203. ff_h264_decode_sei(h);
  204. break;
  205. case NAL_IDR_SLICE:
  206. s->key_frame = 1;
  207. h->prev_frame_num = 0;
  208. h->prev_frame_num_offset = 0;
  209. h->prev_poc_msb =
  210. h->prev_poc_lsb = 0;
  211. /* fall through */
  212. case NAL_SLICE:
  213. get_ue_golomb_long(&h->gb); // skip first_mb_in_slice
  214. slice_type = get_ue_golomb_31(&h->gb);
  215. s->pict_type = golomb_to_pict_type[slice_type % 5];
  216. if (h->sei_recovery_frame_cnt >= 0) {
  217. /* key frame, since recovery_frame_cnt is set */
  218. s->key_frame = 1;
  219. }
  220. pps_id = get_ue_golomb(&h->gb);
  221. if (pps_id >= MAX_PPS_COUNT) {
  222. av_log(h->avctx, AV_LOG_ERROR,
  223. "pps_id out of range\n");
  224. return -1;
  225. }
  226. if (!h->pps_buffers[pps_id]) {
  227. av_log(h->avctx, AV_LOG_ERROR,
  228. "non-existing PPS referenced\n");
  229. return -1;
  230. }
  231. h->pps = *h->pps_buffers[pps_id];
  232. if (!h->sps_buffers[h->pps.sps_id]) {
  233. av_log(h->avctx, AV_LOG_ERROR,
  234. "non-existing SPS referenced\n");
  235. return -1;
  236. }
  237. h->sps = *h->sps_buffers[h->pps.sps_id];
  238. h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num);
  239. avctx->profile = ff_h264_get_profile(&h->sps);
  240. avctx->level = h->sps.level_idc;
  241. if (h->sps.frame_mbs_only_flag) {
  242. h->picture_structure = PICT_FRAME;
  243. } else {
  244. if (get_bits1(&h->gb)) { // field_pic_flag
  245. h->picture_structure = PICT_TOP_FIELD + get_bits1(&h->gb); // bottom_field_flag
  246. } else {
  247. h->picture_structure = PICT_FRAME;
  248. }
  249. }
  250. if (h->nal_unit_type == NAL_IDR_SLICE)
  251. get_ue_golomb(&h->gb); /* idr_pic_id */
  252. if (h->sps.poc_type == 0) {
  253. h->poc_lsb = get_bits(&h->gb, h->sps.log2_max_poc_lsb);
  254. if (h->pps.pic_order_present == 1 &&
  255. h->picture_structure == PICT_FRAME)
  256. h->delta_poc_bottom = get_se_golomb(&h->gb);
  257. }
  258. if (h->sps.poc_type == 1 &&
  259. !h->sps.delta_pic_order_always_zero_flag) {
  260. h->delta_poc[0] = get_se_golomb(&h->gb);
  261. if (h->pps.pic_order_present == 1 &&
  262. h->picture_structure == PICT_FRAME)
  263. h->delta_poc[1] = get_se_golomb(&h->gb);
  264. }
  265. ff_init_poc(h, field_poc, NULL);
  266. if (h->sps.pic_struct_present_flag) {
  267. switch (h->sei_pic_struct) {
  268. case SEI_PIC_STRUCT_TOP_FIELD:
  269. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  270. s->repeat_pict = 0;
  271. break;
  272. case SEI_PIC_STRUCT_FRAME:
  273. case SEI_PIC_STRUCT_TOP_BOTTOM:
  274. case SEI_PIC_STRUCT_BOTTOM_TOP:
  275. s->repeat_pict = 1;
  276. break;
  277. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  278. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  279. s->repeat_pict = 2;
  280. break;
  281. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  282. s->repeat_pict = 3;
  283. break;
  284. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  285. s->repeat_pict = 5;
  286. break;
  287. default:
  288. s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
  289. break;
  290. }
  291. } else {
  292. s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
  293. }
  294. if (h->picture_structure == PICT_FRAME) {
  295. s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
  296. if (h->sps.pic_struct_present_flag) {
  297. switch (h->sei_pic_struct) {
  298. case SEI_PIC_STRUCT_TOP_BOTTOM:
  299. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  300. s->field_order = AV_FIELD_TT;
  301. break;
  302. case SEI_PIC_STRUCT_BOTTOM_TOP:
  303. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  304. s->field_order = AV_FIELD_BB;
  305. break;
  306. default:
  307. s->field_order = AV_FIELD_PROGRESSIVE;
  308. break;
  309. }
  310. } else {
  311. if (field_poc[0] < field_poc[1])
  312. s->field_order = AV_FIELD_TT;
  313. else if (field_poc[0] > field_poc[1])
  314. s->field_order = AV_FIELD_BB;
  315. else
  316. s->field_order = AV_FIELD_PROGRESSIVE;
  317. }
  318. } else {
  319. if (h->picture_structure == PICT_TOP_FIELD)
  320. s->picture_structure = AV_PICTURE_STRUCTURE_TOP_FIELD;
  321. else
  322. s->picture_structure = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
  323. s->field_order = AV_FIELD_UNKNOWN;
  324. }
  325. return 0; /* no need to evaluate the rest */
  326. }
  327. buf += h->is_avc ? nalsize : consumed;
  328. }
  329. if (q264)
  330. return 0;
  331. /* didn't find a picture! */
  332. av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
  333. return -1;
  334. }
  335. static int h264_parse(AVCodecParserContext *s,
  336. AVCodecContext *avctx,
  337. const uint8_t **poutbuf, int *poutbuf_size,
  338. const uint8_t *buf, int buf_size)
  339. {
  340. H264Context *h = s->priv_data;
  341. ParseContext *pc = &h->parse_context;
  342. int next;
  343. if (!h->got_first) {
  344. h->got_first = 1;
  345. if (avctx->extradata_size) {
  346. h->avctx = avctx;
  347. // must be done like in decoder, otherwise opening the parser,
  348. // letting it create extradata and then closing and opening again
  349. // will cause has_b_frames to be always set.
  350. // Note that estimate_timings_from_pts does exactly this.
  351. if (!avctx->has_b_frames)
  352. h->low_delay = 1;
  353. ff_h264_decode_extradata(h, avctx->extradata, avctx->extradata_size);
  354. }
  355. }
  356. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  357. next = buf_size;
  358. } else {
  359. next = h264_find_frame_end(h, buf, buf_size);
  360. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  361. *poutbuf = NULL;
  362. *poutbuf_size = 0;
  363. return buf_size;
  364. }
  365. if (next < 0 && next != END_NOT_FOUND) {
  366. av_assert1(pc->last_index + next >= 0);
  367. h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next); // update state
  368. }
  369. }
  370. parse_nal_units(s, avctx, buf, buf_size);
  371. if (h->sei_cpb_removal_delay >= 0) {
  372. s->dts_sync_point = h->sei_buffering_period_present;
  373. s->dts_ref_dts_delta = h->sei_cpb_removal_delay;
  374. s->pts_dts_delta = h->sei_dpb_output_delay;
  375. } else {
  376. s->dts_sync_point = INT_MIN;
  377. s->dts_ref_dts_delta = INT_MIN;
  378. s->pts_dts_delta = INT_MIN;
  379. }
  380. if (s->flags & PARSER_FLAG_ONCE) {
  381. s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
  382. }
  383. *poutbuf = buf;
  384. *poutbuf_size = buf_size;
  385. return next;
  386. }
  387. static int h264_split(AVCodecContext *avctx,
  388. const uint8_t *buf, int buf_size)
  389. {
  390. int i;
  391. uint32_t state = -1;
  392. int has_sps = 0;
  393. for (i = 0; i <= buf_size; i++) {
  394. if ((state & 0xFFFFFF1F) == 0x107)
  395. has_sps = 1;
  396. /* if ((state&0xFFFFFF1F) == 0x101 ||
  397. * (state&0xFFFFFF1F) == 0x102 ||
  398. * (state&0xFFFFFF1F) == 0x105) {
  399. * }
  400. */
  401. if ((state & 0xFFFFFF00) == 0x100 && (state & 0xFFFFFF1F) != 0x107 &&
  402. (state & 0xFFFFFF1F) != 0x108 && (state & 0xFFFFFF1F) != 0x109) {
  403. if (has_sps) {
  404. while (i > 4 && buf[i - 5] == 0)
  405. i--;
  406. return i - 4;
  407. }
  408. }
  409. if (i < buf_size)
  410. state = (state << 8) | buf[i];
  411. }
  412. return 0;
  413. }
  414. static void close(AVCodecParserContext *s)
  415. {
  416. H264Context *h = s->priv_data;
  417. ParseContext *pc = &h->parse_context;
  418. av_free(pc->buffer);
  419. ff_h264_free_context(h);
  420. }
  421. static av_cold int init(AVCodecParserContext *s)
  422. {
  423. H264Context *h = s->priv_data;
  424. h->thread_context[0] = h;
  425. h->slice_context_count = 1;
  426. return 0;
  427. }
  428. AVCodecParser ff_h264_parser = {
  429. .codec_ids = { AV_CODEC_ID_H264 },
  430. .priv_data_size = sizeof(H264Context),
  431. .parser_init = init,
  432. .parser_parse = h264_parse,
  433. .parser_close = close,
  434. .split = h264_split,
  435. };