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.

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