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.

391 lines
12KB

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