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.

709 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. H2645NAL nal = { NULL };
  223. int buf_index, next_avc;
  224. unsigned int pps_id;
  225. unsigned int slice_type;
  226. int state = -1, got_reset = 0;
  227. int q264 = buf_size >=4 && !memcmp("Q264", buf, 4);
  228. int field_poc[2];
  229. int ret;
  230. /* set some sane default values */
  231. s->pict_type = AV_PICTURE_TYPE_I;
  232. s->key_frame = 0;
  233. s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
  234. ff_h264_sei_uninit(&p->sei);
  235. p->sei.frame_packing.frame_packing_arrangement_cancel_flag = -1;
  236. if (!buf_size)
  237. return 0;
  238. buf_index = 0;
  239. next_avc = p->is_avc ? 0 : buf_size;
  240. for (;;) {
  241. const SPS *sps;
  242. int src_length, consumed, nalsize = 0;
  243. if (buf_index >= next_avc) {
  244. nalsize = get_nalsize(p->nal_length_size, buf, buf_size, &buf_index, avctx);
  245. if (nalsize < 0)
  246. break;
  247. next_avc = buf_index + nalsize;
  248. } else {
  249. buf_index = find_start_code(buf, buf_size, buf_index, next_avc);
  250. if (buf_index >= buf_size)
  251. break;
  252. if (buf_index >= next_avc)
  253. continue;
  254. }
  255. src_length = next_avc - buf_index;
  256. state = buf[buf_index];
  257. switch (state & 0x1f) {
  258. case H264_NAL_SLICE:
  259. case H264_NAL_IDR_SLICE:
  260. // Do not walk the whole buffer just to decode slice header
  261. if ((state & 0x1f) == H264_NAL_IDR_SLICE || ((state >> 5) & 0x3) == 0) {
  262. /* IDR or disposable slice
  263. * No need to decode many bytes because MMCOs shall not be present. */
  264. if (src_length > 60)
  265. src_length = 60;
  266. } else {
  267. /* To decode up to MMCOs */
  268. if (src_length > 1000)
  269. src_length = 1000;
  270. }
  271. break;
  272. }
  273. consumed = ff_h2645_extract_rbsp(buf + buf_index, src_length, &nal, 1);
  274. if (consumed < 0)
  275. break;
  276. buf_index += consumed;
  277. ret = init_get_bits8(&nal.gb, nal.data, nal.size);
  278. if (ret < 0)
  279. goto fail;
  280. get_bits1(&nal.gb);
  281. nal.ref_idc = get_bits(&nal.gb, 2);
  282. nal.type = get_bits(&nal.gb, 5);
  283. switch (nal.type) {
  284. case H264_NAL_SPS:
  285. ff_h264_decode_seq_parameter_set(&nal.gb, avctx, &p->ps, 0);
  286. break;
  287. case H264_NAL_PPS:
  288. ff_h264_decode_picture_parameter_set(&nal.gb, avctx, &p->ps,
  289. nal.size_bits);
  290. break;
  291. case H264_NAL_SEI:
  292. ff_h264_sei_decode(&p->sei, &nal.gb, &p->ps, avctx);
  293. break;
  294. case H264_NAL_IDR_SLICE:
  295. s->key_frame = 1;
  296. p->poc.prev_frame_num = 0;
  297. p->poc.prev_frame_num_offset = 0;
  298. p->poc.prev_poc_msb =
  299. p->poc.prev_poc_lsb = 0;
  300. /* fall through */
  301. case H264_NAL_SLICE:
  302. get_ue_golomb_long(&nal.gb); // skip first_mb_in_slice
  303. slice_type = get_ue_golomb_31(&nal.gb);
  304. s->pict_type = ff_h264_golomb_to_pict_type[slice_type % 5];
  305. if (p->sei.recovery_point.recovery_frame_cnt >= 0) {
  306. /* key frame, since recovery_frame_cnt is set */
  307. s->key_frame = 1;
  308. }
  309. pps_id = get_ue_golomb(&nal.gb);
  310. if (pps_id >= MAX_PPS_COUNT) {
  311. av_log(avctx, AV_LOG_ERROR,
  312. "pps_id %u out of range\n", pps_id);
  313. goto fail;
  314. }
  315. if (!p->ps.pps_list[pps_id]) {
  316. av_log(avctx, AV_LOG_ERROR,
  317. "non-existing PPS %u referenced\n", pps_id);
  318. goto fail;
  319. }
  320. av_buffer_unref(&p->ps.pps_ref);
  321. av_buffer_unref(&p->ps.sps_ref);
  322. p->ps.pps = NULL;
  323. p->ps.sps = NULL;
  324. p->ps.pps_ref = av_buffer_ref(p->ps.pps_list[pps_id]);
  325. if (!p->ps.pps_ref)
  326. goto fail;
  327. p->ps.pps = (const PPS*)p->ps.pps_ref->data;
  328. if (!p->ps.sps_list[p->ps.pps->sps_id]) {
  329. av_log(avctx, AV_LOG_ERROR,
  330. "non-existing SPS %u referenced\n", p->ps.pps->sps_id);
  331. goto fail;
  332. }
  333. p->ps.sps_ref = av_buffer_ref(p->ps.sps_list[p->ps.pps->sps_id]);
  334. if (!p->ps.sps_ref)
  335. goto fail;
  336. p->ps.sps = (const SPS*)p->ps.sps_ref->data;
  337. sps = p->ps.sps;
  338. // heuristic to detect non marked keyframes
  339. if (p->ps.sps->ref_frame_count <= 1 && p->ps.pps->ref_count[0] <= 1 && s->pict_type == AV_PICTURE_TYPE_I)
  340. s->key_frame = 1;
  341. p->poc.frame_num = get_bits(&nal.gb, sps->log2_max_frame_num);
  342. s->coded_width = 16 * sps->mb_width;
  343. s->coded_height = 16 * sps->mb_height;
  344. s->width = s->coded_width - (sps->crop_right + sps->crop_left);
  345. s->height = s->coded_height - (sps->crop_top + sps->crop_bottom);
  346. if (s->width <= 0 || s->height <= 0) {
  347. s->width = s->coded_width;
  348. s->height = s->coded_height;
  349. }
  350. switch (sps->bit_depth_luma) {
  351. case 9:
  352. if (sps->chroma_format_idc == 3) s->format = AV_PIX_FMT_YUV444P9;
  353. else if (sps->chroma_format_idc == 2) s->format = AV_PIX_FMT_YUV422P9;
  354. else s->format = AV_PIX_FMT_YUV420P9;
  355. break;
  356. case 10:
  357. if (sps->chroma_format_idc == 3) s->format = AV_PIX_FMT_YUV444P10;
  358. else if (sps->chroma_format_idc == 2) s->format = AV_PIX_FMT_YUV422P10;
  359. else s->format = AV_PIX_FMT_YUV420P10;
  360. break;
  361. case 8:
  362. if (sps->chroma_format_idc == 3) s->format = AV_PIX_FMT_YUV444P;
  363. else if (sps->chroma_format_idc == 2) s->format = AV_PIX_FMT_YUV422P;
  364. else s->format = AV_PIX_FMT_YUV420P;
  365. break;
  366. default:
  367. s->format = AV_PIX_FMT_NONE;
  368. }
  369. avctx->profile = ff_h264_get_profile(sps);
  370. avctx->level = sps->level_idc;
  371. if (sps->frame_mbs_only_flag) {
  372. p->picture_structure = PICT_FRAME;
  373. } else {
  374. if (get_bits1(&nal.gb)) { // field_pic_flag
  375. p->picture_structure = PICT_TOP_FIELD + get_bits1(&nal.gb); // bottom_field_flag
  376. } else {
  377. p->picture_structure = PICT_FRAME;
  378. }
  379. }
  380. if (nal.type == H264_NAL_IDR_SLICE)
  381. get_ue_golomb_long(&nal.gb); /* idr_pic_id */
  382. if (sps->poc_type == 0) {
  383. p->poc.poc_lsb = get_bits(&nal.gb, sps->log2_max_poc_lsb);
  384. if (p->ps.pps->pic_order_present == 1 &&
  385. p->picture_structure == PICT_FRAME)
  386. p->poc.delta_poc_bottom = get_se_golomb(&nal.gb);
  387. }
  388. if (sps->poc_type == 1 &&
  389. !sps->delta_pic_order_always_zero_flag) {
  390. p->poc.delta_poc[0] = get_se_golomb(&nal.gb);
  391. if (p->ps.pps->pic_order_present == 1 &&
  392. p->picture_structure == PICT_FRAME)
  393. p->poc.delta_poc[1] = get_se_golomb(&nal.gb);
  394. }
  395. /* Decode POC of this picture.
  396. * The prev_ values needed for decoding POC of the next picture are not set here. */
  397. field_poc[0] = field_poc[1] = INT_MAX;
  398. ff_h264_init_poc(field_poc, &s->output_picture_number, sps,
  399. &p->poc, p->picture_structure, nal.ref_idc);
  400. /* Continue parsing to check if MMCO_RESET is present.
  401. * FIXME: MMCO_RESET could appear in non-first slice.
  402. * Maybe, we should parse all undisposable non-IDR slice of this
  403. * picture until encountering MMCO_RESET in a slice of it. */
  404. if (nal.ref_idc && nal.type != H264_NAL_IDR_SLICE) {
  405. got_reset = scan_mmco_reset(s, &nal.gb, avctx);
  406. if (got_reset < 0)
  407. goto fail;
  408. }
  409. /* Set up the prev_ values for decoding POC of the next picture. */
  410. p->poc.prev_frame_num = got_reset ? 0 : p->poc.frame_num;
  411. p->poc.prev_frame_num_offset = got_reset ? 0 : p->poc.frame_num_offset;
  412. if (nal.ref_idc != 0) {
  413. if (!got_reset) {
  414. p->poc.prev_poc_msb = p->poc.poc_msb;
  415. p->poc.prev_poc_lsb = p->poc.poc_lsb;
  416. } else {
  417. p->poc.prev_poc_msb = 0;
  418. p->poc.prev_poc_lsb =
  419. p->picture_structure == PICT_BOTTOM_FIELD ? 0 : field_poc[0];
  420. }
  421. }
  422. if (sps->pic_struct_present_flag && p->sei.picture_timing.present) {
  423. switch (p->sei.picture_timing.pic_struct) {
  424. case SEI_PIC_STRUCT_TOP_FIELD:
  425. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  426. s->repeat_pict = 0;
  427. break;
  428. case SEI_PIC_STRUCT_FRAME:
  429. case SEI_PIC_STRUCT_TOP_BOTTOM:
  430. case SEI_PIC_STRUCT_BOTTOM_TOP:
  431. s->repeat_pict = 1;
  432. break;
  433. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  434. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  435. s->repeat_pict = 2;
  436. break;
  437. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  438. s->repeat_pict = 3;
  439. break;
  440. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  441. s->repeat_pict = 5;
  442. break;
  443. default:
  444. s->repeat_pict = p->picture_structure == PICT_FRAME ? 1 : 0;
  445. break;
  446. }
  447. } else {
  448. s->repeat_pict = p->picture_structure == PICT_FRAME ? 1 : 0;
  449. }
  450. if (p->picture_structure == PICT_FRAME) {
  451. s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
  452. if (sps->pic_struct_present_flag && p->sei.picture_timing.present) {
  453. switch (p->sei.picture_timing.pic_struct) {
  454. case SEI_PIC_STRUCT_TOP_BOTTOM:
  455. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  456. s->field_order = AV_FIELD_TT;
  457. break;
  458. case SEI_PIC_STRUCT_BOTTOM_TOP:
  459. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  460. s->field_order = AV_FIELD_BB;
  461. break;
  462. default:
  463. s->field_order = AV_FIELD_PROGRESSIVE;
  464. break;
  465. }
  466. } else {
  467. if (field_poc[0] < field_poc[1])
  468. s->field_order = AV_FIELD_TT;
  469. else if (field_poc[0] > field_poc[1])
  470. s->field_order = AV_FIELD_BB;
  471. else
  472. s->field_order = AV_FIELD_PROGRESSIVE;
  473. }
  474. } else {
  475. if (p->picture_structure == PICT_TOP_FIELD)
  476. s->picture_structure = AV_PICTURE_STRUCTURE_TOP_FIELD;
  477. else
  478. s->picture_structure = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
  479. if (p->poc.frame_num == p->last_frame_num &&
  480. p->last_picture_structure != AV_PICTURE_STRUCTURE_UNKNOWN &&
  481. p->last_picture_structure != AV_PICTURE_STRUCTURE_FRAME &&
  482. p->last_picture_structure != s->picture_structure) {
  483. if (p->last_picture_structure == AV_PICTURE_STRUCTURE_TOP_FIELD)
  484. s->field_order = AV_FIELD_TT;
  485. else
  486. s->field_order = AV_FIELD_BB;
  487. } else {
  488. s->field_order = AV_FIELD_UNKNOWN;
  489. }
  490. p->last_picture_structure = s->picture_structure;
  491. p->last_frame_num = p->poc.frame_num;
  492. }
  493. av_freep(&nal.rbsp_buffer);
  494. return 0; /* no need to evaluate the rest */
  495. }
  496. }
  497. if (q264) {
  498. av_freep(&nal.rbsp_buffer);
  499. return 0;
  500. }
  501. /* didn't find a picture! */
  502. av_log(avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
  503. fail:
  504. av_freep(&nal.rbsp_buffer);
  505. return -1;
  506. }
  507. static int h264_parse(AVCodecParserContext *s,
  508. AVCodecContext *avctx,
  509. const uint8_t **poutbuf, int *poutbuf_size,
  510. const uint8_t *buf, int buf_size)
  511. {
  512. H264ParseContext *p = s->priv_data;
  513. ParseContext *pc = &p->pc;
  514. int next;
  515. if (!p->got_first) {
  516. p->got_first = 1;
  517. if (avctx->extradata_size) {
  518. ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
  519. &p->ps, &p->is_avc, &p->nal_length_size,
  520. avctx->err_recognition, avctx);
  521. }
  522. }
  523. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  524. next = buf_size;
  525. } else {
  526. next = h264_find_frame_end(p, buf, buf_size, avctx);
  527. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  528. *poutbuf = NULL;
  529. *poutbuf_size = 0;
  530. return buf_size;
  531. }
  532. if (next < 0 && next != END_NOT_FOUND) {
  533. av_assert1(pc->last_index + next >= 0);
  534. h264_find_frame_end(p, &pc->buffer[pc->last_index + next], -next, avctx); // update state
  535. }
  536. }
  537. parse_nal_units(s, avctx, buf, buf_size);
  538. if (avctx->framerate.num)
  539. avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
  540. if (p->sei.picture_timing.cpb_removal_delay >= 0) {
  541. s->dts_sync_point = p->sei.buffering_period.present;
  542. s->dts_ref_dts_delta = p->sei.picture_timing.cpb_removal_delay;
  543. s->pts_dts_delta = p->sei.picture_timing.dpb_output_delay;
  544. } else {
  545. s->dts_sync_point = INT_MIN;
  546. s->dts_ref_dts_delta = INT_MIN;
  547. s->pts_dts_delta = INT_MIN;
  548. }
  549. if (s->flags & PARSER_FLAG_ONCE) {
  550. s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
  551. }
  552. if (s->dts_sync_point >= 0) {
  553. int64_t den = avctx->time_base.den * (int64_t)avctx->pkt_timebase.num;
  554. if (den > 0) {
  555. int64_t num = avctx->time_base.num * (int64_t)avctx->pkt_timebase.den;
  556. if (s->dts != AV_NOPTS_VALUE) {
  557. // got DTS from the stream, update reference timestamp
  558. p->reference_dts = s->dts - av_rescale(s->dts_ref_dts_delta, num, den);
  559. } else if (p->reference_dts != AV_NOPTS_VALUE) {
  560. // compute DTS based on reference timestamp
  561. s->dts = p->reference_dts + av_rescale(s->dts_ref_dts_delta, num, den);
  562. }
  563. if (p->reference_dts != AV_NOPTS_VALUE && s->pts == AV_NOPTS_VALUE)
  564. s->pts = s->dts + av_rescale(s->pts_dts_delta, num, den);
  565. if (s->dts_sync_point > 0)
  566. p->reference_dts = s->dts; // new reference
  567. }
  568. }
  569. *poutbuf = buf;
  570. *poutbuf_size = buf_size;
  571. return next;
  572. }
  573. static int h264_split(AVCodecContext *avctx,
  574. const uint8_t *buf, int buf_size)
  575. {
  576. uint32_t state = -1;
  577. int has_sps = 0;
  578. int has_pps = 0;
  579. const uint8_t *ptr = buf, *end = buf + buf_size;
  580. int nalu_type;
  581. while (ptr < end) {
  582. ptr = avpriv_find_start_code(ptr, end, &state);
  583. if ((state & 0xFFFFFF00) != 0x100)
  584. break;
  585. nalu_type = state & 0x1F;
  586. if (nalu_type == H264_NAL_SPS) {
  587. has_sps = 1;
  588. } else if (nalu_type == H264_NAL_PPS)
  589. has_pps = 1;
  590. /* else if (nalu_type == 0x01 ||
  591. * nalu_type == 0x02 ||
  592. * nalu_type == 0x05) {
  593. * }
  594. */
  595. else if ((nalu_type != H264_NAL_SEI || has_pps) &&
  596. nalu_type != H264_NAL_AUD && nalu_type != H264_NAL_SPS_EXT &&
  597. nalu_type != 0x0f) {
  598. if (has_sps) {
  599. while (ptr - 4 > buf && ptr[-5] == 0)
  600. ptr--;
  601. return ptr - 4 - buf;
  602. }
  603. }
  604. }
  605. return 0;
  606. }
  607. static void h264_close(AVCodecParserContext *s)
  608. {
  609. H264ParseContext *p = s->priv_data;
  610. ParseContext *pc = &p->pc;
  611. av_freep(&pc->buffer);
  612. ff_h264_sei_uninit(&p->sei);
  613. ff_h264_ps_uninit(&p->ps);
  614. }
  615. static av_cold int init(AVCodecParserContext *s)
  616. {
  617. H264ParseContext *p = s->priv_data;
  618. p->reference_dts = AV_NOPTS_VALUE;
  619. p->last_frame_num = INT_MAX;
  620. ff_h264dsp_init(&p->h264dsp, 8, 1);
  621. return 0;
  622. }
  623. AVCodecParser ff_h264_parser = {
  624. .codec_ids = { AV_CODEC_ID_H264 },
  625. .priv_data_size = sizeof(H264ParseContext),
  626. .parser_init = init,
  627. .parser_parse = h264_parse,
  628. .parser_close = h264_close,
  629. .split = h264_split,
  630. };