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.

531 lines
18KB

  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. avctx->profile = ff_h264_get_profile(&h->sps);
  252. avctx->level = h->sps.level_idc;
  253. if (h->sps.frame_mbs_only_flag) {
  254. h->picture_structure = PICT_FRAME;
  255. } else {
  256. if (get_bits1(&h->gb)) { // field_pic_flag
  257. h->picture_structure = PICT_TOP_FIELD + get_bits1(&h->gb); // bottom_field_flag
  258. } else {
  259. h->picture_structure = PICT_FRAME;
  260. }
  261. }
  262. if (h->nal_unit_type == NAL_IDR_SLICE)
  263. get_ue_golomb(&h->gb); /* idr_pic_id */
  264. if (h->sps.poc_type == 0) {
  265. h->poc_lsb = get_bits(&h->gb, h->sps.log2_max_poc_lsb);
  266. if (h->pps.pic_order_present == 1 &&
  267. h->picture_structure == PICT_FRAME)
  268. h->delta_poc_bottom = get_se_golomb(&h->gb);
  269. }
  270. if (h->sps.poc_type == 1 &&
  271. !h->sps.delta_pic_order_always_zero_flag) {
  272. h->delta_poc[0] = get_se_golomb(&h->gb);
  273. if (h->pps.pic_order_present == 1 &&
  274. h->picture_structure == PICT_FRAME)
  275. h->delta_poc[1] = get_se_golomb(&h->gb);
  276. }
  277. /* Decode POC of this picture.
  278. * The prev_ values needed for decoding POC of the next picture are not set here. */
  279. field_poc[0] = field_poc[1] = INT_MAX;
  280. ff_init_poc(h, field_poc, &s->output_picture_number);
  281. /* Continue parsing to check if MMCO_RESET is present.
  282. * FIXME: MMCO_RESET could appear in non-first slice.
  283. * Maybe, we should parse all undisposable non-IDR slice of this
  284. * picture until encountering MMCO_RESET in a slice of it. */
  285. if (h->nal_ref_idc && h->nal_unit_type != NAL_IDR_SLICE) {
  286. got_reset = scan_mmco_reset(s);
  287. if (got_reset < 0)
  288. return got_reset;
  289. }
  290. /* Set up the prev_ values for decoding POC of the next picture. */
  291. h->prev_frame_num = got_reset ? 0 : h->frame_num;
  292. h->prev_frame_num_offset = got_reset ? 0 : h->frame_num_offset;
  293. if (h->nal_ref_idc != 0) {
  294. if (!got_reset) {
  295. h->prev_poc_msb = h->poc_msb;
  296. h->prev_poc_lsb = h->poc_lsb;
  297. } else {
  298. h->prev_poc_msb = 0;
  299. h->prev_poc_lsb =
  300. h->picture_structure == PICT_BOTTOM_FIELD ? 0 : field_poc[0];
  301. }
  302. }
  303. if (h->sps.pic_struct_present_flag) {
  304. switch (h->sei_pic_struct) {
  305. case SEI_PIC_STRUCT_TOP_FIELD:
  306. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  307. s->repeat_pict = 0;
  308. break;
  309. case SEI_PIC_STRUCT_FRAME:
  310. case SEI_PIC_STRUCT_TOP_BOTTOM:
  311. case SEI_PIC_STRUCT_BOTTOM_TOP:
  312. s->repeat_pict = 1;
  313. break;
  314. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  315. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  316. s->repeat_pict = 2;
  317. break;
  318. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  319. s->repeat_pict = 3;
  320. break;
  321. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  322. s->repeat_pict = 5;
  323. break;
  324. default:
  325. s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
  326. break;
  327. }
  328. } else {
  329. s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
  330. }
  331. if (h->picture_structure == PICT_FRAME) {
  332. s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
  333. if (h->sps.pic_struct_present_flag) {
  334. switch (h->sei_pic_struct) {
  335. case SEI_PIC_STRUCT_TOP_BOTTOM:
  336. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  337. s->field_order = AV_FIELD_TT;
  338. break;
  339. case SEI_PIC_STRUCT_BOTTOM_TOP:
  340. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  341. s->field_order = AV_FIELD_BB;
  342. break;
  343. default:
  344. s->field_order = AV_FIELD_PROGRESSIVE;
  345. break;
  346. }
  347. } else {
  348. if (field_poc[0] < field_poc[1])
  349. s->field_order = AV_FIELD_TT;
  350. else if (field_poc[0] > field_poc[1])
  351. s->field_order = AV_FIELD_BB;
  352. else
  353. s->field_order = AV_FIELD_PROGRESSIVE;
  354. }
  355. } else {
  356. if (h->picture_structure == PICT_TOP_FIELD)
  357. s->picture_structure = AV_PICTURE_STRUCTURE_TOP_FIELD;
  358. else
  359. s->picture_structure = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
  360. s->field_order = AV_FIELD_UNKNOWN;
  361. }
  362. return 0; /* no need to evaluate the rest */
  363. }
  364. buf += consumed;
  365. }
  366. /* didn't find a picture! */
  367. av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
  368. return -1;
  369. }
  370. static int h264_parse(AVCodecParserContext *s,
  371. AVCodecContext *avctx,
  372. const uint8_t **poutbuf, int *poutbuf_size,
  373. const uint8_t *buf, int buf_size)
  374. {
  375. H264ParseContext *p = s->priv_data;
  376. H264Context *h = &p->h;
  377. ParseContext *pc = &p->pc;
  378. int next;
  379. if (!p->got_first) {
  380. p->got_first = 1;
  381. if (avctx->extradata_size) {
  382. h->avctx = avctx;
  383. // must be done like in the decoder.
  384. // otherwise opening the parser, creating extradata,
  385. // and then closing and opening again
  386. // will cause has_b_frames to be always set.
  387. // NB: estimate_timings_from_pts behaves exactly like this.
  388. if (!avctx->has_b_frames)
  389. h->low_delay = 1;
  390. ff_h264_decode_extradata(h);
  391. }
  392. }
  393. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  394. next = buf_size;
  395. } else {
  396. next = h264_find_frame_end(p, buf, buf_size);
  397. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  398. *poutbuf = NULL;
  399. *poutbuf_size = 0;
  400. return buf_size;
  401. }
  402. if (next < 0 && next != END_NOT_FOUND) {
  403. assert(pc->last_index + next >= 0);
  404. h264_find_frame_end(p, &pc->buffer[pc->last_index + next], -next); // update state
  405. }
  406. }
  407. parse_nal_units(s, avctx, buf, buf_size);
  408. if (h->sei_cpb_removal_delay >= 0) {
  409. s->dts_sync_point = h->sei_buffering_period_present;
  410. s->dts_ref_dts_delta = h->sei_cpb_removal_delay;
  411. s->pts_dts_delta = h->sei_dpb_output_delay;
  412. } else {
  413. s->dts_sync_point = INT_MIN;
  414. s->dts_ref_dts_delta = INT_MIN;
  415. s->pts_dts_delta = INT_MIN;
  416. }
  417. if (s->flags & PARSER_FLAG_ONCE) {
  418. s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
  419. }
  420. *poutbuf = buf;
  421. *poutbuf_size = buf_size;
  422. return next;
  423. }
  424. static int h264_split(AVCodecContext *avctx,
  425. const uint8_t *buf, int buf_size)
  426. {
  427. int i;
  428. uint32_t state = -1;
  429. int has_sps = 0;
  430. for (i = 0; i <= buf_size; i++) {
  431. if ((state & 0xFFFFFF1F) == 0x107)
  432. has_sps = 1;
  433. /* if((state&0xFFFFFF1F) == 0x101 ||
  434. * (state&0xFFFFFF1F) == 0x102 ||
  435. * (state&0xFFFFFF1F) == 0x105) {
  436. * }
  437. */
  438. if ((state & 0xFFFFFF00) == 0x100 && (state & 0xFFFFFF1F) != 0x106 &&
  439. (state & 0xFFFFFF1F) != 0x107 && (state & 0xFFFFFF1F) != 0x108 &&
  440. (state & 0xFFFFFF1F) != 0x109 && (state & 0xFFFFFF1F) != 0x10d &&
  441. (state & 0xFFFFFF1F) != 0x10f) {
  442. if (has_sps) {
  443. while (i > 4 && buf[i - 5] == 0)
  444. i--;
  445. return i - 4;
  446. }
  447. }
  448. if (i < buf_size)
  449. state = (state << 8) | buf[i];
  450. }
  451. return 0;
  452. }
  453. static void close(AVCodecParserContext *s)
  454. {
  455. H264ParseContext *p = s->priv_data;
  456. H264Context *h = &p->h;
  457. ParseContext *pc = &p->pc;
  458. av_free(pc->buffer);
  459. ff_h264_free_context(h);
  460. }
  461. static av_cold int init(AVCodecParserContext *s)
  462. {
  463. H264ParseContext *p = s->priv_data;
  464. H264Context *h = &p->h;
  465. h->thread_context[0] = h;
  466. h->slice_context_count = 1;
  467. ff_h264dsp_init(&h->h264dsp, 8, 1);
  468. return 0;
  469. }
  470. AVCodecParser ff_h264_parser = {
  471. .codec_ids = { AV_CODEC_ID_H264 },
  472. .priv_data_size = sizeof(H264ParseContext),
  473. .parser_init = init,
  474. .parser_parse = h264_parse,
  475. .parser_close = close,
  476. .split = h264_split,
  477. };