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.

657 lines
22KB

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