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.

400 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. //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. int q264 = buf_size >=4 && !memcmp("Q264", buf, 4);
  140. /* set some sane default values */
  141. s->pict_type = AV_PICTURE_TYPE_I;
  142. s->key_frame = 0;
  143. h->s.avctx= avctx;
  144. h->sei_recovery_frame_cnt = -1;
  145. h->sei_dpb_output_delay = 0;
  146. h->sei_cpb_removal_delay = -1;
  147. h->sei_buffering_period_present = 0;
  148. if (!buf_size)
  149. return 0;
  150. for(;;) {
  151. int src_length, dst_length, consumed, nalsize = 0;
  152. if (h->is_avc) {
  153. int i;
  154. if (h->nal_length_size >= buf_end - buf) break;
  155. nalsize = 0;
  156. for (i = 0; i < h->nal_length_size; i++)
  157. nalsize = (nalsize << 8) | *buf++;
  158. if (nalsize <= 0 || nalsize > buf_end - buf) {
  159. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  160. break;
  161. }
  162. src_length = nalsize;
  163. } else {
  164. buf = avpriv_mpv_find_start_code(buf, buf_end, &state);
  165. if(buf >= buf_end)
  166. break;
  167. --buf;
  168. src_length = buf_end - buf;
  169. }
  170. switch (state & 0x1f) {
  171. case NAL_SLICE:
  172. case NAL_IDR_SLICE:
  173. // Do not walk the whole buffer just to decode slice header
  174. if (src_length > 20)
  175. src_length = 20;
  176. break;
  177. }
  178. ptr= ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length);
  179. if (ptr==NULL || dst_length < 0)
  180. break;
  181. init_get_bits(&h->s.gb, ptr, 8*dst_length);
  182. switch(h->nal_unit_type) {
  183. case NAL_SPS:
  184. ff_h264_decode_seq_parameter_set(h);
  185. break;
  186. case NAL_PPS:
  187. ff_h264_decode_picture_parameter_set(h, h->s.gb.size_in_bits);
  188. break;
  189. case NAL_SEI:
  190. ff_h264_decode_sei(h);
  191. break;
  192. case NAL_IDR_SLICE:
  193. s->key_frame = 1;
  194. /* fall through */
  195. case NAL_SLICE:
  196. get_ue_golomb_long(&h->s.gb); // skip first_mb_in_slice
  197. slice_type = get_ue_golomb_31(&h->s.gb);
  198. s->pict_type = golomb_to_pict_type[slice_type % 5];
  199. if (h->sei_recovery_frame_cnt >= 0) {
  200. /* key frame, since recovery_frame_cnt is set */
  201. s->key_frame = 1;
  202. }
  203. pps_id= get_ue_golomb(&h->s.gb);
  204. if(pps_id>=MAX_PPS_COUNT) {
  205. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  206. return -1;
  207. }
  208. if(!h->pps_buffers[pps_id]) {
  209. av_log(h->s.avctx, AV_LOG_ERROR, "non-existing PPS referenced\n");
  210. return -1;
  211. }
  212. h->pps= *h->pps_buffers[pps_id];
  213. if(!h->sps_buffers[h->pps.sps_id]) {
  214. av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS referenced\n");
  215. return -1;
  216. }
  217. h->sps = *h->sps_buffers[h->pps.sps_id];
  218. h->frame_num = get_bits(&h->s.gb, h->sps.log2_max_frame_num);
  219. avctx->profile = ff_h264_get_profile(&h->sps);
  220. avctx->level = h->sps.level_idc;
  221. if(h->sps.frame_mbs_only_flag){
  222. h->s.picture_structure= PICT_FRAME;
  223. }else{
  224. if(get_bits1(&h->s.gb)) { //field_pic_flag
  225. h->s.picture_structure= PICT_TOP_FIELD + get_bits1(&h->s.gb); //bottom_field_flag
  226. } else {
  227. h->s.picture_structure= PICT_FRAME;
  228. }
  229. }
  230. if(h->sps.pic_struct_present_flag) {
  231. switch (h->sei_pic_struct) {
  232. case SEI_PIC_STRUCT_TOP_FIELD:
  233. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  234. s->repeat_pict = 0;
  235. break;
  236. case SEI_PIC_STRUCT_FRAME:
  237. case SEI_PIC_STRUCT_TOP_BOTTOM:
  238. case SEI_PIC_STRUCT_BOTTOM_TOP:
  239. s->repeat_pict = 1;
  240. break;
  241. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  242. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  243. s->repeat_pict = 2;
  244. break;
  245. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  246. s->repeat_pict = 3;
  247. break;
  248. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  249. s->repeat_pict = 5;
  250. break;
  251. default:
  252. s->repeat_pict = h->s.picture_structure == PICT_FRAME ? 1 : 0;
  253. break;
  254. }
  255. } else {
  256. s->repeat_pict = h->s.picture_structure == PICT_FRAME ? 1 : 0;
  257. }
  258. return 0; /* no need to evaluate the rest */
  259. }
  260. buf += h->is_avc ? nalsize : consumed;
  261. }
  262. if (q264)
  263. return 0;
  264. /* didn't find a picture! */
  265. av_log(h->s.avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
  266. return -1;
  267. }
  268. static int h264_parse(AVCodecParserContext *s,
  269. AVCodecContext *avctx,
  270. const uint8_t **poutbuf, int *poutbuf_size,
  271. const uint8_t *buf, int buf_size)
  272. {
  273. H264Context *h = s->priv_data;
  274. ParseContext *pc = &h->s.parse_context;
  275. int next;
  276. if (!h->got_first) {
  277. h->got_first = 1;
  278. if (avctx->extradata_size) {
  279. h->s.avctx = avctx;
  280. // must be done like in decoder, otherwise opening the parser,
  281. // letting it create extradata and then closing and opening again
  282. // will cause has_b_frames to be always set.
  283. // Note that estimate_timings_from_pts does exactly this.
  284. if (!avctx->has_b_frames)
  285. h->s.low_delay = 1;
  286. ff_h264_decode_extradata(h, avctx->extradata, avctx->extradata_size);
  287. }
  288. }
  289. if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
  290. next= buf_size;
  291. }else{
  292. next= ff_h264_find_frame_end(h, buf, buf_size);
  293. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  294. *poutbuf = NULL;
  295. *poutbuf_size = 0;
  296. return buf_size;
  297. }
  298. if(next<0 && next != END_NOT_FOUND){
  299. av_assert1(pc->last_index + next >= 0 );
  300. ff_h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next); //update state
  301. }
  302. }
  303. parse_nal_units(s, avctx, buf, buf_size);
  304. if (h->sei_cpb_removal_delay >= 0) {
  305. s->dts_sync_point = h->sei_buffering_period_present;
  306. s->dts_ref_dts_delta = h->sei_cpb_removal_delay;
  307. s->pts_dts_delta = h->sei_dpb_output_delay;
  308. } else {
  309. s->dts_sync_point = INT_MIN;
  310. s->dts_ref_dts_delta = INT_MIN;
  311. s->pts_dts_delta = INT_MIN;
  312. }
  313. if (s->flags & PARSER_FLAG_ONCE) {
  314. s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
  315. }
  316. *poutbuf = buf;
  317. *poutbuf_size = buf_size;
  318. return next;
  319. }
  320. static int h264_split(AVCodecContext *avctx,
  321. const uint8_t *buf, int buf_size)
  322. {
  323. int i;
  324. uint32_t state = -1;
  325. int has_sps= 0;
  326. for(i=0; i<=buf_size; i++){
  327. if((state&0xFFFFFF1F) == 0x107)
  328. has_sps=1;
  329. /* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  330. }*/
  331. if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){
  332. if(has_sps){
  333. while(i>4 && buf[i-5]==0) i--;
  334. return i-4;
  335. }
  336. }
  337. if (i<buf_size)
  338. state= (state<<8) | buf[i];
  339. }
  340. return 0;
  341. }
  342. static void close(AVCodecParserContext *s)
  343. {
  344. H264Context *h = s->priv_data;
  345. ParseContext *pc = &h->s.parse_context;
  346. av_free(pc->buffer);
  347. ff_h264_free_context(h);
  348. }
  349. static int init(AVCodecParserContext *s)
  350. {
  351. H264Context *h = s->priv_data;
  352. h->thread_context[0] = h;
  353. h->s.slice_context_count = 1;
  354. return 0;
  355. }
  356. AVCodecParser ff_h264_parser = {
  357. .codec_ids = { AV_CODEC_ID_H264 },
  358. .priv_data_size = sizeof(H264Context),
  359. .parser_init = init,
  360. .parser_parse = h264_parse,
  361. .parser_close = close,
  362. .split = h264_split,
  363. };