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.

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