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. ff_h264_reset_sei(h);
  195. h->sei_fpa.frame_packing_arrangement_cancel_flag = -1;
  196. if (!buf_size)
  197. return 0;
  198. for (;;) {
  199. int src_length, dst_length, consumed, nalsize = 0;
  200. if (h->is_avc) {
  201. int i;
  202. if (h->nal_length_size >= buf_end - buf) break;
  203. nalsize = 0;
  204. for (i = 0; i < h->nal_length_size; i++)
  205. nalsize = (nalsize << 8) | *buf++;
  206. if (nalsize <= 0 || nalsize > buf_end - buf) {
  207. av_log(h->avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  208. break;
  209. }
  210. src_length = nalsize;
  211. } else {
  212. buf = avpriv_find_start_code(buf, buf_end, &state);
  213. if (buf >= buf_end)
  214. break;
  215. --buf;
  216. src_length = buf_end - buf;
  217. }
  218. switch (state & 0x1f) {
  219. case NAL_SLICE:
  220. case NAL_IDR_SLICE:
  221. // Do not walk the whole buffer just to decode slice header
  222. if ((state & 0x1f) == NAL_IDR_SLICE || ((state >> 5) & 0x3) == 0) {
  223. /* IDR or disposable slice
  224. * No need to decode many bytes because MMCOs shall not be present. */
  225. if (src_length > 60)
  226. src_length = 60;
  227. } else {
  228. /* To decode up to MMCOs */
  229. if (src_length > 1000)
  230. src_length = 1000;
  231. }
  232. break;
  233. }
  234. ptr = ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length);
  235. if (ptr == NULL || dst_length < 0)
  236. break;
  237. init_get_bits(&h->gb, ptr, 8 * dst_length);
  238. switch (h->nal_unit_type) {
  239. case NAL_SPS:
  240. ff_h264_decode_seq_parameter_set(h);
  241. break;
  242. case NAL_PPS:
  243. ff_h264_decode_picture_parameter_set(h, h->gb.size_in_bits);
  244. break;
  245. case NAL_SEI:
  246. ff_h264_decode_sei(h);
  247. break;
  248. case NAL_IDR_SLICE:
  249. s->key_frame = 1;
  250. h->prev_frame_num = 0;
  251. h->prev_frame_num_offset = 0;
  252. h->prev_poc_msb =
  253. h->prev_poc_lsb = 0;
  254. /* fall through */
  255. case NAL_SLICE:
  256. get_ue_golomb_long(&h->gb); // skip first_mb_in_slice
  257. slice_type = get_ue_golomb_31(&h->gb);
  258. s->pict_type = golomb_to_pict_type[slice_type % 5];
  259. if (h->sei_recovery_frame_cnt >= 0) {
  260. /* key frame, since recovery_frame_cnt is set */
  261. s->key_frame = 1;
  262. }
  263. pps_id = get_ue_golomb(&h->gb);
  264. if (pps_id >= MAX_PPS_COUNT) {
  265. av_log(h->avctx, AV_LOG_ERROR,
  266. "pps_id out of range\n");
  267. return -1;
  268. }
  269. if (!h->pps_buffers[pps_id]) {
  270. av_log(h->avctx, AV_LOG_ERROR,
  271. "non-existing PPS referenced\n");
  272. return -1;
  273. }
  274. h->pps = *h->pps_buffers[pps_id];
  275. if (!h->sps_buffers[h->pps.sps_id]) {
  276. av_log(h->avctx, AV_LOG_ERROR,
  277. "non-existing SPS referenced\n");
  278. return -1;
  279. }
  280. h->sps = *h->sps_buffers[h->pps.sps_id];
  281. h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num);
  282. if(h->sps.ref_frame_count <= 1 && h->pps.ref_count[0] <= 1 && s->pict_type == AV_PICTURE_TYPE_I)
  283. s->key_frame = 1;
  284. avctx->profile = ff_h264_get_profile(&h->sps);
  285. avctx->level = h->sps.level_idc;
  286. if (h->sps.frame_mbs_only_flag) {
  287. h->picture_structure = PICT_FRAME;
  288. } else {
  289. if (get_bits1(&h->gb)) { // field_pic_flag
  290. h->picture_structure = PICT_TOP_FIELD + get_bits1(&h->gb); // bottom_field_flag
  291. } else {
  292. h->picture_structure = PICT_FRAME;
  293. }
  294. }
  295. if (h->nal_unit_type == NAL_IDR_SLICE)
  296. get_ue_golomb(&h->gb); /* idr_pic_id */
  297. if (h->sps.poc_type == 0) {
  298. h->poc_lsb = get_bits(&h->gb, h->sps.log2_max_poc_lsb);
  299. if (h->pps.pic_order_present == 1 &&
  300. h->picture_structure == PICT_FRAME)
  301. h->delta_poc_bottom = get_se_golomb(&h->gb);
  302. }
  303. if (h->sps.poc_type == 1 &&
  304. !h->sps.delta_pic_order_always_zero_flag) {
  305. h->delta_poc[0] = get_se_golomb(&h->gb);
  306. if (h->pps.pic_order_present == 1 &&
  307. h->picture_structure == PICT_FRAME)
  308. h->delta_poc[1] = get_se_golomb(&h->gb);
  309. }
  310. /* Decode POC of this picture.
  311. * The prev_ values needed for decoding POC of the next picture are not set here. */
  312. field_poc[0] = field_poc[1] = INT_MAX;
  313. ff_init_poc(h, field_poc, &s->output_picture_number);
  314. /* Continue parsing to check if MMCO_RESET is present.
  315. * FIXME: MMCO_RESET could appear in non-first slice.
  316. * Maybe, we should parse all undisposable non-IDR slice of this
  317. * picture until encountering MMCO_RESET in a slice of it. */
  318. if (h->nal_ref_idc && h->nal_unit_type != NAL_IDR_SLICE) {
  319. got_reset = scan_mmco_reset(s);
  320. if (got_reset < 0)
  321. return got_reset;
  322. }
  323. /* Set up the prev_ values for decoding POC of the next picture. */
  324. h->prev_frame_num = got_reset ? 0 : h->frame_num;
  325. h->prev_frame_num_offset = got_reset ? 0 : h->frame_num_offset;
  326. if (h->nal_ref_idc != 0) {
  327. if (!got_reset) {
  328. h->prev_poc_msb = h->poc_msb;
  329. h->prev_poc_lsb = h->poc_lsb;
  330. } else {
  331. h->prev_poc_msb = 0;
  332. h->prev_poc_lsb =
  333. h->picture_structure == PICT_BOTTOM_FIELD ? 0 : field_poc[0];
  334. }
  335. }
  336. if (h->sps.pic_struct_present_flag) {
  337. switch (h->sei_pic_struct) {
  338. case SEI_PIC_STRUCT_TOP_FIELD:
  339. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  340. s->repeat_pict = 0;
  341. break;
  342. case SEI_PIC_STRUCT_FRAME:
  343. case SEI_PIC_STRUCT_TOP_BOTTOM:
  344. case SEI_PIC_STRUCT_BOTTOM_TOP:
  345. s->repeat_pict = 1;
  346. break;
  347. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  348. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  349. s->repeat_pict = 2;
  350. break;
  351. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  352. s->repeat_pict = 3;
  353. break;
  354. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  355. s->repeat_pict = 5;
  356. break;
  357. default:
  358. s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
  359. break;
  360. }
  361. } else {
  362. s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
  363. }
  364. if (h->picture_structure == PICT_FRAME) {
  365. s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
  366. if (h->sps.pic_struct_present_flag) {
  367. switch (h->sei_pic_struct) {
  368. case SEI_PIC_STRUCT_TOP_BOTTOM:
  369. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  370. s->field_order = AV_FIELD_TT;
  371. break;
  372. case SEI_PIC_STRUCT_BOTTOM_TOP:
  373. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  374. s->field_order = AV_FIELD_BB;
  375. break;
  376. default:
  377. s->field_order = AV_FIELD_PROGRESSIVE;
  378. break;
  379. }
  380. } else {
  381. if (field_poc[0] < field_poc[1])
  382. s->field_order = AV_FIELD_TT;
  383. else if (field_poc[0] > field_poc[1])
  384. s->field_order = AV_FIELD_BB;
  385. else
  386. s->field_order = AV_FIELD_PROGRESSIVE;
  387. }
  388. } else {
  389. if (h->picture_structure == PICT_TOP_FIELD)
  390. s->picture_structure = AV_PICTURE_STRUCTURE_TOP_FIELD;
  391. else
  392. s->picture_structure = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
  393. s->field_order = AV_FIELD_UNKNOWN;
  394. }
  395. return 0; /* no need to evaluate the rest */
  396. }
  397. buf += h->is_avc ? nalsize : consumed;
  398. }
  399. if (q264)
  400. return 0;
  401. /* didn't find a picture! */
  402. av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
  403. return -1;
  404. }
  405. static int h264_parse(AVCodecParserContext *s,
  406. AVCodecContext *avctx,
  407. const uint8_t **poutbuf, int *poutbuf_size,
  408. const uint8_t *buf, int buf_size)
  409. {
  410. H264Context *h = s->priv_data;
  411. ParseContext *pc = &h->parse_context;
  412. int next;
  413. if (!h->got_first) {
  414. h->got_first = 1;
  415. if (avctx->extradata_size) {
  416. h->avctx = avctx;
  417. // must be done like in decoder, otherwise opening the parser,
  418. // letting it create extradata and then closing and opening again
  419. // will cause has_b_frames to be always set.
  420. // Note that estimate_timings_from_pts does exactly this.
  421. if (!avctx->has_b_frames)
  422. h->low_delay = 1;
  423. ff_h264_decode_extradata(h, avctx->extradata, avctx->extradata_size);
  424. }
  425. }
  426. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  427. next = buf_size;
  428. } else {
  429. next = h264_find_frame_end(h, buf, buf_size);
  430. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  431. *poutbuf = NULL;
  432. *poutbuf_size = 0;
  433. return buf_size;
  434. }
  435. if (next < 0 && next != END_NOT_FOUND) {
  436. av_assert1(pc->last_index + next >= 0);
  437. h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next); // update state
  438. }
  439. }
  440. parse_nal_units(s, avctx, buf, buf_size);
  441. if (h->sei_cpb_removal_delay >= 0) {
  442. s->dts_sync_point = h->sei_buffering_period_present;
  443. s->dts_ref_dts_delta = h->sei_cpb_removal_delay;
  444. s->pts_dts_delta = h->sei_dpb_output_delay;
  445. } else {
  446. s->dts_sync_point = INT_MIN;
  447. s->dts_ref_dts_delta = INT_MIN;
  448. s->pts_dts_delta = INT_MIN;
  449. }
  450. if (s->flags & PARSER_FLAG_ONCE) {
  451. s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
  452. }
  453. *poutbuf = buf;
  454. *poutbuf_size = buf_size;
  455. return next;
  456. }
  457. static int h264_split(AVCodecContext *avctx,
  458. const uint8_t *buf, int buf_size)
  459. {
  460. int i;
  461. uint32_t state = -1;
  462. int has_sps = 0;
  463. for (i = 0; i <= buf_size; i++) {
  464. if ((state & 0xFFFFFF1F) == 0x107)
  465. has_sps = 1;
  466. /* if ((state&0xFFFFFF1F) == 0x101 ||
  467. * (state&0xFFFFFF1F) == 0x102 ||
  468. * (state&0xFFFFFF1F) == 0x105) {
  469. * }
  470. */
  471. if ((state & 0xFFFFFF00) == 0x100 && (state & 0xFFFFFF1F) != 0x107 &&
  472. (state & 0xFFFFFF1F) != 0x108 && (state & 0xFFFFFF1F) != 0x109) {
  473. if (has_sps) {
  474. while (i > 4 && buf[i - 5] == 0)
  475. i--;
  476. return i - 4;
  477. }
  478. }
  479. if (i < buf_size)
  480. state = (state << 8) | buf[i];
  481. }
  482. return 0;
  483. }
  484. static void close(AVCodecParserContext *s)
  485. {
  486. H264Context *h = s->priv_data;
  487. ParseContext *pc = &h->parse_context;
  488. av_free(pc->buffer);
  489. ff_h264_free_context(h);
  490. }
  491. static av_cold int init(AVCodecParserContext *s)
  492. {
  493. H264Context *h = s->priv_data;
  494. h->thread_context[0] = h;
  495. h->slice_context_count = 1;
  496. ff_h264dsp_init(&h->h264dsp, 8, 1);
  497. return 0;
  498. }
  499. AVCodecParser ff_h264_parser = {
  500. .codec_ids = { AV_CODEC_ID_H264 },
  501. .priv_data_size = sizeof(H264Context),
  502. .parser_init = init,
  503. .parser_parse = h264_parse,
  504. .parser_close = close,
  505. .split = h264_split,
  506. };