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.

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