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.

599 lines
20KB

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