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.

411 lines
13KB

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