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.

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