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.

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