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.

692 lines
24KB

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