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.

512 lines
17KB

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