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.

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