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.

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