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.

431 lines
14KB

  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. #if HAVE_FAST_UNALIGNED
  45. /* we check i < buf_size instead of i + 3 / 7 because it is
  46. * simpler and there must be FF_INPUT_BUFFER_PADDING_SIZE
  47. * bytes at the end.
  48. */
  49. #if HAVE_FAST_64BIT
  50. while (i < buf_size &&
  51. !((~*(const uint64_t *)(buf + i) &
  52. (*(const uint64_t *)(buf + i) - 0x0101010101010101ULL)) &
  53. 0x8080808080808080ULL))
  54. i += 8;
  55. #else
  56. while (i < buf_size &&
  57. !((~*(const uint32_t *)(buf + i) &
  58. (*(const uint32_t *)(buf + i) - 0x01010101U)) &
  59. 0x80808080U))
  60. i += 4;
  61. #endif
  62. #endif
  63. for (; i < buf_size; i++)
  64. if (!buf[i]) {
  65. state = 2;
  66. break;
  67. }
  68. } else if (state <= 2) {
  69. if (buf[i] == 1)
  70. state ^= 5; // 2->7, 1->4, 0->5
  71. else if (buf[i])
  72. state = 7;
  73. else
  74. state >>= 1; // 2->1, 1->0, 0->0
  75. } else if (state <= 5) {
  76. int v = buf[i] & 0x1F;
  77. if (v == 6 || v == 7 || v == 8 || v == 9) {
  78. if (pc->frame_start_found) {
  79. i++;
  80. goto found;
  81. }
  82. } else if (v == 1 || v == 2 || v == 5) {
  83. if (pc->frame_start_found) {
  84. state += 8;
  85. continue;
  86. } else
  87. pc->frame_start_found = 1;
  88. }
  89. state = 7;
  90. } else {
  91. if (buf[i] & 0x80)
  92. goto found;
  93. state = 7;
  94. }
  95. }
  96. pc->state = state;
  97. return END_NOT_FOUND;
  98. found:
  99. pc->state = 7;
  100. pc->frame_start_found = 0;
  101. return i - (state & 5);
  102. }
  103. /**
  104. * Parse NAL units of found picture and decode some basic information.
  105. *
  106. * @param s parser context.
  107. * @param avctx codec context.
  108. * @param buf buffer with field/frame data.
  109. * @param buf_size size of the buffer.
  110. */
  111. static inline int parse_nal_units(AVCodecParserContext *s,
  112. AVCodecContext *avctx,
  113. const uint8_t *buf, int buf_size)
  114. {
  115. H264Context *h = s->priv_data;
  116. const uint8_t *buf_end = buf + buf_size;
  117. unsigned int pps_id;
  118. unsigned int slice_type;
  119. int state = -1;
  120. const uint8_t *ptr;
  121. int field_poc[2];
  122. /* set some sane default values */
  123. s->pict_type = AV_PICTURE_TYPE_I;
  124. s->key_frame = 0;
  125. s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
  126. h->avctx = avctx;
  127. h->sei_recovery_frame_cnt = -1;
  128. h->sei_dpb_output_delay = 0;
  129. h->sei_cpb_removal_delay = -1;
  130. h->sei_buffering_period_present = 0;
  131. if (!buf_size)
  132. return 0;
  133. for (;;) {
  134. int src_length, dst_length, consumed;
  135. buf = avpriv_find_start_code(buf, buf_end, &state);
  136. if (buf >= buf_end)
  137. break;
  138. --buf;
  139. src_length = buf_end - buf;
  140. switch (state & 0x1f) {
  141. case NAL_SLICE:
  142. case NAL_IDR_SLICE:
  143. // Do not walk the whole buffer just to decode slice header
  144. if (src_length > 20)
  145. src_length = 20;
  146. break;
  147. }
  148. ptr = ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length);
  149. if (ptr == NULL || dst_length < 0)
  150. break;
  151. init_get_bits(&h->gb, ptr, 8 * dst_length);
  152. switch (h->nal_unit_type) {
  153. case NAL_SPS:
  154. ff_h264_decode_seq_parameter_set(h);
  155. break;
  156. case NAL_PPS:
  157. ff_h264_decode_picture_parameter_set(h, h->gb.size_in_bits);
  158. break;
  159. case NAL_SEI:
  160. ff_h264_decode_sei(h);
  161. break;
  162. case NAL_IDR_SLICE:
  163. s->key_frame = 1;
  164. h->prev_frame_num = 0;
  165. h->prev_frame_num_offset = 0;
  166. h->prev_poc_msb =
  167. h->prev_poc_lsb = 0;
  168. /* fall through */
  169. case NAL_SLICE:
  170. get_ue_golomb(&h->gb); // skip first_mb_in_slice
  171. slice_type = get_ue_golomb_31(&h->gb);
  172. s->pict_type = golomb_to_pict_type[slice_type % 5];
  173. if (h->sei_recovery_frame_cnt >= 0) {
  174. /* key frame, since recovery_frame_cnt is set */
  175. s->key_frame = 1;
  176. }
  177. pps_id = get_ue_golomb(&h->gb);
  178. if (pps_id >= MAX_PPS_COUNT) {
  179. av_log(h->avctx, AV_LOG_ERROR,
  180. "pps_id out of range\n");
  181. return -1;
  182. }
  183. if (!h->pps_buffers[pps_id]) {
  184. av_log(h->avctx, AV_LOG_ERROR,
  185. "non-existing PPS referenced\n");
  186. return -1;
  187. }
  188. h->pps = *h->pps_buffers[pps_id];
  189. if (!h->sps_buffers[h->pps.sps_id]) {
  190. av_log(h->avctx, AV_LOG_ERROR,
  191. "non-existing SPS referenced\n");
  192. return -1;
  193. }
  194. h->sps = *h->sps_buffers[h->pps.sps_id];
  195. h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num);
  196. avctx->profile = ff_h264_get_profile(&h->sps);
  197. avctx->level = h->sps.level_idc;
  198. if (h->sps.frame_mbs_only_flag) {
  199. h->picture_structure = PICT_FRAME;
  200. } else {
  201. if (get_bits1(&h->gb)) { // field_pic_flag
  202. h->picture_structure = PICT_TOP_FIELD + get_bits1(&h->gb); // bottom_field_flag
  203. } else {
  204. h->picture_structure = PICT_FRAME;
  205. }
  206. }
  207. if (h->nal_unit_type == NAL_IDR_SLICE)
  208. get_ue_golomb(&h->gb); /* idr_pic_id */
  209. if (h->sps.poc_type == 0) {
  210. h->poc_lsb = get_bits(&h->gb, h->sps.log2_max_poc_lsb);
  211. if (h->pps.pic_order_present == 1 &&
  212. h->picture_structure == PICT_FRAME)
  213. h->delta_poc_bottom = get_se_golomb(&h->gb);
  214. }
  215. if (h->sps.poc_type == 1 &&
  216. !h->sps.delta_pic_order_always_zero_flag) {
  217. h->delta_poc[0] = get_se_golomb(&h->gb);
  218. if (h->pps.pic_order_present == 1 &&
  219. h->picture_structure == PICT_FRAME)
  220. h->delta_poc[1] = get_se_golomb(&h->gb);
  221. }
  222. ff_init_poc(h, field_poc, NULL);
  223. if (h->sps.pic_struct_present_flag) {
  224. switch (h->sei_pic_struct) {
  225. case SEI_PIC_STRUCT_TOP_FIELD:
  226. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  227. s->repeat_pict = 0;
  228. break;
  229. case SEI_PIC_STRUCT_FRAME:
  230. case SEI_PIC_STRUCT_TOP_BOTTOM:
  231. case SEI_PIC_STRUCT_BOTTOM_TOP:
  232. s->repeat_pict = 1;
  233. break;
  234. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  235. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  236. s->repeat_pict = 2;
  237. break;
  238. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  239. s->repeat_pict = 3;
  240. break;
  241. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  242. s->repeat_pict = 5;
  243. break;
  244. default:
  245. s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
  246. break;
  247. }
  248. } else {
  249. s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
  250. }
  251. if (h->picture_structure == PICT_FRAME) {
  252. s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
  253. if (h->sps.pic_struct_present_flag) {
  254. switch (h->sei_pic_struct) {
  255. case SEI_PIC_STRUCT_TOP_BOTTOM:
  256. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  257. s->field_order = AV_FIELD_TT;
  258. break;
  259. case SEI_PIC_STRUCT_BOTTOM_TOP:
  260. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  261. s->field_order = AV_FIELD_BB;
  262. break;
  263. default:
  264. s->field_order = AV_FIELD_PROGRESSIVE;
  265. break;
  266. }
  267. } else {
  268. if (field_poc[0] < field_poc[1])
  269. s->field_order = AV_FIELD_TT;
  270. else if (field_poc[0] > field_poc[1])
  271. s->field_order = AV_FIELD_BB;
  272. else
  273. s->field_order = AV_FIELD_PROGRESSIVE;
  274. }
  275. } else {
  276. if (h->picture_structure == PICT_TOP_FIELD)
  277. s->picture_structure = AV_PICTURE_STRUCTURE_TOP_FIELD;
  278. else
  279. s->picture_structure = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
  280. s->field_order = AV_FIELD_UNKNOWN;
  281. }
  282. return 0; /* no need to evaluate the rest */
  283. }
  284. buf += consumed;
  285. }
  286. /* didn't find a picture! */
  287. av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
  288. return -1;
  289. }
  290. static int h264_parse(AVCodecParserContext *s,
  291. AVCodecContext *avctx,
  292. const uint8_t **poutbuf, int *poutbuf_size,
  293. const uint8_t *buf, int buf_size)
  294. {
  295. H264Context *h = s->priv_data;
  296. ParseContext *pc = &h->parse_context;
  297. int next;
  298. if (!h->got_first) {
  299. h->got_first = 1;
  300. if (avctx->extradata_size) {
  301. h->avctx = avctx;
  302. // must be done like in the decoder.
  303. // otherwise opening the parser, creating extradata,
  304. // and then closing and opening again
  305. // will cause has_b_frames to be always set.
  306. // NB: estimate_timings_from_pts behaves exactly like this.
  307. if (!avctx->has_b_frames)
  308. h->low_delay = 1;
  309. ff_h264_decode_extradata(h);
  310. }
  311. }
  312. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  313. next = buf_size;
  314. } else {
  315. next = h264_find_frame_end(h, buf, buf_size);
  316. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  317. *poutbuf = NULL;
  318. *poutbuf_size = 0;
  319. return buf_size;
  320. }
  321. if (next < 0 && next != END_NOT_FOUND) {
  322. assert(pc->last_index + next >= 0);
  323. h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next); // update state
  324. }
  325. }
  326. parse_nal_units(s, avctx, buf, buf_size);
  327. if (h->sei_cpb_removal_delay >= 0) {
  328. s->dts_sync_point = h->sei_buffering_period_present;
  329. s->dts_ref_dts_delta = h->sei_cpb_removal_delay;
  330. s->pts_dts_delta = h->sei_dpb_output_delay;
  331. } else {
  332. s->dts_sync_point = INT_MIN;
  333. s->dts_ref_dts_delta = INT_MIN;
  334. s->pts_dts_delta = INT_MIN;
  335. }
  336. if (s->flags & PARSER_FLAG_ONCE) {
  337. s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
  338. }
  339. *poutbuf = buf;
  340. *poutbuf_size = buf_size;
  341. return next;
  342. }
  343. static int h264_split(AVCodecContext *avctx,
  344. const uint8_t *buf, int buf_size)
  345. {
  346. int i;
  347. uint32_t state = -1;
  348. int has_sps = 0;
  349. for (i = 0; i <= buf_size; i++) {
  350. if ((state & 0xFFFFFF1F) == 0x107)
  351. has_sps = 1;
  352. /* if((state&0xFFFFFF1F) == 0x101 ||
  353. * (state&0xFFFFFF1F) == 0x102 ||
  354. * (state&0xFFFFFF1F) == 0x105) {
  355. * }
  356. */
  357. if ((state & 0xFFFFFF00) == 0x100 && (state & 0xFFFFFF1F) != 0x107 &&
  358. (state & 0xFFFFFF1F) != 0x108 && (state & 0xFFFFFF1F) != 0x109) {
  359. if (has_sps) {
  360. while (i > 4 && buf[i - 5] == 0)
  361. i--;
  362. return i - 4;
  363. }
  364. }
  365. if (i < buf_size)
  366. state = (state << 8) | buf[i];
  367. }
  368. return 0;
  369. }
  370. static void close(AVCodecParserContext *s)
  371. {
  372. H264Context *h = s->priv_data;
  373. ParseContext *pc = &h->parse_context;
  374. av_free(pc->buffer);
  375. ff_h264_free_context(h);
  376. }
  377. static av_cold int init(AVCodecParserContext *s)
  378. {
  379. H264Context *h = s->priv_data;
  380. h->thread_context[0] = h;
  381. h->slice_context_count = 1;
  382. return 0;
  383. }
  384. AVCodecParser ff_h264_parser = {
  385. .codec_ids = { AV_CODEC_ID_H264 },
  386. .priv_data_size = sizeof(H264Context),
  387. .parser_init = init,
  388. .parser_parse = h264_parse,
  389. .parser_close = close,
  390. .split = h264_split,
  391. };