/libavcodec/h264_parser.c

http://github.com/FFmpeg/FFmpeg · C · 712 lines · 578 code · 77 blank · 57 comment · 226 complexity · ba218d4b72e90d48fb08b5d544c6c196 MD5 · raw file

  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 / MPEG-4 part10 parser.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #define UNCHECKED_BITSTREAM_READER 1
  27. #include <assert.h>
  28. #include <stdint.h>
  29. #include "libavutil/avutil.h"
  30. #include "libavutil/error.h"
  31. #include "libavutil/log.h"
  32. #include "libavutil/mem.h"
  33. #include "libavutil/pixfmt.h"
  34. #include "avcodec.h"
  35. #include "get_bits.h"
  36. #include "golomb.h"
  37. #include "h264.h"
  38. #include "h264_sei.h"
  39. #include "h264_ps.h"
  40. #include "h264data.h"
  41. #include "internal.h"
  42. #include "mpegutils.h"
  43. #include "parser.h"
  44. typedef struct H264ParseContext {
  45. ParseContext pc;
  46. H264ParamSets ps;
  47. H264DSPContext h264dsp;
  48. H264POCContext poc;
  49. H264SEIContext sei;
  50. int is_avc;
  51. int nal_length_size;
  52. int got_first;
  53. int picture_structure;
  54. uint8_t parse_history[6];
  55. int parse_history_count;
  56. int parse_last_mb;
  57. int64_t reference_dts;
  58. int last_frame_num, last_picture_structure;
  59. } H264ParseContext;
  60. static int h264_find_frame_end(H264ParseContext *p, const uint8_t *buf,
  61. int buf_size, void *logctx)
  62. {
  63. int i, j;
  64. uint32_t state;
  65. ParseContext *pc = &p->pc;
  66. int next_avc = p->is_avc ? 0 : buf_size;
  67. // mb_addr= pc->mb_addr - 1;
  68. state = pc->state;
  69. if (state > 13)
  70. state = 7;
  71. if (p->is_avc && !p->nal_length_size)
  72. av_log(logctx, AV_LOG_ERROR, "AVC-parser: nal length size invalid\n");
  73. for (i = 0; i < buf_size; i++) {
  74. if (i >= next_avc) {
  75. int nalsize = 0;
  76. i = next_avc;
  77. for (j = 0; j < p->nal_length_size; j++)
  78. nalsize = (nalsize << 8) | buf[i++];
  79. if (nalsize <= 0 || nalsize > buf_size - i) {
  80. av_log(logctx, AV_LOG_ERROR, "AVC-parser: nal size %d remaining %d\n", nalsize, buf_size - i);
  81. return buf_size;
  82. }
  83. next_avc = i + nalsize;
  84. state = 5;
  85. }
  86. if (state == 7) {
  87. i += p->h264dsp.startcode_find_candidate(buf + i, next_avc - i);
  88. if (i < next_avc)
  89. state = 2;
  90. } else if (state <= 2) {
  91. if (buf[i] == 1)
  92. state ^= 5; // 2->7, 1->4, 0->5
  93. else if (buf[i])
  94. state = 7;
  95. else
  96. state >>= 1; // 2->1, 1->0, 0->0
  97. } else if (state <= 5) {
  98. int nalu_type = buf[i] & 0x1F;
  99. if (nalu_type == H264_NAL_SEI || nalu_type == H264_NAL_SPS ||
  100. nalu_type == H264_NAL_PPS || nalu_type == H264_NAL_AUD) {
  101. if (pc->frame_start_found) {
  102. i++;
  103. goto found;
  104. }
  105. } else if (nalu_type == H264_NAL_SLICE || nalu_type == H264_NAL_DPA ||
  106. nalu_type == H264_NAL_IDR_SLICE) {
  107. state += 8;
  108. continue;
  109. }
  110. state = 7;
  111. } else {
  112. unsigned int mb, last_mb = p->parse_last_mb;
  113. GetBitContext gb;
  114. p->parse_history[p->parse_history_count++] = buf[i];
  115. init_get_bits(&gb, p->parse_history, 8*p->parse_history_count);
  116. mb= get_ue_golomb_long(&gb);
  117. if (get_bits_left(&gb) > 0 || p->parse_history_count > 5) {
  118. p->parse_last_mb = mb;
  119. if (pc->frame_start_found) {
  120. if (mb <= last_mb) {
  121. i -= p->parse_history_count - 1;
  122. p->parse_history_count = 0;
  123. goto found;
  124. }
  125. } else
  126. pc->frame_start_found = 1;
  127. p->parse_history_count = 0;
  128. state = 7;
  129. }
  130. }
  131. }
  132. pc->state = state;
  133. if (p->is_avc)
  134. return next_avc;
  135. return END_NOT_FOUND;
  136. found:
  137. pc->state = 7;
  138. pc->frame_start_found = 0;
  139. if (p->is_avc)
  140. return next_avc;
  141. return i - (state & 5);
  142. }
  143. static int scan_mmco_reset(AVCodecParserContext *s, GetBitContext *gb,
  144. void *logctx)
  145. {
  146. H264PredWeightTable pwt;
  147. int slice_type_nos = s->pict_type & 3;
  148. H264ParseContext *p = s->priv_data;
  149. int list_count, ref_count[2];
  150. if (p->ps.pps->redundant_pic_cnt_present)
  151. get_ue_golomb(gb); // redundant_pic_count
  152. if (slice_type_nos == AV_PICTURE_TYPE_B)
  153. get_bits1(gb); // direct_spatial_mv_pred
  154. if (ff_h264_parse_ref_count(&list_count, ref_count, gb, p->ps.pps,
  155. slice_type_nos, p->picture_structure, logctx) < 0)
  156. return AVERROR_INVALIDDATA;
  157. if (slice_type_nos != AV_PICTURE_TYPE_I) {
  158. int list;
  159. for (list = 0; list < list_count; list++) {
  160. if (get_bits1(gb)) {
  161. int index;
  162. for (index = 0; ; index++) {
  163. unsigned int reordering_of_pic_nums_idc = get_ue_golomb_31(gb);
  164. if (reordering_of_pic_nums_idc < 3)
  165. get_ue_golomb_long(gb);
  166. else if (reordering_of_pic_nums_idc > 3) {
  167. av_log(logctx, AV_LOG_ERROR,
  168. "illegal reordering_of_pic_nums_idc %d\n",
  169. reordering_of_pic_nums_idc);
  170. return AVERROR_INVALIDDATA;
  171. } else
  172. break;
  173. if (index >= ref_count[list]) {
  174. av_log(logctx, AV_LOG_ERROR,
  175. "reference count %d overflow\n", index);
  176. return AVERROR_INVALIDDATA;
  177. }
  178. }
  179. }
  180. }
  181. }
  182. if ((p->ps.pps->weighted_pred && slice_type_nos == AV_PICTURE_TYPE_P) ||
  183. (p->ps.pps->weighted_bipred_idc == 1 && slice_type_nos == AV_PICTURE_TYPE_B))
  184. ff_h264_pred_weight_table(gb, p->ps.sps, ref_count, slice_type_nos,
  185. &pwt, p->picture_structure, logctx);
  186. if (get_bits1(gb)) { // adaptive_ref_pic_marking_mode_flag
  187. int i;
  188. for (i = 0; i < MAX_MMCO_COUNT; i++) {
  189. MMCOOpcode opcode = get_ue_golomb_31(gb);
  190. if (opcode > (unsigned) MMCO_LONG) {
  191. av_log(logctx, AV_LOG_ERROR,
  192. "illegal memory management control operation %d\n",
  193. opcode);
  194. return AVERROR_INVALIDDATA;
  195. }
  196. if (opcode == MMCO_END)
  197. return 0;
  198. else if (opcode == MMCO_RESET)
  199. return 1;
  200. if (opcode == MMCO_SHORT2UNUSED || opcode == MMCO_SHORT2LONG)
  201. get_ue_golomb_long(gb); // difference_of_pic_nums_minus1
  202. if (opcode == MMCO_SHORT2LONG || opcode == MMCO_LONG2UNUSED ||
  203. opcode == MMCO_LONG || opcode == MMCO_SET_MAX_LONG)
  204. get_ue_golomb_31(gb);
  205. }
  206. }
  207. return 0;
  208. }
  209. /**
  210. * Parse NAL units of found picture and decode some basic information.
  211. *
  212. * @param s parser context.
  213. * @param avctx codec context.
  214. * @param buf buffer with field/frame data.
  215. * @param buf_size size of the buffer.
  216. */
  217. static inline int parse_nal_units(AVCodecParserContext *s,
  218. AVCodecContext *avctx,
  219. const uint8_t * const buf, int buf_size)
  220. {
  221. H264ParseContext *p = s->priv_data;
  222. H2645RBSP rbsp = { NULL };
  223. H2645NAL nal = { NULL };
  224. int buf_index, next_avc;
  225. unsigned int pps_id;
  226. unsigned int slice_type;
  227. int state = -1, got_reset = 0;
  228. int q264 = buf_size >=4 && !memcmp("Q264", buf, 4);
  229. int field_poc[2];
  230. int ret;
  231. /* set some sane default values */
  232. s->pict_type = AV_PICTURE_TYPE_I;
  233. s->key_frame = 0;
  234. s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
  235. ff_h264_sei_uninit(&p->sei);
  236. p->sei.frame_packing.arrangement_cancel_flag = -1;
  237. if (!buf_size)
  238. return 0;
  239. av_fast_padded_malloc(&rbsp.rbsp_buffer, &rbsp.rbsp_buffer_alloc_size, buf_size);
  240. if (!rbsp.rbsp_buffer)
  241. return AVERROR(ENOMEM);
  242. buf_index = 0;
  243. next_avc = p->is_avc ? 0 : buf_size;
  244. for (;;) {
  245. const SPS *sps;
  246. int src_length, consumed, nalsize = 0;
  247. if (buf_index >= next_avc) {
  248. nalsize = get_nalsize(p->nal_length_size, buf, buf_size, &buf_index, avctx);
  249. if (nalsize < 0)
  250. break;
  251. next_avc = buf_index + nalsize;
  252. } else {
  253. buf_index = find_start_code(buf, buf_size, buf_index, next_avc);
  254. if (buf_index >= buf_size)
  255. break;
  256. if (buf_index >= next_avc)
  257. continue;
  258. }
  259. src_length = next_avc - buf_index;
  260. state = buf[buf_index];
  261. switch (state & 0x1f) {
  262. case H264_NAL_SLICE:
  263. case H264_NAL_IDR_SLICE:
  264. // Do not walk the whole buffer just to decode slice header
  265. if ((state & 0x1f) == H264_NAL_IDR_SLICE || ((state >> 5) & 0x3) == 0) {
  266. /* IDR or disposable slice
  267. * No need to decode many bytes because MMCOs shall not be present. */
  268. if (src_length > 60)
  269. src_length = 60;
  270. } else {
  271. /* To decode up to MMCOs */
  272. if (src_length > 1000)
  273. src_length = 1000;
  274. }
  275. break;
  276. }
  277. consumed = ff_h2645_extract_rbsp(buf + buf_index, src_length, &rbsp, &nal, 1);
  278. if (consumed < 0)
  279. break;
  280. buf_index += consumed;
  281. ret = init_get_bits8(&nal.gb, nal.data, nal.size);
  282. if (ret < 0)
  283. goto fail;
  284. get_bits1(&nal.gb);
  285. nal.ref_idc = get_bits(&nal.gb, 2);
  286. nal.type = get_bits(&nal.gb, 5);
  287. switch (nal.type) {
  288. case H264_NAL_SPS:
  289. ff_h264_decode_seq_parameter_set(&nal.gb, avctx, &p->ps, 0);
  290. break;
  291. case H264_NAL_PPS:
  292. ff_h264_decode_picture_parameter_set(&nal.gb, avctx, &p->ps,
  293. nal.size_bits);
  294. break;
  295. case H264_NAL_SEI:
  296. ff_h264_sei_decode(&p->sei, &nal.gb, &p->ps, avctx);
  297. break;
  298. case H264_NAL_IDR_SLICE:
  299. s->key_frame = 1;
  300. p->poc.prev_frame_num = 0;
  301. p->poc.prev_frame_num_offset = 0;
  302. p->poc.prev_poc_msb =
  303. p->poc.prev_poc_lsb = 0;
  304. /* fall through */
  305. case H264_NAL_SLICE:
  306. get_ue_golomb_long(&nal.gb); // skip first_mb_in_slice
  307. slice_type = get_ue_golomb_31(&nal.gb);
  308. s->pict_type = ff_h264_golomb_to_pict_type[slice_type % 5];
  309. if (p->sei.recovery_point.recovery_frame_cnt >= 0) {
  310. /* key frame, since recovery_frame_cnt is set */
  311. s->key_frame = 1;
  312. }
  313. pps_id = get_ue_golomb(&nal.gb);
  314. if (pps_id >= MAX_PPS_COUNT) {
  315. av_log(avctx, AV_LOG_ERROR,
  316. "pps_id %u out of range\n", pps_id);
  317. goto fail;
  318. }
  319. if (!p->ps.pps_list[pps_id]) {
  320. av_log(avctx, AV_LOG_ERROR,
  321. "non-existing PPS %u referenced\n", pps_id);
  322. goto fail;
  323. }
  324. av_buffer_unref(&p->ps.pps_ref);
  325. p->ps.pps = NULL;
  326. p->ps.sps = NULL;
  327. p->ps.pps_ref = av_buffer_ref(p->ps.pps_list[pps_id]);
  328. if (!p->ps.pps_ref)
  329. goto fail;
  330. p->ps.pps = (const PPS*)p->ps.pps_ref->data;
  331. p->ps.sps = p->ps.pps->sps;
  332. sps = p->ps.sps;
  333. // heuristic to detect non marked keyframes
  334. if (p->ps.sps->ref_frame_count <= 1 && p->ps.pps->ref_count[0] <= 1 && s->pict_type == AV_PICTURE_TYPE_I)
  335. s->key_frame = 1;
  336. p->poc.frame_num = get_bits(&nal.gb, sps->log2_max_frame_num);
  337. s->coded_width = 16 * sps->mb_width;
  338. s->coded_height = 16 * sps->mb_height;
  339. s->width = s->coded_width - (sps->crop_right + sps->crop_left);
  340. s->height = s->coded_height - (sps->crop_top + sps->crop_bottom);
  341. if (s->width <= 0 || s->height <= 0) {
  342. s->width = s->coded_width;
  343. s->height = s->coded_height;
  344. }
  345. switch (sps->bit_depth_luma) {
  346. case 9:
  347. if (sps->chroma_format_idc == 3) s->format = AV_PIX_FMT_YUV444P9;
  348. else if (sps->chroma_format_idc == 2) s->format = AV_PIX_FMT_YUV422P9;
  349. else s->format = AV_PIX_FMT_YUV420P9;
  350. break;
  351. case 10:
  352. if (sps->chroma_format_idc == 3) s->format = AV_PIX_FMT_YUV444P10;
  353. else if (sps->chroma_format_idc == 2) s->format = AV_PIX_FMT_YUV422P10;
  354. else s->format = AV_PIX_FMT_YUV420P10;
  355. break;
  356. case 8:
  357. if (sps->chroma_format_idc == 3) s->format = AV_PIX_FMT_YUV444P;
  358. else if (sps->chroma_format_idc == 2) s->format = AV_PIX_FMT_YUV422P;
  359. else s->format = AV_PIX_FMT_YUV420P;
  360. break;
  361. default:
  362. s->format = AV_PIX_FMT_NONE;
  363. }
  364. avctx->profile = ff_h264_get_profile(sps);
  365. avctx->level = sps->level_idc;
  366. if (sps->frame_mbs_only_flag) {
  367. p->picture_structure = PICT_FRAME;
  368. } else {
  369. if (get_bits1(&nal.gb)) { // field_pic_flag
  370. p->picture_structure = PICT_TOP_FIELD + get_bits1(&nal.gb); // bottom_field_flag
  371. } else {
  372. p->picture_structure = PICT_FRAME;
  373. }
  374. }
  375. if (nal.type == H264_NAL_IDR_SLICE)
  376. get_ue_golomb_long(&nal.gb); /* idr_pic_id */
  377. if (sps->poc_type == 0) {
  378. p->poc.poc_lsb = get_bits(&nal.gb, sps->log2_max_poc_lsb);
  379. if (p->ps.pps->pic_order_present == 1 &&
  380. p->picture_structure == PICT_FRAME)
  381. p->poc.delta_poc_bottom = get_se_golomb(&nal.gb);
  382. }
  383. if (sps->poc_type == 1 &&
  384. !sps->delta_pic_order_always_zero_flag) {
  385. p->poc.delta_poc[0] = get_se_golomb(&nal.gb);
  386. if (p->ps.pps->pic_order_present == 1 &&
  387. p->picture_structure == PICT_FRAME)
  388. p->poc.delta_poc[1] = get_se_golomb(&nal.gb);
  389. }
  390. /* Decode POC of this picture.
  391. * The prev_ values needed for decoding POC of the next picture are not set here. */
  392. field_poc[0] = field_poc[1] = INT_MAX;
  393. ret = ff_h264_init_poc(field_poc, &s->output_picture_number, sps,
  394. &p->poc, p->picture_structure, nal.ref_idc);
  395. if (ret < 0)
  396. goto fail;
  397. /* Continue parsing to check if MMCO_RESET is present.
  398. * FIXME: MMCO_RESET could appear in non-first slice.
  399. * Maybe, we should parse all undisposable non-IDR slice of this
  400. * picture until encountering MMCO_RESET in a slice of it. */
  401. if (nal.ref_idc && nal.type != H264_NAL_IDR_SLICE) {
  402. got_reset = scan_mmco_reset(s, &nal.gb, avctx);
  403. if (got_reset < 0)
  404. goto fail;
  405. }
  406. /* Set up the prev_ values for decoding POC of the next picture. */
  407. p->poc.prev_frame_num = got_reset ? 0 : p->poc.frame_num;
  408. p->poc.prev_frame_num_offset = got_reset ? 0 : p->poc.frame_num_offset;
  409. if (nal.ref_idc != 0) {
  410. if (!got_reset) {
  411. p->poc.prev_poc_msb = p->poc.poc_msb;
  412. p->poc.prev_poc_lsb = p->poc.poc_lsb;
  413. } else {
  414. p->poc.prev_poc_msb = 0;
  415. p->poc.prev_poc_lsb =
  416. p->picture_structure == PICT_BOTTOM_FIELD ? 0 : field_poc[0];
  417. }
  418. }
  419. if (p->sei.picture_timing.present) {
  420. ret = ff_h264_sei_process_picture_timing(&p->sei.picture_timing,
  421. sps, avctx);
  422. if (ret < 0) {
  423. av_log(avctx, AV_LOG_ERROR, "Error processing the picture timing SEI\n");
  424. p->sei.picture_timing.present = 0;
  425. }
  426. }
  427. if (sps->pic_struct_present_flag && p->sei.picture_timing.present) {
  428. switch (p->sei.picture_timing.pic_struct) {
  429. case H264_SEI_PIC_STRUCT_TOP_FIELD:
  430. case H264_SEI_PIC_STRUCT_BOTTOM_FIELD:
  431. s->repeat_pict = 0;
  432. break;
  433. case H264_SEI_PIC_STRUCT_FRAME:
  434. case H264_SEI_PIC_STRUCT_TOP_BOTTOM:
  435. case H264_SEI_PIC_STRUCT_BOTTOM_TOP:
  436. s->repeat_pict = 1;
  437. break;
  438. case H264_SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  439. case H264_SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  440. s->repeat_pict = 2;
  441. break;
  442. case H264_SEI_PIC_STRUCT_FRAME_DOUBLING:
  443. s->repeat_pict = 3;
  444. break;
  445. case H264_SEI_PIC_STRUCT_FRAME_TRIPLING:
  446. s->repeat_pict = 5;
  447. break;
  448. default:
  449. s->repeat_pict = p->picture_structure == PICT_FRAME ? 1 : 0;
  450. break;
  451. }
  452. } else {
  453. s->repeat_pict = p->picture_structure == PICT_FRAME ? 1 : 0;
  454. }
  455. if (p->picture_structure == PICT_FRAME) {
  456. s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
  457. if (sps->pic_struct_present_flag && p->sei.picture_timing.present) {
  458. switch (p->sei.picture_timing.pic_struct) {
  459. case H264_SEI_PIC_STRUCT_TOP_BOTTOM:
  460. case H264_SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  461. s->field_order = AV_FIELD_TT;
  462. break;
  463. case H264_SEI_PIC_STRUCT_BOTTOM_TOP:
  464. case H264_SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  465. s->field_order = AV_FIELD_BB;
  466. break;
  467. default:
  468. s->field_order = AV_FIELD_PROGRESSIVE;
  469. break;
  470. }
  471. } else {
  472. if (field_poc[0] < field_poc[1])
  473. s->field_order = AV_FIELD_TT;
  474. else if (field_poc[0] > field_poc[1])
  475. s->field_order = AV_FIELD_BB;
  476. else
  477. s->field_order = AV_FIELD_PROGRESSIVE;
  478. }
  479. } else {
  480. if (p->picture_structure == PICT_TOP_FIELD)
  481. s->picture_structure = AV_PICTURE_STRUCTURE_TOP_FIELD;
  482. else
  483. s->picture_structure = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
  484. if (p->poc.frame_num == p->last_frame_num &&
  485. p->last_picture_structure != AV_PICTURE_STRUCTURE_UNKNOWN &&
  486. p->last_picture_structure != AV_PICTURE_STRUCTURE_FRAME &&
  487. p->last_picture_structure != s->picture_structure) {
  488. if (p->last_picture_structure == AV_PICTURE_STRUCTURE_TOP_FIELD)
  489. s->field_order = AV_FIELD_TT;
  490. else
  491. s->field_order = AV_FIELD_BB;
  492. } else {
  493. s->field_order = AV_FIELD_UNKNOWN;
  494. }
  495. p->last_picture_structure = s->picture_structure;
  496. p->last_frame_num = p->poc.frame_num;
  497. }
  498. av_freep(&rbsp.rbsp_buffer);
  499. return 0; /* no need to evaluate the rest */
  500. }
  501. }
  502. if (q264) {
  503. av_freep(&rbsp.rbsp_buffer);
  504. return 0;
  505. }
  506. /* didn't find a picture! */
  507. av_log(avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
  508. fail:
  509. av_freep(&rbsp.rbsp_buffer);
  510. return -1;
  511. }
  512. static int h264_parse(AVCodecParserContext *s,
  513. AVCodecContext *avctx,
  514. const uint8_t **poutbuf, int *poutbuf_size,
  515. const uint8_t *buf, int buf_size)
  516. {
  517. H264ParseContext *p = s->priv_data;
  518. ParseContext *pc = &p->pc;
  519. int next;
  520. if (!p->got_first) {
  521. p->got_first = 1;
  522. if (avctx->extradata_size) {
  523. ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
  524. &p->ps, &p->is_avc, &p->nal_length_size,
  525. avctx->err_recognition, avctx);
  526. }
  527. }
  528. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  529. next = buf_size;
  530. } else {
  531. next = h264_find_frame_end(p, buf, buf_size, avctx);
  532. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  533. *poutbuf = NULL;
  534. *poutbuf_size = 0;
  535. return buf_size;
  536. }
  537. if (next < 0 && next != END_NOT_FOUND) {
  538. av_assert1(pc->last_index + next >= 0);
  539. h264_find_frame_end(p, &pc->buffer[pc->last_index + next], -next, avctx); // update state
  540. }
  541. }
  542. parse_nal_units(s, avctx, buf, buf_size);
  543. if (avctx->framerate.num)
  544. avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
  545. if (p->sei.picture_timing.cpb_removal_delay >= 0) {
  546. s->dts_sync_point = p->sei.buffering_period.present;
  547. s->dts_ref_dts_delta = p->sei.picture_timing.cpb_removal_delay;
  548. s->pts_dts_delta = p->sei.picture_timing.dpb_output_delay;
  549. } else {
  550. s->dts_sync_point = INT_MIN;
  551. s->dts_ref_dts_delta = INT_MIN;
  552. s->pts_dts_delta = INT_MIN;
  553. }
  554. if (s->flags & PARSER_FLAG_ONCE) {
  555. s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
  556. }
  557. if (s->dts_sync_point >= 0) {
  558. int64_t den = avctx->time_base.den * (int64_t)avctx->pkt_timebase.num;
  559. if (den > 0) {
  560. int64_t num = avctx->time_base.num * (int64_t)avctx->pkt_timebase.den;
  561. if (s->dts != AV_NOPTS_VALUE) {
  562. // got DTS from the stream, update reference timestamp
  563. p->reference_dts = s->dts - av_rescale(s->dts_ref_dts_delta, num, den);
  564. } else if (p->reference_dts != AV_NOPTS_VALUE) {
  565. // compute DTS based on reference timestamp
  566. s->dts = p->reference_dts + av_rescale(s->dts_ref_dts_delta, num, den);
  567. }
  568. if (p->reference_dts != AV_NOPTS_VALUE && s->pts == AV_NOPTS_VALUE)
  569. s->pts = s->dts + av_rescale(s->pts_dts_delta, num, den);
  570. if (s->dts_sync_point > 0)
  571. p->reference_dts = s->dts; // new reference
  572. }
  573. }
  574. *poutbuf = buf;
  575. *poutbuf_size = buf_size;
  576. return next;
  577. }
  578. static int h264_split(AVCodecContext *avctx,
  579. const uint8_t *buf, int buf_size)
  580. {
  581. uint32_t state = -1;
  582. int has_sps = 0;
  583. int has_pps = 0;
  584. const uint8_t *ptr = buf, *end = buf + buf_size;
  585. int nalu_type;
  586. while (ptr < end) {
  587. ptr = avpriv_find_start_code(ptr, end, &state);
  588. if ((state & 0xFFFFFF00) != 0x100)
  589. break;
  590. nalu_type = state & 0x1F;
  591. if (nalu_type == H264_NAL_SPS) {
  592. has_sps = 1;
  593. } else if (nalu_type == H264_NAL_PPS)
  594. has_pps = 1;
  595. /* else if (nalu_type == 0x01 ||
  596. * nalu_type == 0x02 ||
  597. * nalu_type == 0x05) {
  598. * }
  599. */
  600. else if ((nalu_type != H264_NAL_SEI || has_pps) &&
  601. nalu_type != H264_NAL_AUD && nalu_type != H264_NAL_SPS_EXT &&
  602. nalu_type != 0x0f) {
  603. if (has_sps) {
  604. while (ptr - 4 > buf && ptr[-5] == 0)
  605. ptr--;
  606. return ptr - 4 - buf;
  607. }
  608. }
  609. }
  610. return 0;
  611. }
  612. static void h264_close(AVCodecParserContext *s)
  613. {
  614. H264ParseContext *p = s->priv_data;
  615. ParseContext *pc = &p->pc;
  616. av_freep(&pc->buffer);
  617. ff_h264_sei_uninit(&p->sei);
  618. ff_h264_ps_uninit(&p->ps);
  619. }
  620. static av_cold int init(AVCodecParserContext *s)
  621. {
  622. H264ParseContext *p = s->priv_data;
  623. p->reference_dts = AV_NOPTS_VALUE;
  624. p->last_frame_num = INT_MAX;
  625. ff_h264dsp_init(&p->h264dsp, 8, 1);
  626. return 0;
  627. }
  628. AVCodecParser ff_h264_parser = {
  629. .codec_ids = { AV_CODEC_ID_H264 },
  630. .priv_data_size = sizeof(H264ParseContext),
  631. .parser_init = init,
  632. .parser_parse = h264_parse,
  633. .parser_close = h264_close,
  634. .split = h264_split,
  635. };