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.

675 lines
23KB

  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 "get_bits.h"
  35. #include "golomb.h"
  36. #include "h264.h"
  37. #include "h264_sei.h"
  38. #include "h264data.h"
  39. #include "internal.h"
  40. #include "mpegutils.h"
  41. #include "parser.h"
  42. typedef struct H264ParseContext {
  43. ParseContext pc;
  44. H264ParamSets ps;
  45. H264DSPContext h264dsp;
  46. H264POCContext poc;
  47. H264SEIContext sei;
  48. int is_avc;
  49. int nal_length_size;
  50. int got_first;
  51. int picture_structure;
  52. uint8_t parse_history[6];
  53. int parse_history_count;
  54. int parse_last_mb;
  55. } H264ParseContext;
  56. static int h264_find_frame_end(H264ParseContext *p, const uint8_t *buf,
  57. int buf_size, void *logctx)
  58. {
  59. int i, j;
  60. uint32_t state;
  61. ParseContext *pc = &p->pc;
  62. int next_avc = p->is_avc ? 0 : buf_size;
  63. // mb_addr= pc->mb_addr - 1;
  64. state = pc->state;
  65. if (state > 13)
  66. state = 7;
  67. if (p->is_avc && !p->nal_length_size)
  68. av_log(logctx, AV_LOG_ERROR, "AVC-parser: nal length size invalid\n");
  69. for (i = 0; i < buf_size; i++) {
  70. if (i >= next_avc) {
  71. int nalsize = 0;
  72. i = next_avc;
  73. for (j = 0; j < p->nal_length_size; j++)
  74. nalsize = (nalsize << 8) | buf[i++];
  75. if (nalsize <= 0 || nalsize > buf_size - i) {
  76. av_log(logctx, AV_LOG_ERROR, "AVC-parser: nal size %d remaining %d\n", nalsize, buf_size - i);
  77. return buf_size;
  78. }
  79. next_avc = i + nalsize;
  80. state = 5;
  81. }
  82. if (state == 7) {
  83. i += p->h264dsp.startcode_find_candidate(buf + i, next_avc - i);
  84. if (i < next_avc)
  85. state = 2;
  86. } else if (state <= 2) {
  87. if (buf[i] == 1)
  88. state ^= 5; // 2->7, 1->4, 0->5
  89. else if (buf[i])
  90. state = 7;
  91. else
  92. state >>= 1; // 2->1, 1->0, 0->0
  93. } else if (state <= 5) {
  94. int nalu_type = buf[i] & 0x1F;
  95. if (nalu_type == NAL_SEI || nalu_type == NAL_SPS ||
  96. nalu_type == NAL_PPS || nalu_type == NAL_AUD) {
  97. if (pc->frame_start_found) {
  98. i++;
  99. goto found;
  100. }
  101. } else if (nalu_type == NAL_SLICE || nalu_type == NAL_DPA ||
  102. nalu_type == NAL_IDR_SLICE) {
  103. state += 8;
  104. continue;
  105. }
  106. state = 7;
  107. } else {
  108. p->parse_history[p->parse_history_count++] = buf[i];
  109. if (p->parse_history_count > 5) {
  110. unsigned int mb, last_mb = p->parse_last_mb;
  111. GetBitContext gb;
  112. init_get_bits(&gb, p->parse_history, 8*p->parse_history_count);
  113. p->parse_history_count = 0;
  114. mb= get_ue_golomb_long(&gb);
  115. p->parse_last_mb = mb;
  116. if (pc->frame_start_found) {
  117. if (mb <= last_mb)
  118. goto found;
  119. } else
  120. pc->frame_start_found = 1;
  121. state = 7;
  122. }
  123. }
  124. }
  125. pc->state = state;
  126. if (p->is_avc)
  127. return next_avc;
  128. return END_NOT_FOUND;
  129. found:
  130. pc->state = 7;
  131. pc->frame_start_found = 0;
  132. if (p->is_avc)
  133. return next_avc;
  134. return i - (state & 5) - 5 * (state > 7);
  135. }
  136. static int scan_mmco_reset(AVCodecParserContext *s, GetBitContext *gb,
  137. void *logctx)
  138. {
  139. H264PredWeightTable pwt;
  140. int slice_type_nos = s->pict_type & 3;
  141. H264ParseContext *p = s->priv_data;
  142. int list_count, ref_count[2];
  143. if (p->ps.pps->redundant_pic_cnt_present)
  144. get_ue_golomb(gb); // redundant_pic_count
  145. if (slice_type_nos == AV_PICTURE_TYPE_B)
  146. get_bits1(gb); // direct_spatial_mv_pred
  147. if (ff_h264_parse_ref_count(&list_count, ref_count, gb, p->ps.pps,
  148. slice_type_nos, p->picture_structure, logctx) < 0)
  149. return AVERROR_INVALIDDATA;
  150. if (slice_type_nos != AV_PICTURE_TYPE_I) {
  151. int list;
  152. for (list = 0; list < list_count; list++) {
  153. if (get_bits1(gb)) {
  154. int index;
  155. for (index = 0; ; index++) {
  156. unsigned int reordering_of_pic_nums_idc = get_ue_golomb_31(gb);
  157. if (reordering_of_pic_nums_idc < 3)
  158. get_ue_golomb_long(gb);
  159. else if (reordering_of_pic_nums_idc > 3) {
  160. av_log(logctx, AV_LOG_ERROR,
  161. "illegal reordering_of_pic_nums_idc %d\n",
  162. reordering_of_pic_nums_idc);
  163. return AVERROR_INVALIDDATA;
  164. } else
  165. break;
  166. if (index >= ref_count[list]) {
  167. av_log(logctx, AV_LOG_ERROR,
  168. "reference count %d overflow\n", index);
  169. return AVERROR_INVALIDDATA;
  170. }
  171. }
  172. }
  173. }
  174. }
  175. if ((p->ps.pps->weighted_pred && slice_type_nos == AV_PICTURE_TYPE_P) ||
  176. (p->ps.pps->weighted_bipred_idc == 1 && slice_type_nos == AV_PICTURE_TYPE_B))
  177. ff_h264_pred_weight_table(gb, p->ps.sps, ref_count, slice_type_nos,
  178. &pwt, logctx);
  179. if (get_bits1(gb)) { // adaptive_ref_pic_marking_mode_flag
  180. int i;
  181. for (i = 0; i < MAX_MMCO_COUNT; i++) {
  182. MMCOOpcode opcode = get_ue_golomb_31(gb);
  183. if (opcode > (unsigned) MMCO_LONG) {
  184. av_log(logctx, AV_LOG_ERROR,
  185. "illegal memory management control operation %d\n",
  186. opcode);
  187. return AVERROR_INVALIDDATA;
  188. }
  189. if (opcode == MMCO_END)
  190. return 0;
  191. else if (opcode == MMCO_RESET)
  192. return 1;
  193. if (opcode == MMCO_SHORT2UNUSED || opcode == MMCO_SHORT2LONG)
  194. get_ue_golomb_long(gb); // difference_of_pic_nums_minus1
  195. if (opcode == MMCO_SHORT2LONG || opcode == MMCO_LONG2UNUSED ||
  196. opcode == MMCO_LONG || opcode == MMCO_SET_MAX_LONG)
  197. get_ue_golomb_31(gb);
  198. }
  199. }
  200. return 0;
  201. }
  202. static inline int get_avc_nalsize(H264ParseContext *p, const uint8_t *buf,
  203. int buf_size, int *buf_index, void *logctx)
  204. {
  205. int i, nalsize = 0;
  206. if (*buf_index >= buf_size - p->nal_length_size) {
  207. // the end of the buffer is reached, refill it
  208. return AVERROR(EAGAIN);
  209. }
  210. for (i = 0; i < p->nal_length_size; i++)
  211. nalsize = ((unsigned)nalsize << 8) | buf[(*buf_index)++];
  212. if (nalsize <= 0 || nalsize > buf_size - *buf_index) {
  213. av_log(logctx, AV_LOG_ERROR,
  214. "AVC: nal size %d\n", nalsize);
  215. return AVERROR_INVALIDDATA;
  216. }
  217. return nalsize;
  218. }
  219. /**
  220. * Parse NAL units of found picture and decode some basic information.
  221. *
  222. * @param s parser context.
  223. * @param avctx codec context.
  224. * @param buf buffer with field/frame data.
  225. * @param buf_size size of the buffer.
  226. */
  227. static inline int parse_nal_units(AVCodecParserContext *s,
  228. AVCodecContext *avctx,
  229. const uint8_t * const buf, int buf_size)
  230. {
  231. H264ParseContext *p = s->priv_data;
  232. H2645NAL nal = { NULL };
  233. int buf_index, next_avc;
  234. unsigned int pps_id;
  235. unsigned int slice_type;
  236. int state = -1, got_reset = 0;
  237. int q264 = buf_size >=4 && !memcmp("Q264", buf, 4);
  238. int field_poc[2];
  239. int ret;
  240. /* set some sane default values */
  241. s->pict_type = AV_PICTURE_TYPE_I;
  242. s->key_frame = 0;
  243. s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
  244. ff_h264_sei_uninit(&p->sei);
  245. p->sei.frame_packing.frame_packing_arrangement_cancel_flag = -1;
  246. if (!buf_size)
  247. return 0;
  248. buf_index = 0;
  249. next_avc = p->is_avc ? 0 : buf_size;
  250. for (;;) {
  251. const SPS *sps;
  252. int src_length, consumed, nalsize = 0;
  253. if (buf_index >= next_avc) {
  254. nalsize = get_avc_nalsize(p, buf, buf_size, &buf_index, avctx);
  255. if (nalsize < 0)
  256. break;
  257. next_avc = buf_index + nalsize;
  258. } else {
  259. buf_index = find_start_code(buf, buf_size, buf_index, next_avc);
  260. if (buf_index >= buf_size)
  261. break;
  262. if (buf_index >= next_avc)
  263. continue;
  264. }
  265. src_length = next_avc - buf_index;
  266. state = buf[buf_index];
  267. switch (state & 0x1f) {
  268. case NAL_SLICE:
  269. case NAL_IDR_SLICE:
  270. // Do not walk the whole buffer just to decode slice header
  271. if ((state & 0x1f) == NAL_IDR_SLICE || ((state >> 5) & 0x3) == 0) {
  272. /* IDR or disposable slice
  273. * No need to decode many bytes because MMCOs shall not be present. */
  274. if (src_length > 60)
  275. src_length = 60;
  276. } else {
  277. /* To decode up to MMCOs */
  278. if (src_length > 1000)
  279. src_length = 1000;
  280. }
  281. break;
  282. }
  283. consumed = ff_h2645_extract_rbsp(buf + buf_index, src_length, &nal);
  284. if (consumed < 0)
  285. break;
  286. buf_index += consumed;
  287. ret = init_get_bits8(&nal.gb, nal.data, nal.size);
  288. if (ret < 0)
  289. goto fail;
  290. get_bits1(&nal.gb);
  291. nal.ref_idc = get_bits(&nal.gb, 2);
  292. nal.type = get_bits(&nal.gb, 5);
  293. switch (nal.type) {
  294. case NAL_SPS:
  295. ff_h264_decode_seq_parameter_set(&nal.gb, avctx, &p->ps, 0);
  296. break;
  297. case NAL_PPS:
  298. ff_h264_decode_picture_parameter_set(&nal.gb, avctx, &p->ps,
  299. nal.size_bits);
  300. break;
  301. case NAL_SEI:
  302. ff_h264_sei_decode(&p->sei, &nal.gb, &p->ps, avctx);
  303. break;
  304. case NAL_IDR_SLICE:
  305. s->key_frame = 1;
  306. p->poc.prev_frame_num = 0;
  307. p->poc.prev_frame_num_offset = 0;
  308. p->poc.prev_poc_msb =
  309. p->poc.prev_poc_lsb = 0;
  310. /* fall through */
  311. case NAL_SLICE:
  312. get_ue_golomb_long(&nal.gb); // skip first_mb_in_slice
  313. slice_type = get_ue_golomb_31(&nal.gb);
  314. s->pict_type = ff_h264_golomb_to_pict_type[slice_type % 5];
  315. if (p->sei.recovery_point.recovery_frame_cnt >= 0) {
  316. /* key frame, since recovery_frame_cnt is set */
  317. s->key_frame = 1;
  318. }
  319. pps_id = get_ue_golomb(&nal.gb);
  320. if (pps_id >= MAX_PPS_COUNT) {
  321. av_log(avctx, AV_LOG_ERROR,
  322. "pps_id %u out of range\n", pps_id);
  323. goto fail;
  324. }
  325. if (!p->ps.pps_list[pps_id]) {
  326. av_log(avctx, AV_LOG_ERROR,
  327. "non-existing PPS %u referenced\n", pps_id);
  328. goto fail;
  329. }
  330. p->ps.pps = (const PPS*)p->ps.pps_list[pps_id]->data;
  331. if (!p->ps.sps_list[p->ps.pps->sps_id]) {
  332. av_log(avctx, AV_LOG_ERROR,
  333. "non-existing SPS %u referenced\n", p->ps.pps->sps_id);
  334. goto fail;
  335. }
  336. p->ps.sps = (SPS*)p->ps.sps_list[p->ps.pps->sps_id]->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 == 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 != 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) {
  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) {
  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. s->field_order = AV_FIELD_UNKNOWN;
  480. }
  481. av_freep(&nal.rbsp_buffer);
  482. return 0; /* no need to evaluate the rest */
  483. }
  484. }
  485. if (q264) {
  486. av_freep(&nal.rbsp_buffer);
  487. return 0;
  488. }
  489. /* didn't find a picture! */
  490. av_log(avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
  491. fail:
  492. av_freep(&nal.rbsp_buffer);
  493. return -1;
  494. }
  495. static int h264_parse(AVCodecParserContext *s,
  496. AVCodecContext *avctx,
  497. const uint8_t **poutbuf, int *poutbuf_size,
  498. const uint8_t *buf, int buf_size)
  499. {
  500. H264ParseContext *p = s->priv_data;
  501. ParseContext *pc = &p->pc;
  502. int next;
  503. if (!p->got_first) {
  504. p->got_first = 1;
  505. if (avctx->extradata_size) {
  506. ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
  507. &p->ps, &p->is_avc, &p->nal_length_size,
  508. avctx->err_recognition, avctx);
  509. }
  510. }
  511. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  512. next = buf_size;
  513. } else {
  514. next = h264_find_frame_end(p, buf, buf_size, avctx);
  515. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  516. *poutbuf = NULL;
  517. *poutbuf_size = 0;
  518. return buf_size;
  519. }
  520. if (next < 0 && next != END_NOT_FOUND) {
  521. av_assert1(pc->last_index + next >= 0);
  522. h264_find_frame_end(p, &pc->buffer[pc->last_index + next], -next, avctx); // update state
  523. }
  524. }
  525. parse_nal_units(s, avctx, buf, buf_size);
  526. if (avctx->framerate.num)
  527. avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
  528. if (p->sei.picture_timing.cpb_removal_delay >= 0) {
  529. s->dts_sync_point = p->sei.buffering_period.present;
  530. s->dts_ref_dts_delta = p->sei.picture_timing.cpb_removal_delay;
  531. s->pts_dts_delta = p->sei.picture_timing.dpb_output_delay;
  532. } else {
  533. s->dts_sync_point = INT_MIN;
  534. s->dts_ref_dts_delta = INT_MIN;
  535. s->pts_dts_delta = INT_MIN;
  536. }
  537. if (s->flags & PARSER_FLAG_ONCE) {
  538. s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
  539. }
  540. *poutbuf = buf;
  541. *poutbuf_size = buf_size;
  542. return next;
  543. }
  544. static int h264_split(AVCodecContext *avctx,
  545. const uint8_t *buf, int buf_size)
  546. {
  547. uint32_t state = -1;
  548. int has_sps = 0;
  549. int has_pps = 0;
  550. const uint8_t *ptr = buf, *end = buf + buf_size;
  551. int nalu_type;
  552. while (ptr < end) {
  553. ptr = avpriv_find_start_code(ptr, end, &state);
  554. if ((state & 0xFFFFFF00) != 0x100)
  555. break;
  556. nalu_type = state & 0x1F;
  557. if (nalu_type == NAL_SPS) {
  558. has_sps = 1;
  559. } else if (nalu_type == NAL_PPS)
  560. has_pps = 1;
  561. /* else if (nalu_type == 0x01 ||
  562. * nalu_type == 0x02 ||
  563. * nalu_type == 0x05) {
  564. * }
  565. */
  566. else if ((nalu_type != NAL_SEI || has_pps) &&
  567. nalu_type != NAL_AUD && nalu_type != NAL_SPS_EXT &&
  568. nalu_type != 0x0f) {
  569. if (has_sps) {
  570. while (ptr - 4 > buf && ptr[-5] == 0)
  571. ptr--;
  572. return ptr - 4 - buf;
  573. }
  574. }
  575. }
  576. return 0;
  577. }
  578. static void h264_close(AVCodecParserContext *s)
  579. {
  580. H264ParseContext *p = s->priv_data;
  581. ParseContext *pc = &p->pc;
  582. av_freep(&pc->buffer);
  583. ff_h264_sei_uninit(&p->sei);
  584. ff_h264_ps_uninit(&p->ps);
  585. }
  586. static av_cold int init(AVCodecParserContext *s)
  587. {
  588. H264ParseContext *p = s->priv_data;
  589. ff_h264dsp_init(&p->h264dsp, 8, 1);
  590. return 0;
  591. }
  592. AVCodecParser ff_h264_parser = {
  593. .codec_ids = { AV_CODEC_ID_H264 },
  594. .priv_data_size = sizeof(H264ParseContext),
  595. .parser_init = init,
  596. .parser_parse = h264_parse,
  597. .parser_close = h264_close,
  598. .split = h264_split,
  599. };