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.

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