Ticket #984: main.c

File main.c, 7.8 KB (added by Denis, 14 years ago)

C source for get the bug

Line 
1#include <stdio.h>
2#include "libavcodec/avcodec.h"
3#include "libavformat/avformat.h"
4#include "libswscale/swscale.h"
5
6FILE *FileOut;
7
8static int WritePacket(void *opaque, uint8_t *pBuffer, int pBufferSize) {
9 return fwrite(pBuffer, pBufferSize, 1, FileOut);
10}
11
12void run() {
13 printf("begin\n");
14 int Width= 320, Height= 240;
15 const char *FileName= "a.ogg";
16 FileOut= fopen(FileName, "w");
17 int BUFFERSIZE= 32768;
18 //int BUFFERSIZE= 512;
19 AVIOContext *pAVIOContext= NULL;
20 unsigned char *pBufferCallBack= (unsigned char*)av_malloc(BUFFERSIZE * sizeof(uint8_t)); {
21 pAVIOContext= avio_alloc_context(pBufferCallBack, BUFFERSIZE, 1, NULL, NULL, WritePacket, NULL);
22 if (pAVIOContext== NULL) printf("Errore avio_alloc_context !!!\n");
23 else {
24 AVOutputFormat *pAVOutputFormat= av_guess_format(NULL, FileName, NULL); // CODEC_ID_H264 -> mp4, mov; CODEC_ID_THEORA -> ogg; CODEC_ID_MPEG4 -> mpegts, avi; CODEC_ID_VP8 -> webm
25 if (!pAVOutputFormat) {
26 printf("Could not set output format, using MPEG.\n");
27 pAVOutputFormat= av_guess_format("mpeg", NULL, NULL);
28 }
29 if (!pAVOutputFormat) printf("av_guess_format Error!\n");
30 else {
31 AVFormatContext *pAVFormatContext;
32 pAVFormatContext= avformat_alloc_context();
33 if (!pAVFormatContext) printf("avformat_alloc_context Error!\n");
34 else {
35 pAVFormatContext->pb= pAVIOContext;
36 pAVFormatContext->oformat= pAVOutputFormat;
37 AVStream *pAVStream= avformat_new_stream(pAVFormatContext, NULL);
38 if (!pAVStream) printf("av_new_stream Error!");
39 else {
40 AVCodecContext *pAVCodecContext= pAVStream->codec;
41 pAVCodecContext->codec_id= /*(CodecID)*/pAVOutputFormat->video_codec;
42 pAVCodecContext->codec_type= AVMEDIA_TYPE_VIDEO;
43 pAVCodecContext->bit_rate= 40000;
44 pAVCodecContext->width= Width;
45 pAVCodecContext->height= Height;
46 pAVCodecContext->time_base.den= 25;
47 pAVCodecContext->time_base.num= 1;
48 pAVCodecContext->gop_size= 10;
49 pAVCodecContext->pix_fmt= PIX_FMT_YUV420P;
50 if (pAVFormatContext->oformat->flags & AVFMT_GLOBALHEADER) pAVCodecContext->flags |= CODEC_FLAG_GLOBAL_HEADER;
51 if (pAVCodecContext->codec_id== CODEC_ID_H264) {
52 printf("CODEC_ID_H264\n");
53 pAVCodecContext->bit_rate= 500 * 1000;
54 pAVCodecContext->bit_rate_tolerance= 0;
55 pAVCodecContext->rc_max_rate= 0;
56 pAVCodecContext->rc_buffer_size= 0;
57 pAVCodecContext->gop_size= 40;
58 pAVCodecContext->max_b_frames= 3;
59 pAVCodecContext->b_frame_strategy= 1;
60 pAVCodecContext->coder_type= 1;
61 pAVCodecContext->me_cmp= 1;
62 pAVCodecContext->me_range= 16;
63 pAVCodecContext->qmin= 10;
64 pAVCodecContext->qmax= 51;
65 pAVCodecContext->scenechange_threshold= 40;
66 pAVCodecContext->flags|= CODEC_FLAG_LOOP_FILTER;
67 pAVCodecContext->me_method= ME_HEX;
68 pAVCodecContext->me_subpel_quality= 5;
69 pAVCodecContext->i_quant_factor= 0.71;
70 pAVCodecContext->qcompress= 0.6;
71 pAVCodecContext->max_qdiff= 4;
72 pAVCodecContext->flags2|= CODEC_FLAG2_FASTPSKIP;
73 pAVFormatContext->oformat->flags |= AVFMT_TS_NONSTRICT;
74 } else if (pAVCodecContext->codec_id== CODEC_ID_THEORA) {
75 printf("CODEC_ID_THEORA\n");
76 //pAVCodecContext->extradata= av_malloc((void*)FF_INPUT_BUFFER_PADDING_SIZE);
77 }
78 av_dict_set(&pAVFormatContext->metadata, "title", "Titolo", 0);
79 av_dump_format(pAVFormatContext, 0, NULL, 1);
80 if (avformat_write_header(pAVFormatContext, NULL)!= 0) printf("av_write_header Error!\n");
81 AVCodec *pCodec= avcodec_find_encoder(pAVCodecContext->codec_id);
82 if (!pCodec) printf("avcodec_find_encoder Error!");
83 else {
84 if (avcodec_open2(pAVCodecContext, pCodec, NULL)< 0) printf("avcodec_open Error!\n");
85 else {
86 int BYTEPIC= Width * Height * 3;
87 uint8_t *pOutBuffer= (uint8_t*)malloc(BYTEPIC); {
88 int Frames= 0;
89 while (Frames< 250) {
90 Frames++;
91 AVFrame *pAVFrame= avcodec_alloc_frame();
92 uint8_t *pBuffer= (uint8_t*)malloc(avpicture_get_size(PIX_FMT_YUV420P, Width, Height)); {
93 avpicture_fill((AVPicture*)pAVFrame, pBuffer, PIX_FMT_YUV420P, Width, Height);
94 pAVFrame->pts= Frames;
95 int OutSize= avcodec_encode_video(pAVCodecContext, pOutBuffer, BYTEPIC, pAVFrame);
96 if (OutSize> 0) {
97 AVPacket Packet;
98 av_init_packet(&Packet);
99 //Packet.pts= Frames;
100 //if (pAVCodecContext->coded_frame->pts!= unsigned(AV_NOPTS_VALUE)) {
101 Packet.pts= av_rescale_q(pAVCodecContext->coded_frame->pts, pAVCodecContext->time_base, pAVStream->time_base);
102 //}
103 if (pAVCodecContext->coded_frame->key_frame) Packet.flags |= AV_PKT_FLAG_KEY;
104 Packet.stream_index= pAVStream->index;
105 Packet.data= pOutBuffer;
106 Packet.size= OutSize;
107 if (av_interleaved_write_frame(pAVFormatContext, &Packet)!= 0) printf("av_interleaved_write_frame Error!\n");
108 }
109 }{
110 av_free(pAVFrame);
111 free(pBuffer);
112 }
113 usleep(1000 * 1000 / 25);
114 }
115 }{
116 free(pOutBuffer);
117 }
118 avcodec_close(pAVCodecContext);
119 }
120 }
121 if (av_write_trailer(pAVFormatContext)!= 0) printf("av_write_trailer Error!\n");
122 av_free(pAVStream);
123 }
124 av_free(pAVFormatContext);
125 }
126 //av_free(pAVOutputFormat);
127 }
128 av_free(pAVIOContext);
129 }
130 }{
131 free(pBufferCallBack);
132 }
133 fclose(FileOut);
134 printf("end\n");
135}
136
137int main(void) {
138 printf("Hello World!\n");
139 av_register_all();
140 run();
141 return 0;
142}