comparison src/impl/hoofbaby/app/codec_adhoc.d @ 0:3425707ddbf6

Initial import (hopefully this mercurial stuff works...)
author fraserofthenight
date Mon, 06 Jul 2009 08:06:28 -0700
parents
children 52278c5dc19c
comparison
equal deleted inserted replaced
-1:000000000000 0:3425707ddbf6
1 /**
2 * Hoofbaby -- http://www.dsource.org/projects/hoofbaby
3 * Copyright (C) 2009 Robert Fraser
4 *
5 * This program is free software; you can redistribute it andor
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15 module hoofbaby.app.codec_adhoc;
16
17 import hoofbaby.util.buffer;
18 import tango.stdc.stdio;
19 import tango.stdc.stringz;
20 import Math = tango.math.Math;
21 import tango.io.device.File;
22 import tango.time.StopWatch;
23
24 import hoofbaby.codec.libav.avutil;
25 import hoofbaby.codec.libav.avcodec;
26 import hoofbaby.codec.libav.avformat;
27 import hoofbaby.codec.libav.avbuffer;
28
29 private const int STREAM_FRAME_RATE = 25;
30 private const double STREAM_DURATION = 5.0;
31 private const int STREAM_NB_FRAMES = (cast(int) (STREAM_DURATION * STREAM_FRAME_RATE));
32 private const int OUTBUF_SIZE = 100000;
33
34 int codec_main()
35 {
36 StopWatch sw;
37 int frameCount = 0;
38 double audioCount, audioIncr, audioIncr2;
39 char* voutbuf, aoutbuf;
40 int res;
41
42 AVFrame* allocFrame(int pix_fmt, int width, int height)
43 {
44 AVFrame* picture;
45 char* buf;
46 int size;
47
48 picture = avcodec_alloc_frame();
49 if(!picture)
50 return null;
51 size = avpicture_get_size(pix_fmt, width, height);
52 buf = cast(char*) av_malloc(size);
53 if(!buf)
54 {
55 av_free(picture);
56 return null;
57 }
58 avpicture_fill(cast(AVPicture*) picture, buf, pix_fmt, width, height);
59 return picture;
60 }
61
62 void generatePicture(AVFrame* pict, int width, int height)
63 {
64 int x, y, i;
65 i = frameCount;
66
67 /* Y */
68 for(y = 0; y < height; y++)
69 {
70 for(x = 0; x < width; x++)
71 {
72 pict.data[0][y * pict.linesize[0] + x] = x + y + i * 3;
73 }
74 }
75
76 /* Cb and Cr */
77 for(y = 0; y < height / 2; y++)
78 {
79 for(x = 0; x < width / 2; x++)
80 {
81 pict.data[1][y * pict.linesize[1] + x] = 128 + y + i * 2;
82 pict.data[2][y * pict.linesize[2] + x] = 64 + x + i * 5;
83 }
84 }
85 }
86
87 int writeVideoFrame(AVFormatContext* ctx, AVStream* stream, AVFrame* picture)
88 {
89 AVCodecContext* vcodec = stream.codec;
90 int ret = 0;
91
92 if(frameCount >= STREAM_NB_FRAMES)
93 {
94 // no more frame to compress. The codec has a latency of a few
95 // frames if using B frames, so we get the last frames by
96 // passing the same picture again
97 }
98 else
99 {
100 generatePicture(picture, vcodec.width, vcodec.height);
101 }
102
103 // Encode it!
104 int outSize = avcodec_encode_video(vcodec, voutbuf, OUTBUF_SIZE, picture);
105 // if zero size, it means the image was buffered.. if not, write that ****!
106 if(outSize > 0)
107 {
108 AVPacket pkt;
109 av_init_packet(&pkt);
110
111 pkt.pts = av_rescale_q(vcodec.coded_frame.pts, vcodec.time_base, stream.time_base);
112 if(vcodec.coded_frame.key_frame)
113 pkt.flags |= PKT_FLAG_KEY;
114 pkt.stream_index = stream.index;
115 pkt.data = voutbuf;
116 pkt.size = outSize;
117
118 // oh yeah!
119 ret = av_write_frame(ctx, &pkt);
120 }
121 else
122 {
123 ret = 0;
124 }
125
126 frameCount++;
127 assert(!ret, "Error writing video frame");
128 return ret;
129 }
130
131 int writeAudioFrame(AVFormatContext* ctx, AVStream* stream, short* samples)
132 {
133 AVCodecContext* acodec = stream.codec;
134
135 {
136 int j, i, v;
137 short *q;
138
139 q = samples;
140 for(j = 0; j < acodec.frame_size; j++)
141 {
142 v = cast(int)(Math.sin(audioCount) * 10000);
143 for(i = 0; i < 2; i++) // 2 is number of channels
144 *q++ = v;
145 audioCount += audioIncr;
146 audioIncr += audioIncr2;
147 }
148 }
149
150 AVPacket pkt;
151 av_init_packet(&pkt);
152 pkt.size = avcodec_encode_audio(acodec, aoutbuf, OUTBUF_SIZE, samples);
153 //pkt.pts = av_rescale_q(acodec.coded_frame.pts, acodec.time_base, acodec.time_base);
154 pkt.flags |= PKT_FLAG_KEY;
155 pkt.stream_index = stream.index;
156 pkt.data = aoutbuf;
157
158 int res = av_write_frame(ctx, &pkt) != 0;
159 assert(res == 0, "Error writing audio frame");
160 return res;
161 }
162
163 //--------------------------------------------------------------------------
164 // Container format
165
166 sw.start();
167
168 AVOutputFormat* fmt = guess_format("asf", null, null);
169 assert(fmt !is null, "Could not find format");
170
171 AVFormatContext* ctx = av_alloc_format_context();
172 assert(ctx !is null, "Could not allocate format context");
173 scope(exit) if(ctx) av_free(ctx);
174 ctx.oformat = fmt;
175 //ctx.preload = cast(int) (0.5 * AV_TIME_BASE);
176 ctx.max_delay = cast(int) (0.7 * AV_TIME_BASE);
177 ctx.loop_output = AVFMT_NOOUTPUTLOOP;
178 ctx.flags |= AVFMT_FLAG_NONBLOCK;
179
180 AVFormatParameters params;
181 params.prealloced_context = 1;
182 params.video_codec_id = CODEC_ID_WMV2;
183 params.audio_codec_id = CODEC_ID_WMAV2;
184 params.width = 352;
185 params.height = 288;
186 params.time_base.num = 1;
187 params.time_base.den = STREAM_FRAME_RATE;
188 params.pix_fmt = PIX_FMT_YUV420P;
189 params.channels = 2;
190 params.sample_rate = 44100;
191 res = av_set_parameters(ctx, null);
192 assert(res >= 0, "Could not set parameters");
193
194 //--------------------------------------------------------------------------
195 // Video stream
196
197 AVStream* vstream = av_new_stream(ctx, 0);
198 assert(vstream !is null, "Could not allocate video stream");
199 ctx.streams[0] = vstream;
200
201 AVCodec* vcodecName = avcodec_find_encoder(CODEC_ID_WMV2);
202 assert(vcodecName, "Could not find video codec");
203 AVCodecContext* vcodec = vstream.codec;
204 vcodec.codec_id = CODEC_ID_WMV2;
205 vcodec.codec_type = CODEC_TYPE_VIDEO;
206 vcodec.bit_rate = 400000;
207 vcodec.width = 352;
208 vcodec.height = 288;
209 vcodec.gop_size = 12;
210 vcodec.qmin = 3;
211 vcodec.time_base.den = STREAM_FRAME_RATE;
212 vcodec.time_base.num = 1;
213 vcodec.pix_fmt = PIX_FMT_YUV420P;
214 vcodec.flags |= CODEC_FLAG_GLOBAL_HEADER;
215 res = avcodec_open(vcodec, vcodecName);
216 assert(res >= 0, "Could not open video codec");
217
218 //--------------------------------------------------------------------------
219 // Audio stream
220
221 AVStream* astream = av_new_stream(ctx, 0);
222 assert(astream !is null, "Could not allocate audio stream");
223 ctx.streams[1] = astream;
224
225 AVCodec* acodecName = avcodec_find_encoder(CODEC_ID_WMAV2);
226 assert(acodecName, "Could not find audio codec");
227 AVCodecContext* acodec = astream.codec;
228 acodec.codec_id = CODEC_ID_WMAV2;
229 acodec.codec_type = CODEC_TYPE_AUDIO;
230 acodec.bit_rate = 64000;
231 acodec.sample_rate = 44100;
232 acodec.channels = 2;
233 acodec.flags |= CODEC_FLAG_GLOBAL_HEADER;
234 audioCount = 0.0;
235 audioIncr = 2 * Math.PI * 110.0 / acodec.sample_rate;
236 audioIncr2 = 2 * Math.PI * 110.0 / acodec.sample_rate / acodec.sample_rate;
237 res = avcodec_open(acodec, acodecName);
238 assert(res >= 0, "Could not open audio codec");
239
240 //--------------------------------------------------------------------------
241 // Actually doing stuff
242
243 // Open output file
244 RingBuffer ringBuf = RingBuffer(1 << 24); // 16 MB
245 scope(exit) ringBuf.free();
246 ctx.pb = getBioContext(&ringBuf, 1 << 24);
247 assert(ctx.pb !is null);
248 assert(ctx.pb.opaque !is null);
249
250 // Allocate a video frame and audio buffer to store stuff
251 AVFrame* frame = allocFrame(PIX_FMT_YUV420P, vcodec.width, vcodec.height);
252 assert(frame !is null, "Could not allocate frame");
253 scope(exit) if(frame) av_free(frame);
254 short* samples = cast(short*) av_malloc(acodec.frame_size * 2 * acodec.channels);
255 assert(frame !is null, "Could not allocate samples");
256 scope(exit) if(samples) av_free(samples);
257
258 // Allocate some output buffers
259 voutbuf = cast(char*) av_malloc(OUTBUF_SIZE);
260 assert(voutbuf !is null, "Could not allocate video output buffer");
261 scope(exit) if(voutbuf) av_free(voutbuf);
262 aoutbuf = cast(char*) av_malloc(OUTBUF_SIZE);
263 assert(aoutbuf !is null, "Could not allocate audio output buffer");
264 scope(exit) if(aoutbuf) av_free(voutbuf);
265
266 printf("Setup time %f\n", sw.stop());
267 sw.start();
268
269 // Write the header
270 res = av_write_header(ctx);
271 assert(res >= 0, "Could not write header for output file (incorrect codec paramters?)");
272
273 while(true)
274 {
275 double audio_pts = cast(double) astream.pts.val * astream.time_base.num / astream.time_base.den;
276 double video_pts = cast(double) vstream.pts.val * vstream.time_base.num / vstream.time_base.den;
277
278 if(audio_pts >= STREAM_DURATION && video_pts >= STREAM_DURATION)
279 break;
280
281 // Write interleaved audio & video
282 if(audio_pts < video_pts)
283 writeAudioFrame(ctx, astream, samples);
284 else
285 writeVideoFrame(ctx, vstream, frame);
286 }
287
288 res = av_write_trailer(ctx);
289 assert(res >= 0, "Could not write trailer for output file");
290
291 printf("Encoding time %f\n", sw.stop());
292 sw.start();
293
294 scope File file = new File("biff_happy.wmv", File.WriteCreate);
295 uint available = ringBuf.available;
296 ringBuf.beginRead(available);
297 file.write(ringBuf.addr[0 .. ringBuf.available()]);
298 ringBuf.endRead(available);
299 file.close();
300
301 printf("IO time %f\n", sw.stop());
302 sw.start();
303
304 return 0;
305 }