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.

716 lines
25KB

  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. av_buffer_unref(&p->ps.sps_ref);
  326. p->ps.pps = NULL;
  327. p->ps.sps = NULL;
  328. p->ps.pps_ref = av_buffer_ref(p->ps.pps_list[pps_id]);
  329. if (!p->ps.pps_ref)
  330. goto fail;
  331. p->ps.pps = (const PPS*)p->ps.pps_ref->data;
  332. if (!p->ps.sps_list[p->ps.pps->sps_id]) {
  333. av_log(avctx, AV_LOG_ERROR,
  334. "non-existing SPS %u referenced\n", p->ps.pps->sps_id);
  335. goto fail;
  336. }
  337. p->ps.sps_ref = av_buffer_ref(p->ps.sps_list[p->ps.pps->sps_id]);
  338. if (!p->ps.sps_ref)
  339. goto fail;
  340. p->ps.sps = (const SPS*)p->ps.sps_ref->data;
  341. sps = p->ps.sps;
  342. // heuristic to detect non marked keyframes
  343. if (p->ps.sps->ref_frame_count <= 1 && p->ps.pps->ref_count[0] <= 1 && s->pict_type == AV_PICTURE_TYPE_I)
  344. s->key_frame = 1;
  345. p->poc.frame_num = get_bits(&nal.gb, sps->log2_max_frame_num);
  346. s->coded_width = 16 * sps->mb_width;
  347. s->coded_height = 16 * sps->mb_height;
  348. s->width = s->coded_width - (sps->crop_right + sps->crop_left);
  349. s->height = s->coded_height - (sps->crop_top + sps->crop_bottom);
  350. if (s->width <= 0 || s->height <= 0) {
  351. s->width = s->coded_width;
  352. s->height = s->coded_height;
  353. }
  354. switch (sps->bit_depth_luma) {
  355. case 9:
  356. if (sps->chroma_format_idc == 3) s->format = AV_PIX_FMT_YUV444P9;
  357. else if (sps->chroma_format_idc == 2) s->format = AV_PIX_FMT_YUV422P9;
  358. else s->format = AV_PIX_FMT_YUV420P9;
  359. break;
  360. case 10:
  361. if (sps->chroma_format_idc == 3) s->format = AV_PIX_FMT_YUV444P10;
  362. else if (sps->chroma_format_idc == 2) s->format = AV_PIX_FMT_YUV422P10;
  363. else s->format = AV_PIX_FMT_YUV420P10;
  364. break;
  365. case 8:
  366. if (sps->chroma_format_idc == 3) s->format = AV_PIX_FMT_YUV444P;
  367. else if (sps->chroma_format_idc == 2) s->format = AV_PIX_FMT_YUV422P;
  368. else s->format = AV_PIX_FMT_YUV420P;
  369. break;
  370. default:
  371. s->format = AV_PIX_FMT_NONE;
  372. }
  373. avctx->profile = ff_h264_get_profile(sps);
  374. avctx->level = sps->level_idc;
  375. if (sps->frame_mbs_only_flag) {
  376. p->picture_structure = PICT_FRAME;
  377. } else {
  378. if (get_bits1(&nal.gb)) { // field_pic_flag
  379. p->picture_structure = PICT_TOP_FIELD + get_bits1(&nal.gb); // bottom_field_flag
  380. } else {
  381. p->picture_structure = PICT_FRAME;
  382. }
  383. }
  384. if (nal.type == H264_NAL_IDR_SLICE)
  385. get_ue_golomb_long(&nal.gb); /* idr_pic_id */
  386. if (sps->poc_type == 0) {
  387. p->poc.poc_lsb = get_bits(&nal.gb, sps->log2_max_poc_lsb);
  388. if (p->ps.pps->pic_order_present == 1 &&
  389. p->picture_structure == PICT_FRAME)
  390. p->poc.delta_poc_bottom = get_se_golomb(&nal.gb);
  391. }
  392. if (sps->poc_type == 1 &&
  393. !sps->delta_pic_order_always_zero_flag) {
  394. p->poc.delta_poc[0] = get_se_golomb(&nal.gb);
  395. if (p->ps.pps->pic_order_present == 1 &&
  396. p->picture_structure == PICT_FRAME)
  397. p->poc.delta_poc[1] = get_se_golomb(&nal.gb);
  398. }
  399. /* Decode POC of this picture.
  400. * The prev_ values needed for decoding POC of the next picture are not set here. */
  401. field_poc[0] = field_poc[1] = INT_MAX;
  402. ret = ff_h264_init_poc(field_poc, &s->output_picture_number, sps,
  403. &p->poc, p->picture_structure, nal.ref_idc);
  404. if (ret < 0)
  405. goto fail;
  406. /* Continue parsing to check if MMCO_RESET is present.
  407. * FIXME: MMCO_RESET could appear in non-first slice.
  408. * Maybe, we should parse all undisposable non-IDR slice of this
  409. * picture until encountering MMCO_RESET in a slice of it. */
  410. if (nal.ref_idc && nal.type != H264_NAL_IDR_SLICE) {
  411. got_reset = scan_mmco_reset(s, &nal.gb, avctx);
  412. if (got_reset < 0)
  413. goto fail;
  414. }
  415. /* Set up the prev_ values for decoding POC of the next picture. */
  416. p->poc.prev_frame_num = got_reset ? 0 : p->poc.frame_num;
  417. p->poc.prev_frame_num_offset = got_reset ? 0 : p->poc.frame_num_offset;
  418. if (nal.ref_idc != 0) {
  419. if (!got_reset) {
  420. p->poc.prev_poc_msb = p->poc.poc_msb;
  421. p->poc.prev_poc_lsb = p->poc.poc_lsb;
  422. } else {
  423. p->poc.prev_poc_msb = 0;
  424. p->poc.prev_poc_lsb =
  425. p->picture_structure == PICT_BOTTOM_FIELD ? 0 : field_poc[0];
  426. }
  427. }
  428. if (sps->pic_struct_present_flag && p->sei.picture_timing.present) {
  429. switch (p->sei.picture_timing.pic_struct) {
  430. case H264_SEI_PIC_STRUCT_TOP_FIELD:
  431. case H264_SEI_PIC_STRUCT_BOTTOM_FIELD:
  432. s->repeat_pict = 0;
  433. break;
  434. case H264_SEI_PIC_STRUCT_FRAME:
  435. case H264_SEI_PIC_STRUCT_TOP_BOTTOM:
  436. case H264_SEI_PIC_STRUCT_BOTTOM_TOP:
  437. s->repeat_pict = 1;
  438. break;
  439. case H264_SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  440. case H264_SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  441. s->repeat_pict = 2;
  442. break;
  443. case H264_SEI_PIC_STRUCT_FRAME_DOUBLING:
  444. s->repeat_pict = 3;
  445. break;
  446. case H264_SEI_PIC_STRUCT_FRAME_TRIPLING:
  447. s->repeat_pict = 5;
  448. break;
  449. default:
  450. s->repeat_pict = p->picture_structure == PICT_FRAME ? 1 : 0;
  451. break;
  452. }
  453. } else {
  454. s->repeat_pict = p->picture_structure == PICT_FRAME ? 1 : 0;
  455. }
  456. if (p->picture_structure == PICT_FRAME) {
  457. s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
  458. if (sps->pic_struct_present_flag && p->sei.picture_timing.present) {
  459. switch (p->sei.picture_timing.pic_struct) {
  460. case H264_SEI_PIC_STRUCT_TOP_BOTTOM:
  461. case H264_SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  462. s->field_order = AV_FIELD_TT;
  463. break;
  464. case H264_SEI_PIC_STRUCT_BOTTOM_TOP:
  465. case H264_SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  466. s->field_order = AV_FIELD_BB;
  467. break;
  468. default:
  469. s->field_order = AV_FIELD_PROGRESSIVE;
  470. break;
  471. }
  472. } else {
  473. if (field_poc[0] < field_poc[1])
  474. s->field_order = AV_FIELD_TT;
  475. else if (field_poc[0] > field_poc[1])
  476. s->field_order = AV_FIELD_BB;
  477. else
  478. s->field_order = AV_FIELD_PROGRESSIVE;
  479. }
  480. } else {
  481. if (p->picture_structure == PICT_TOP_FIELD)
  482. s->picture_structure = AV_PICTURE_STRUCTURE_TOP_FIELD;
  483. else
  484. s->picture_structure = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
  485. if (p->poc.frame_num == p->last_frame_num &&
  486. p->last_picture_structure != AV_PICTURE_STRUCTURE_UNKNOWN &&
  487. p->last_picture_structure != AV_PICTURE_STRUCTURE_FRAME &&
  488. p->last_picture_structure != s->picture_structure) {
  489. if (p->last_picture_structure == AV_PICTURE_STRUCTURE_TOP_FIELD)
  490. s->field_order = AV_FIELD_TT;
  491. else
  492. s->field_order = AV_FIELD_BB;
  493. } else {
  494. s->field_order = AV_FIELD_UNKNOWN;
  495. }
  496. p->last_picture_structure = s->picture_structure;
  497. p->last_frame_num = p->poc.frame_num;
  498. }
  499. av_freep(&rbsp.rbsp_buffer);
  500. return 0; /* no need to evaluate the rest */
  501. }
  502. }
  503. if (q264) {
  504. av_freep(&rbsp.rbsp_buffer);
  505. return 0;
  506. }
  507. /* didn't find a picture! */
  508. av_log(avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
  509. fail:
  510. av_freep(&rbsp.rbsp_buffer);
  511. return -1;
  512. }
  513. static int h264_parse(AVCodecParserContext *s,
  514. AVCodecContext *avctx,
  515. const uint8_t **poutbuf, int *poutbuf_size,
  516. const uint8_t *buf, int buf_size)
  517. {
  518. H264ParseContext *p = s->priv_data;
  519. ParseContext *pc = &p->pc;
  520. int next;
  521. if (!p->got_first) {
  522. p->got_first = 1;
  523. if (avctx->extradata_size) {
  524. ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
  525. &p->ps, &p->is_avc, &p->nal_length_size,
  526. avctx->err_recognition, avctx);
  527. }
  528. }
  529. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  530. next = buf_size;
  531. } else {
  532. next = h264_find_frame_end(p, buf, buf_size, avctx);
  533. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  534. *poutbuf = NULL;
  535. *poutbuf_size = 0;
  536. return buf_size;
  537. }
  538. if (next < 0 && next != END_NOT_FOUND) {
  539. av_assert1(pc->last_index + next >= 0);
  540. h264_find_frame_end(p, &pc->buffer[pc->last_index + next], -next, avctx); // update state
  541. }
  542. }
  543. parse_nal_units(s, avctx, buf, buf_size);
  544. if (avctx->framerate.num)
  545. avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
  546. if (p->sei.picture_timing.cpb_removal_delay >= 0) {
  547. s->dts_sync_point = p->sei.buffering_period.present;
  548. s->dts_ref_dts_delta = p->sei.picture_timing.cpb_removal_delay;
  549. s->pts_dts_delta = p->sei.picture_timing.dpb_output_delay;
  550. } else {
  551. s->dts_sync_point = INT_MIN;
  552. s->dts_ref_dts_delta = INT_MIN;
  553. s->pts_dts_delta = INT_MIN;
  554. }
  555. if (s->flags & PARSER_FLAG_ONCE) {
  556. s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
  557. }
  558. if (s->dts_sync_point >= 0) {
  559. int64_t den = avctx->time_base.den * (int64_t)avctx->pkt_timebase.num;
  560. if (den > 0) {
  561. int64_t num = avctx->time_base.num * (int64_t)avctx->pkt_timebase.den;
  562. if (s->dts != AV_NOPTS_VALUE) {
  563. // got DTS from the stream, update reference timestamp
  564. p->reference_dts = s->dts - av_rescale(s->dts_ref_dts_delta, num, den);
  565. } else if (p->reference_dts != AV_NOPTS_VALUE) {
  566. // compute DTS based on reference timestamp
  567. s->dts = p->reference_dts + av_rescale(s->dts_ref_dts_delta, num, den);
  568. }
  569. if (p->reference_dts != AV_NOPTS_VALUE && s->pts == AV_NOPTS_VALUE)
  570. s->pts = s->dts + av_rescale(s->pts_dts_delta, num, den);
  571. if (s->dts_sync_point > 0)
  572. p->reference_dts = s->dts; // new reference
  573. }
  574. }
  575. *poutbuf = buf;
  576. *poutbuf_size = buf_size;
  577. return next;
  578. }
  579. static int h264_split(AVCodecContext *avctx,
  580. const uint8_t *buf, int buf_size)
  581. {
  582. uint32_t state = -1;
  583. int has_sps = 0;
  584. int has_pps = 0;
  585. const uint8_t *ptr = buf, *end = buf + buf_size;
  586. int nalu_type;
  587. while (ptr < end) {
  588. ptr = avpriv_find_start_code(ptr, end, &state);
  589. if ((state & 0xFFFFFF00) != 0x100)
  590. break;
  591. nalu_type = state & 0x1F;
  592. if (nalu_type == H264_NAL_SPS) {
  593. has_sps = 1;
  594. } else if (nalu_type == H264_NAL_PPS)
  595. has_pps = 1;
  596. /* else if (nalu_type == 0x01 ||
  597. * nalu_type == 0x02 ||
  598. * nalu_type == 0x05) {
  599. * }
  600. */
  601. else if ((nalu_type != H264_NAL_SEI || has_pps) &&
  602. nalu_type != H264_NAL_AUD && nalu_type != H264_NAL_SPS_EXT &&
  603. nalu_type != 0x0f) {
  604. if (has_sps) {
  605. while (ptr - 4 > buf && ptr[-5] == 0)
  606. ptr--;
  607. return ptr - 4 - buf;
  608. }
  609. }
  610. }
  611. return 0;
  612. }
  613. static void h264_close(AVCodecParserContext *s)
  614. {
  615. H264ParseContext *p = s->priv_data;
  616. ParseContext *pc = &p->pc;
  617. av_freep(&pc->buffer);
  618. ff_h264_sei_uninit(&p->sei);
  619. ff_h264_ps_uninit(&p->ps);
  620. }
  621. static av_cold int init(AVCodecParserContext *s)
  622. {
  623. H264ParseContext *p = s->priv_data;
  624. p->reference_dts = AV_NOPTS_VALUE;
  625. p->last_frame_num = INT_MAX;
  626. ff_h264dsp_init(&p->h264dsp, 8, 1);
  627. return 0;
  628. }
  629. AVCodecParser ff_h264_parser = {
  630. .codec_ids = { AV_CODEC_ID_H264 },
  631. .priv_data_size = sizeof(H264ParseContext),
  632. .parser_init = init,
  633. .parser_parse = h264_parse,
  634. .parser_close = h264_close,
  635. .split = h264_split,
  636. };