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.

614 lines
21KB

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