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.

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