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.

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