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.

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