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.

568 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.h264_find_start_code_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. last_mb= h->parse_last_mb;
  93. h->parse_last_mb= mb;
  94. if (pc->frame_start_found) {
  95. if (mb <= last_mb)
  96. goto found;
  97. } else
  98. pc->frame_start_found = 1;
  99. state = 7;
  100. }
  101. }
  102. }
  103. pc->state = state;
  104. if (h->is_avc)
  105. return next_avc;
  106. return END_NOT_FOUND;
  107. found:
  108. pc->state = 7;
  109. pc->frame_start_found = 0;
  110. if (h->is_avc)
  111. return next_avc;
  112. return i - (state & 5) - 5 * (state > 7);
  113. }
  114. static int scan_mmco_reset(AVCodecParserContext *s)
  115. {
  116. H264Context *h = s->priv_data;
  117. h->slice_type_nos = s->pict_type & 3;
  118. if (h->pps.redundant_pic_cnt_present)
  119. get_ue_golomb(&h->gb); // redundant_pic_count
  120. if (ff_set_ref_count(h) < 0)
  121. return AVERROR_INVALIDDATA;
  122. if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
  123. int list;
  124. for (list = 0; list < h->list_count; list++) {
  125. if (get_bits1(&h->gb)) {
  126. int index;
  127. for (index = 0; ; index++) {
  128. unsigned int reordering_of_pic_nums_idc = get_ue_golomb_31(&h->gb);
  129. if (reordering_of_pic_nums_idc < 3)
  130. get_ue_golomb(&h->gb);
  131. else if (reordering_of_pic_nums_idc > 3) {
  132. av_log(h->avctx, AV_LOG_ERROR,
  133. "illegal reordering_of_pic_nums_idc %d\n",
  134. reordering_of_pic_nums_idc);
  135. return AVERROR_INVALIDDATA;
  136. } else
  137. break;
  138. if (index >= h->ref_count[list]) {
  139. av_log(h->avctx, AV_LOG_ERROR,
  140. "reference count %d overflow\n", index);
  141. return AVERROR_INVALIDDATA;
  142. }
  143. }
  144. }
  145. }
  146. }
  147. if ((h->pps.weighted_pred && h->slice_type_nos == AV_PICTURE_TYPE_P) ||
  148. (h->pps.weighted_bipred_idc == 1 && h->slice_type_nos == AV_PICTURE_TYPE_B))
  149. ff_pred_weight_table(h);
  150. if (get_bits1(&h->gb)) { // adaptive_ref_pic_marking_mode_flag
  151. int i;
  152. for (i = 0; i < MAX_MMCO_COUNT; i++) {
  153. MMCOOpcode opcode = get_ue_golomb_31(&h->gb);
  154. if (opcode > (unsigned) MMCO_LONG) {
  155. av_log(h->avctx, AV_LOG_ERROR,
  156. "illegal memory management control operation %d\n",
  157. opcode);
  158. return AVERROR_INVALIDDATA;
  159. }
  160. if (opcode == MMCO_END)
  161. return 0;
  162. else if (opcode == MMCO_RESET)
  163. return 1;
  164. if (opcode == MMCO_SHORT2UNUSED || opcode == MMCO_SHORT2LONG)
  165. get_ue_golomb(&h->gb);
  166. if (opcode == MMCO_SHORT2LONG || opcode == MMCO_LONG2UNUSED ||
  167. opcode == MMCO_LONG || opcode == MMCO_SET_MAX_LONG)
  168. get_ue_golomb_31(&h->gb);
  169. }
  170. }
  171. return 0;
  172. }
  173. /**
  174. * Parse NAL units of found picture and decode some basic information.
  175. *
  176. * @param s parser context.
  177. * @param avctx codec context.
  178. * @param buf buffer with field/frame data.
  179. * @param buf_size size of the buffer.
  180. */
  181. static inline int parse_nal_units(AVCodecParserContext *s,
  182. AVCodecContext *avctx,
  183. const uint8_t *buf, int buf_size)
  184. {
  185. H264Context *h = s->priv_data;
  186. const uint8_t *buf_end = buf + buf_size;
  187. unsigned int pps_id;
  188. unsigned int slice_type;
  189. int state = -1, got_reset = 0;
  190. const uint8_t *ptr;
  191. int q264 = buf_size >=4 && !memcmp("Q264", buf, 4);
  192. int field_poc[2];
  193. /* set some sane default values */
  194. s->pict_type = AV_PICTURE_TYPE_I;
  195. s->key_frame = 0;
  196. s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
  197. h->avctx = avctx;
  198. ff_h264_reset_sei(h);
  199. h->sei_fpa.frame_packing_arrangement_cancel_flag = -1;
  200. if (!buf_size)
  201. return 0;
  202. for (;;) {
  203. int src_length, dst_length, consumed, nalsize = 0;
  204. if (h->is_avc) {
  205. int i;
  206. if (h->nal_length_size >= buf_end - buf) break;
  207. nalsize = 0;
  208. for (i = 0; i < h->nal_length_size; i++)
  209. nalsize = (nalsize << 8) | *buf++;
  210. if (nalsize <= 0 || nalsize > buf_end - buf) {
  211. av_log(h->avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  212. break;
  213. }
  214. src_length = nalsize;
  215. } else {
  216. buf = avpriv_find_start_code(buf, buf_end, &state);
  217. if (buf >= buf_end)
  218. break;
  219. --buf;
  220. src_length = buf_end - buf;
  221. }
  222. switch (state & 0x1f) {
  223. case NAL_SLICE:
  224. case NAL_IDR_SLICE:
  225. // Do not walk the whole buffer just to decode slice header
  226. if ((state & 0x1f) == NAL_IDR_SLICE || ((state >> 5) & 0x3) == 0) {
  227. /* IDR or disposable slice
  228. * No need to decode many bytes because MMCOs shall not be present. */
  229. if (src_length > 60)
  230. src_length = 60;
  231. } else {
  232. /* To decode up to MMCOs */
  233. if (src_length > 1000)
  234. src_length = 1000;
  235. }
  236. break;
  237. }
  238. ptr = ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length);
  239. if (ptr == NULL || dst_length < 0)
  240. break;
  241. init_get_bits(&h->gb, ptr, 8 * dst_length);
  242. switch (h->nal_unit_type) {
  243. case NAL_SPS:
  244. ff_h264_decode_seq_parameter_set(h);
  245. break;
  246. case NAL_PPS:
  247. ff_h264_decode_picture_parameter_set(h, h->gb.size_in_bits);
  248. break;
  249. case NAL_SEI:
  250. ff_h264_decode_sei(h);
  251. break;
  252. case NAL_IDR_SLICE:
  253. s->key_frame = 1;
  254. h->prev_frame_num = 0;
  255. h->prev_frame_num_offset = 0;
  256. h->prev_poc_msb =
  257. h->prev_poc_lsb = 0;
  258. /* fall through */
  259. case NAL_SLICE:
  260. get_ue_golomb_long(&h->gb); // skip first_mb_in_slice
  261. slice_type = get_ue_golomb_31(&h->gb);
  262. s->pict_type = golomb_to_pict_type[slice_type % 5];
  263. if (h->sei_recovery_frame_cnt >= 0) {
  264. /* key frame, since recovery_frame_cnt is set */
  265. s->key_frame = 1;
  266. }
  267. pps_id = get_ue_golomb(&h->gb);
  268. if (pps_id >= MAX_PPS_COUNT) {
  269. av_log(h->avctx, AV_LOG_ERROR,
  270. "pps_id %u out of range\n", pps_id);
  271. return -1;
  272. }
  273. if (!h->pps_buffers[pps_id]) {
  274. av_log(h->avctx, AV_LOG_ERROR,
  275. "non-existing PPS %u referenced\n", pps_id);
  276. return -1;
  277. }
  278. h->pps = *h->pps_buffers[pps_id];
  279. if (!h->sps_buffers[h->pps.sps_id]) {
  280. av_log(h->avctx, AV_LOG_ERROR,
  281. "non-existing SPS %u referenced\n", h->pps.sps_id);
  282. return -1;
  283. }
  284. h->sps = *h->sps_buffers[h->pps.sps_id];
  285. h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num);
  286. if(h->sps.ref_frame_count <= 1 && h->pps.ref_count[0] <= 1 && s->pict_type == AV_PICTURE_TYPE_I)
  287. s->key_frame = 1;
  288. avctx->profile = ff_h264_get_profile(&h->sps);
  289. avctx->level = h->sps.level_idc;
  290. if (h->sps.frame_mbs_only_flag) {
  291. h->picture_structure = PICT_FRAME;
  292. } else {
  293. if (get_bits1(&h->gb)) { // field_pic_flag
  294. h->picture_structure = PICT_TOP_FIELD + get_bits1(&h->gb); // bottom_field_flag
  295. } else {
  296. h->picture_structure = PICT_FRAME;
  297. }
  298. }
  299. if (h->nal_unit_type == NAL_IDR_SLICE)
  300. get_ue_golomb(&h->gb); /* idr_pic_id */
  301. if (h->sps.poc_type == 0) {
  302. h->poc_lsb = get_bits(&h->gb, h->sps.log2_max_poc_lsb);
  303. if (h->pps.pic_order_present == 1 &&
  304. h->picture_structure == PICT_FRAME)
  305. h->delta_poc_bottom = get_se_golomb(&h->gb);
  306. }
  307. if (h->sps.poc_type == 1 &&
  308. !h->sps.delta_pic_order_always_zero_flag) {
  309. h->delta_poc[0] = get_se_golomb(&h->gb);
  310. if (h->pps.pic_order_present == 1 &&
  311. h->picture_structure == PICT_FRAME)
  312. h->delta_poc[1] = get_se_golomb(&h->gb);
  313. }
  314. /* Decode POC of this picture.
  315. * The prev_ values needed for decoding POC of the next picture are not set here. */
  316. field_poc[0] = field_poc[1] = INT_MAX;
  317. ff_init_poc(h, field_poc, &s->output_picture_number);
  318. /* Continue parsing to check if MMCO_RESET is present.
  319. * FIXME: MMCO_RESET could appear in non-first slice.
  320. * Maybe, we should parse all undisposable non-IDR slice of this
  321. * picture until encountering MMCO_RESET in a slice of it. */
  322. if (h->nal_ref_idc && h->nal_unit_type != NAL_IDR_SLICE) {
  323. got_reset = scan_mmco_reset(s);
  324. if (got_reset < 0)
  325. return got_reset;
  326. }
  327. /* Set up the prev_ values for decoding POC of the next picture. */
  328. h->prev_frame_num = got_reset ? 0 : h->frame_num;
  329. h->prev_frame_num_offset = got_reset ? 0 : h->frame_num_offset;
  330. if (h->nal_ref_idc != 0) {
  331. if (!got_reset) {
  332. h->prev_poc_msb = h->poc_msb;
  333. h->prev_poc_lsb = h->poc_lsb;
  334. } else {
  335. h->prev_poc_msb = 0;
  336. h->prev_poc_lsb =
  337. h->picture_structure == PICT_BOTTOM_FIELD ? 0 : field_poc[0];
  338. }
  339. }
  340. if (h->sps.pic_struct_present_flag) {
  341. switch (h->sei_pic_struct) {
  342. case SEI_PIC_STRUCT_TOP_FIELD:
  343. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  344. s->repeat_pict = 0;
  345. break;
  346. case SEI_PIC_STRUCT_FRAME:
  347. case SEI_PIC_STRUCT_TOP_BOTTOM:
  348. case SEI_PIC_STRUCT_BOTTOM_TOP:
  349. s->repeat_pict = 1;
  350. break;
  351. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  352. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  353. s->repeat_pict = 2;
  354. break;
  355. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  356. s->repeat_pict = 3;
  357. break;
  358. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  359. s->repeat_pict = 5;
  360. break;
  361. default:
  362. s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
  363. break;
  364. }
  365. } else {
  366. s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
  367. }
  368. if (h->picture_structure == PICT_FRAME) {
  369. s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
  370. if (h->sps.pic_struct_present_flag) {
  371. switch (h->sei_pic_struct) {
  372. case SEI_PIC_STRUCT_TOP_BOTTOM:
  373. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  374. s->field_order = AV_FIELD_TT;
  375. break;
  376. case SEI_PIC_STRUCT_BOTTOM_TOP:
  377. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  378. s->field_order = AV_FIELD_BB;
  379. break;
  380. default:
  381. s->field_order = AV_FIELD_PROGRESSIVE;
  382. break;
  383. }
  384. } else {
  385. if (field_poc[0] < field_poc[1])
  386. s->field_order = AV_FIELD_TT;
  387. else if (field_poc[0] > field_poc[1])
  388. s->field_order = AV_FIELD_BB;
  389. else
  390. s->field_order = AV_FIELD_PROGRESSIVE;
  391. }
  392. } else {
  393. if (h->picture_structure == PICT_TOP_FIELD)
  394. s->picture_structure = AV_PICTURE_STRUCTURE_TOP_FIELD;
  395. else
  396. s->picture_structure = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
  397. s->field_order = AV_FIELD_UNKNOWN;
  398. }
  399. return 0; /* no need to evaluate the rest */
  400. }
  401. buf += h->is_avc ? nalsize : consumed;
  402. }
  403. if (q264)
  404. return 0;
  405. /* didn't find a picture! */
  406. av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
  407. return -1;
  408. }
  409. static int h264_parse(AVCodecParserContext *s,
  410. AVCodecContext *avctx,
  411. const uint8_t **poutbuf, int *poutbuf_size,
  412. const uint8_t *buf, int buf_size)
  413. {
  414. H264Context *h = s->priv_data;
  415. ParseContext *pc = &h->parse_context;
  416. int next;
  417. if (!h->got_first) {
  418. h->got_first = 1;
  419. if (avctx->extradata_size) {
  420. h->avctx = avctx;
  421. // must be done like in decoder, otherwise opening the parser,
  422. // letting it create extradata and then closing and opening again
  423. // will cause has_b_frames to be always set.
  424. // Note that estimate_timings_from_pts does exactly this.
  425. if (!avctx->has_b_frames)
  426. h->low_delay = 1;
  427. ff_h264_decode_extradata(h, avctx->extradata, avctx->extradata_size);
  428. }
  429. }
  430. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  431. next = buf_size;
  432. } else {
  433. next = h264_find_frame_end(h, buf, buf_size);
  434. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  435. *poutbuf = NULL;
  436. *poutbuf_size = 0;
  437. return buf_size;
  438. }
  439. if (next < 0 && next != END_NOT_FOUND) {
  440. av_assert1(pc->last_index + next >= 0);
  441. h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next); // update state
  442. }
  443. }
  444. parse_nal_units(s, avctx, buf, buf_size);
  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_free(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. };