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.

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