fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <string.h>
  5. extern "C"
  6. {
  7. #include <libavutil/opt.h>
  8. #include <libavcodec/avcodec.h>
  9. #include <libavutil/channel_layout.h>
  10. #include <libavutil/common.h>
  11. #include <libavutil/imgutils.h>
  12. #include <libavutil/mathematics.h>
  13. #include <libavutil/samplefmt.h>
  14. #include <libavformat/avformat.h>
  15.  
  16. }
  17.  
  18. static AVFormatContext *input_fmt_ctx=NULL;
  19. static int video_stream_idx=-1;
  20. static AVStream *input_video_stream=NULL;
  21. static AVCodecContext *video_dec_ctx = NULL;
  22. static uint8_t *video_dst_data[4] = {NULL};
  23. static int video_dst_linesize[4];
  24. static int video_dst_bufsize;
  25. static AVFrame *input_frame;
  26. static AVPacket input_pkt;
  27.  
  28.  
  29. /*video output*/
  30. static AVFrame *frame;
  31. static AVPicture src_picture, dst_picture;
  32. static int frame_count;
  33.  
  34. static int open_codec_context(int *stream_idx,AVFormatContext *fmt_ctx, enum AVMediaType type)
  35. {
  36. int ret;
  37. AVStream *st=NULL;
  38. AVCodecContext *dec_ctx=NULL;
  39. AVCodec *dec=NULL;
  40.  
  41. ret=av_find_best_stream(fmt_ctx,type,-1,-1,NULL,0);
  42. if(ret<0)
  43. {
  44. return ret;
  45. }
  46. else
  47. {
  48. *stream_idx=ret;
  49. st=fmt_ctx->streams[*stream_idx];
  50.  
  51. /*find decoded*/
  52. dec_ctx=st->codec;
  53. dec=avcodec_find_decoder(dec_ctx->codec_id);
  54.  
  55. if(!dec)
  56. {
  57. printf("avcodec_find_decoder fail\n");
  58. return -1;
  59. }
  60.  
  61. if((ret=avcodec_open2(dec_ctx,dec,NULL))<0)
  62. {
  63. printf("avcodec_open2 fail\n");
  64. return -1;
  65. }
  66.  
  67. }
  68. return 0;
  69. }
  70.  
  71. static void open_video(AVFormatContext *oc, AVCodec *codec, AVStream *st)
  72. {
  73. int ret;
  74. AVCodecContext *c=st->codec;
  75.  
  76. ret=avcodec_open2(c,codec,NULL);
  77. if(ret<0)
  78. {
  79. printf("avcodec_open2 fail\n");
  80. exit(-1);
  81. }
  82.  
  83. frame=avcodec_alloc_frame();
  84. if(!frame)
  85. {
  86. printf("avcodec_alloc_frame fail\n");
  87. exit(-1);
  88. }
  89.  
  90. /* Allocate the encoded raw picture. */
  91. ret = avpicture_alloc(&dst_picture, c->pix_fmt, c->width, c->height);
  92. if(ret<0)
  93. {
  94. printf("avpicture_alloc fail\n");
  95. exit(-1);
  96. }
  97.  
  98. if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
  99.  
  100. ret = avpicture_alloc(&src_picture, AV_PIX_FMT_YUV420P, c->width, c->height);
  101. if(ret<0)
  102. {
  103. printf("avpicture_alloc fail\n");
  104. exit(-1);
  105. }
  106. }
  107.  
  108. /* copy data and linesize picture pointers to frame */
  109. *((AVPicture *)frame) = dst_picture;
  110.  
  111. }
  112.  
  113.  
  114. int main()
  115. {
  116. const char* input_file="input.avi";
  117.  
  118. av_register_all();
  119. avcodec_register_all();
  120. int ret;
  121.  
  122.  
  123. /*Reads the file header and stores information about the file format in the AVFormatContext structure*/
  124. if(avformat_open_input(&input_fmt_ctx,input_file,NULL,NULL)<0)
  125. {
  126. printf("avformat_open_input fail\n");
  127. exit(-1);
  128. }
  129.  
  130.  
  131. /*Check out the stream information in the file*/
  132. if(avformat_find_stream_info(input_fmt_ctx,NULL)<0)
  133. {
  134. printf("avformat_find_stream_info fail\n");
  135. exit(-1);
  136. }
  137.  
  138. av_dump_format(input_fmt_ctx,0, input_file, 0);
  139.  
  140. if(open_codec_context(&video_stream_idx,input_fmt_ctx,AVMEDIA_TYPE_VIDEO)>=0)
  141. {
  142. input_video_stream=input_fmt_ctx->streams[video_stream_idx];
  143. video_dec_ctx=input_video_stream->codec;
  144. ret=av_image_alloc(video_dst_data,video_dst_linesize,video_dec_ctx->width,video_dec_ctx->height,video_dec_ctx->pix_fmt,1);
  145. if (ret < 0)
  146. {
  147. printf("av_image_alloc fail\n");
  148. }
  149. video_dst_bufsize=ret;
  150. }
  151. else
  152. {
  153. printf("open_codec_context fail\n");
  154. exit(-1);
  155. }
  156.  
  157. /*配置一個AVframe空間 準備放待會要解的影像資料*/
  158. input_frame =av_frame_alloc();
  159.  
  160. if (!input_frame) {
  161. printf("av_frame_alloc fail\n");
  162. exit(-1);
  163. }
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170. /*========================================*/
  171. /*==========output media==================*/
  172. /*========================================*/
  173.  
  174. /*配置outputfile的參數*/
  175. AVOutputFormat *fmt;
  176. AVFormatContext *oc;
  177. AVStream *video_st=NULL;
  178. AVCodec *video_codec;
  179.  
  180. const char *outputfile;
  181. outputfile="output.mp4";
  182. /* allocate the output media context */
  183.  
  184. avformat_alloc_output_context2(&oc,NULL,NULL,outputfile);
  185. if(!oc)
  186. {
  187. avformat_alloc_output_context2(&oc,NULL,"mpeg",outputfile);
  188. }
  189.  
  190. if(!oc)
  191. {
  192. printf("avformat_alloc_output_context2 fail\n");
  193. exit(-1);
  194. }
  195.  
  196. fmt=oc->oformat;
  197.  
  198. /*Mp4 format為H264*/
  199. fmt->video_codec=AV_CODEC_ID_H264;
  200.  
  201. /*Find the encoder*/
  202. video_codec=avcodec_find_encoder(fmt->video_codec);
  203.  
  204. if(!(video_codec))
  205. {
  206. printf("avcodec_find_encoder fail\n");
  207. exit(-1);
  208. }
  209.  
  210. /*配置output stream*/
  211. video_st=avformat_new_stream(oc,video_codec);
  212. if(!(video_st))
  213. {
  214. printf("avformat_new_stream fail\n");
  215. exit(-1);
  216. }
  217.  
  218. video_st->id=oc->nb_streams-1;
  219.  
  220. video_st->codec->codec_id=fmt->video_codec;
  221. video_st->codec->bit_rate=400000;
  222. /*frame rate*/
  223. video_st->codec->time_base.den=25;
  224. video_st->codec->time_base.num=1;
  225. /*every 12 frame must have I frame*/
  226. video_st->codec->gop_size=12;
  227. video_st->codec->pix_fmt=AV_PIX_FMT_YUV420P;
  228.  
  229. /* Some formats want stream headers to be separate. */
  230. if (oc->oformat->flags & AVFMT_GLOBALHEADER)
  231. {
  232. video_st->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
  233. }
  234.  
  235. video_st->codec->width=video_dec_ctx->width;
  236. video_st->codec->height=video_dec_ctx->height;
  237.  
  238. av_dump_format(oc,0,outputfile,1);
  239.  
  240. /*Try to open output file*/
  241. if((fmt->flags&AVFMT_NOFILE))
  242. {
  243. ret=avio_open(&oc->pb,outputfile,AVIO_FLAG_WRITE);
  244. if(ret<0)
  245. {
  246. fprintf(stderr,"Could not open\n");
  247. exit(-1);
  248. }
  249. }
  250. open_video(oc, video_codec, video_st);
  251.  
  252. /*Write the stream header*/
  253. //ret=avformat_write_header(oc,NULL);
  254.  
  255.  
  256. /*========================================*/
  257. /*========================================*/
  258. /*========================================*/
  259.  
  260.  
  261.  
  262.  
  263. /*Av_read_frame*/
  264. int index=0;
  265. int frameFinished=0;
  266.  
  267. while(av_read_frame(input_fmt_ctx,&input_pkt)>=0)
  268. {
  269. if(input_pkt.stream_index==video_stream_idx)
  270. {
  271. /* decode video frame */
  272. ret=avcodec_decode_video2(video_dec_ctx,input_frame,&frameFinished,&input_pkt);
  273.  
  274. if(ret<0)
  275. {
  276. printf("avcodec_decode_video2 fail\n");
  277. exit(-1);
  278. }
  279.  
  280. /*Aready get a video frame*/
  281. if(frameFinished)
  282. {
  283. /*do something*/
  284. printf("HI ");
  285. }
  286.  
  287. }
  288.  
  289. av_free_packet(&input_pkt);
  290. }
  291.  
  292. /*Close codec*/
  293. avcodec_close(video_st->codec);
  294. av_free(src_picture.data[0]);
  295. av_free(dst_picture.data[0]);
  296. av_free(frame);
  297.  
  298. if(!(fmt->flags&AVFMT_NOFILE))
  299. avio_close(oc->pb);
  300.  
  301. avformat_free_context(oc);
  302. av_free(input_frame);
  303. av_free(video_dst_data[0]);
  304. avcodec_close(video_dec_ctx);
  305.  
  306.  
  307. system("pause");
  308. return 0;
  309. }
  310.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:7:28: fatal error: libavutil/opt.h: No such file or directory
  #include <libavutil/opt.h>
                            ^
compilation terminated.
stdout
Standard output is empty