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.

588 lines
20KB

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