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.

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