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.

567 lines
19KB

  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 *buf, int buf_size)
  183. {
  184. H264Context *h = s->priv_data;
  185. const uint8_t *buf_end = buf + buf_size;
  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. for (;;) {
  202. int src_length, dst_length, consumed, nalsize = 0;
  203. if (h->is_avc) {
  204. int i;
  205. if (h->nal_length_size >= buf_end - buf) break;
  206. nalsize = 0;
  207. for (i = 0; i < h->nal_length_size; i++)
  208. nalsize = (nalsize << 8) | *buf++;
  209. if (nalsize <= 0 || nalsize > buf_end - buf) {
  210. av_log(h->avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  211. break;
  212. }
  213. src_length = nalsize;
  214. } else {
  215. buf = avpriv_find_start_code(buf, buf_end, &state);
  216. if (buf >= buf_end)
  217. break;
  218. --buf;
  219. src_length = buf_end - buf;
  220. }
  221. switch (state & 0x1f) {
  222. case NAL_SLICE:
  223. case NAL_IDR_SLICE:
  224. // Do not walk the whole buffer just to decode slice header
  225. if ((state & 0x1f) == NAL_IDR_SLICE || ((state >> 5) & 0x3) == 0) {
  226. /* IDR or disposable slice
  227. * No need to decode many bytes because MMCOs shall not be present. */
  228. if (src_length > 60)
  229. src_length = 60;
  230. } else {
  231. /* To decode up to MMCOs */
  232. if (src_length > 1000)
  233. src_length = 1000;
  234. }
  235. break;
  236. }
  237. ptr = ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length);
  238. if (ptr == NULL || dst_length < 0)
  239. break;
  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. buf += h->is_avc ? nalsize : consumed;
  401. }
  402. if (q264)
  403. return 0;
  404. /* didn't find a picture! */
  405. av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
  406. return -1;
  407. }
  408. static int h264_parse(AVCodecParserContext *s,
  409. AVCodecContext *avctx,
  410. const uint8_t **poutbuf, int *poutbuf_size,
  411. const uint8_t *buf, int buf_size)
  412. {
  413. H264Context *h = s->priv_data;
  414. ParseContext *pc = &h->parse_context;
  415. int next;
  416. if (!h->got_first) {
  417. h->got_first = 1;
  418. if (avctx->extradata_size) {
  419. h->avctx = avctx;
  420. // must be done like in decoder, otherwise opening the parser,
  421. // letting it create extradata and then closing and opening again
  422. // will cause has_b_frames to be always set.
  423. // Note that estimate_timings_from_pts does exactly this.
  424. if (!avctx->has_b_frames)
  425. h->low_delay = 1;
  426. ff_h264_decode_extradata(h, avctx->extradata, avctx->extradata_size);
  427. }
  428. }
  429. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  430. next = buf_size;
  431. } else {
  432. next = h264_find_frame_end(h, buf, buf_size);
  433. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  434. *poutbuf = NULL;
  435. *poutbuf_size = 0;
  436. return buf_size;
  437. }
  438. if (next < 0 && next != END_NOT_FOUND) {
  439. av_assert1(pc->last_index + next >= 0);
  440. h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next); // update state
  441. }
  442. }
  443. parse_nal_units(s, avctx, buf, buf_size);
  444. if (h->sei_cpb_removal_delay >= 0) {
  445. s->dts_sync_point = h->sei_buffering_period_present;
  446. s->dts_ref_dts_delta = h->sei_cpb_removal_delay;
  447. s->pts_dts_delta = h->sei_dpb_output_delay;
  448. } else {
  449. s->dts_sync_point = INT_MIN;
  450. s->dts_ref_dts_delta = INT_MIN;
  451. s->pts_dts_delta = INT_MIN;
  452. }
  453. if (s->flags & PARSER_FLAG_ONCE) {
  454. s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
  455. }
  456. *poutbuf = buf;
  457. *poutbuf_size = buf_size;
  458. return next;
  459. }
  460. static int h264_split(AVCodecContext *avctx,
  461. const uint8_t *buf, int buf_size)
  462. {
  463. int i;
  464. uint32_t state = -1;
  465. int has_sps = 0;
  466. for (i = 0; i <= buf_size; i++) {
  467. if ((state & 0xFFFFFF1F) == 0x107)
  468. has_sps = 1;
  469. /* if ((state&0xFFFFFF1F) == 0x101 ||
  470. * (state&0xFFFFFF1F) == 0x102 ||
  471. * (state&0xFFFFFF1F) == 0x105) {
  472. * }
  473. */
  474. if ((state & 0xFFFFFF00) == 0x100 && (state & 0xFFFFFF1F) != 0x107 &&
  475. (state & 0xFFFFFF1F) != 0x108 && (state & 0xFFFFFF1F) != 0x109) {
  476. if (has_sps) {
  477. while (i > 4 && buf[i - 5] == 0)
  478. i--;
  479. return i - 4;
  480. }
  481. }
  482. if (i < buf_size)
  483. state = (state << 8) | buf[i];
  484. }
  485. return 0;
  486. }
  487. static void close(AVCodecParserContext *s)
  488. {
  489. H264Context *h = s->priv_data;
  490. ParseContext *pc = &h->parse_context;
  491. av_free(pc->buffer);
  492. ff_h264_free_context(h);
  493. }
  494. static av_cold int init(AVCodecParserContext *s)
  495. {
  496. H264Context *h = s->priv_data;
  497. h->thread_context[0] = h;
  498. h->slice_context_count = 1;
  499. ff_h264dsp_init(&h->h264dsp, 8, 1);
  500. return 0;
  501. }
  502. AVCodecParser ff_h264_parser = {
  503. .codec_ids = { AV_CODEC_ID_H264 },
  504. .priv_data_size = sizeof(H264Context),
  505. .parser_init = init,
  506. .parser_parse = h264_parse,
  507. .parser_close = close,
  508. .split = h264_split,
  509. };