PageRenderTime 26ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/external/ffmpeg/libavcodec/h264_parser.c

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk
C | 409 lines | 317 code | 38 blank | 54 comment | 87 complexity | 623a9d87d388a1c3b5434b9efe917ba8 MD5 | raw file
  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. case NAL_SUBSET_SEQ_PARAM_SET:
  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. /*
  218. ** if current stream is mvc stream, use mvc profile_idc as
  219. ** sps->profile_idc, all here is for report mvc profile_idc
  220. ** to player for mvc disable support.
  221. ** @Sep 17th, 2013, by hbb.
  222. */
  223. if ((h->sps.profile_idc !=118) && (h->sps.profile_idc !=128)) {
  224. h->sps = *h->sps_buffers[h->pps.sps_id];
  225. }
  226. h->frame_num = get_bits(&h->s.gb, h->sps.log2_max_frame_num);
  227. avctx->profile = ff_h264_get_profile(&h->sps);
  228. avctx->level = h->sps.level_idc;
  229. if(h->sps.frame_mbs_only_flag){
  230. h->s.picture_structure= PICT_FRAME;
  231. }else{
  232. if(get_bits1(&h->s.gb)) { //field_pic_flag
  233. h->s.picture_structure= PICT_TOP_FIELD + get_bits1(&h->s.gb); //bottom_field_flag
  234. } else {
  235. h->s.picture_structure= PICT_FRAME;
  236. }
  237. }
  238. if(h->sps.pic_struct_present_flag) {
  239. switch (h->sei_pic_struct) {
  240. case SEI_PIC_STRUCT_TOP_FIELD:
  241. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  242. s->repeat_pict = 0;
  243. break;
  244. case SEI_PIC_STRUCT_FRAME:
  245. case SEI_PIC_STRUCT_TOP_BOTTOM:
  246. case SEI_PIC_STRUCT_BOTTOM_TOP:
  247. s->repeat_pict = 1;
  248. break;
  249. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  250. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  251. s->repeat_pict = 2;
  252. break;
  253. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  254. s->repeat_pict = 3;
  255. break;
  256. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  257. s->repeat_pict = 5;
  258. break;
  259. default:
  260. s->repeat_pict = h->s.picture_structure == PICT_FRAME ? 1 : 0;
  261. break;
  262. }
  263. } else {
  264. s->repeat_pict = h->s.picture_structure == PICT_FRAME ? 1 : 0;
  265. }
  266. return 0; /* no need to evaluate the rest */
  267. }
  268. buf += h->is_avc ? nalsize : consumed;
  269. }
  270. if (q264)
  271. return 0;
  272. /* didn't find a picture! */
  273. av_log(h->s.avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
  274. return -1;
  275. }
  276. static int h264_parse(AVCodecParserContext *s,
  277. AVCodecContext *avctx,
  278. const uint8_t **poutbuf, int *poutbuf_size,
  279. const uint8_t *buf, int buf_size)
  280. {
  281. H264Context *h = s->priv_data;
  282. ParseContext *pc = &h->s.parse_context;
  283. int next;
  284. if (!h->got_first) {
  285. h->got_first = 1;
  286. if (avctx->extradata_size) {
  287. h->s.avctx = avctx;
  288. // must be done like in decoder, otherwise opening the parser,
  289. // letting it create extradata and then closing and opening again
  290. // will cause has_b_frames to be always set.
  291. // Note that estimate_timings_from_pts does exactly this.
  292. if (!avctx->has_b_frames)
  293. h->s.low_delay = 1;
  294. ff_h264_decode_extradata(h, avctx->extradata, avctx->extradata_size);
  295. }
  296. }
  297. if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
  298. next= buf_size;
  299. }else{
  300. next= ff_h264_find_frame_end(h, buf, buf_size);
  301. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  302. *poutbuf = NULL;
  303. *poutbuf_size = 0;
  304. return buf_size;
  305. }
  306. if(next<0 && next != END_NOT_FOUND){
  307. av_assert1(pc->last_index + next >= 0 );
  308. ff_h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next); //update state
  309. }
  310. }
  311. parse_nal_units(s, avctx, buf, buf_size);
  312. if (h->sei_cpb_removal_delay >= 0) {
  313. s->dts_sync_point = h->sei_buffering_period_present;
  314. s->dts_ref_dts_delta = h->sei_cpb_removal_delay;
  315. s->pts_dts_delta = h->sei_dpb_output_delay;
  316. } else {
  317. s->dts_sync_point = INT_MIN;
  318. s->dts_ref_dts_delta = INT_MIN;
  319. s->pts_dts_delta = INT_MIN;
  320. }
  321. if (s->flags & PARSER_FLAG_ONCE) {
  322. s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
  323. }
  324. *poutbuf = buf;
  325. *poutbuf_size = buf_size;
  326. return next;
  327. }
  328. static int h264_split(AVCodecContext *avctx,
  329. const uint8_t *buf, int buf_size)
  330. {
  331. int i;
  332. uint32_t state = -1;
  333. int has_sps= 0;
  334. for(i=0; i<=buf_size; i++){
  335. if((state&0xFFFFFF1F) == 0x107)
  336. has_sps=1;
  337. /* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  338. }*/
  339. if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){
  340. if(has_sps){
  341. while(i>4 && buf[i-5]==0) i--;
  342. return i-4;
  343. }
  344. }
  345. if (i<buf_size)
  346. state= (state<<8) | buf[i];
  347. }
  348. return 0;
  349. }
  350. static void close(AVCodecParserContext *s)
  351. {
  352. H264Context *h = s->priv_data;
  353. ParseContext *pc = &h->s.parse_context;
  354. av_free(pc->buffer);
  355. ff_h264_free_context(h);
  356. }
  357. static int init(AVCodecParserContext *s)
  358. {
  359. H264Context *h = s->priv_data;
  360. h->thread_context[0] = h;
  361. h->s.slice_context_count = 1;
  362. return 0;
  363. }
  364. AVCodecParser ff_h264_parser = {
  365. .codec_ids = { AV_CODEC_ID_H264 },
  366. .priv_data_size = sizeof(H264Context),
  367. .parser_init = init,
  368. .parser_parse = h264_parse,
  369. .parser_close = close,
  370. .split = h264_split,
  371. };