Mercurial > projects > hoofbaby
annotate src/test/hoofbaby/test/adhoc/transcode.d @ 8:71ebad05f542
It seems to be successfully opening input files... Now to transcode!
author | fraserofthenight |
---|---|
date | Sun, 12 Jul 2009 03:49:39 -0700 |
parents | 9fdfe4a64a13 |
children |
rev | line source |
---|---|
3
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
1 /** |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
2 * Hoofbaby -- http://www.dsource.org/projects/hoofbaby |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
3 * Copyright (C) 2009 Robert Fraser |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
4 * |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
5 * This program is free software; you can redistribute it andor |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
6 * modify it under the terms of the GNU General Public License |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
7 * as published by the Free Software Foundation; either version 2 |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
8 * of the License, or (at your option) any later version. |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
9 * |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
10 * This program is distributed in the hope that it will be useful, |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
13 * GNU General Public License for more details. |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
14 */ |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
15 |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
16 module hoofbaby.test.adhoc.transcode; |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
17 |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
18 import NONE_1 = tango.stdc.stdarg; // Must be linked in to prevent strange linker errors |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
19 debug import NONE_2 = tango.core.stacktrace.TraceExceptions; |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
20 import NONE_3 = hoofbaby.codec.libav.mingw; |
6
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
21 import hoofbaby.app.libs; |
3
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
22 |
7
9fdfe4a64a13
Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
6
diff
changeset
|
23 import hoofbaby.codec.decoder; |
6
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
24 import hoofbaby.codec.encoder; |
8
71ebad05f542
It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents:
7
diff
changeset
|
25 |
71ebad05f542
It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents:
7
diff
changeset
|
26 |
71ebad05f542
It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents:
7
diff
changeset
|
27 public int main(char[][] args) |
71ebad05f542
It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents:
7
diff
changeset
|
28 { |
71ebad05f542
It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents:
7
diff
changeset
|
29 initLibs(); |
71ebad05f542
It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents:
7
diff
changeset
|
30 scope Decoder dec = new Decoder("../../ref/testv-h264-vorbis-ass.mkv"); |
71ebad05f542
It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents:
7
diff
changeset
|
31 delete dec; |
71ebad05f542
It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents:
7
diff
changeset
|
32 return 0; |
71ebad05f542
It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents:
7
diff
changeset
|
33 } |
71ebad05f542
It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents:
7
diff
changeset
|
34 |
71ebad05f542
It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents:
7
diff
changeset
|
35 /+ |
71ebad05f542
It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents:
7
diff
changeset
|
36 |
71ebad05f542
It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents:
7
diff
changeset
|
37 import Math = tango.math.Math; |
3
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
38 import hoofbaby.codec.libav.avutil; |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
39 import hoofbaby.codec.libav.avcodec; |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
40 import hoofbaby.codec.libav.avformat; |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
41 |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
42 private const int STREAM_FRAME_RATE = 25; |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
43 private const double STREAM_DURATION = 5.0; |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
44 private const int STREAM_NB_FRAMES = (cast(int) (STREAM_DURATION * STREAM_FRAME_RATE)); |
6
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
45 private const int WIDTH = 352; |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
46 private const int HEIGHT = 288; |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
47 private const int SAMPLE_RATE = 44100; |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
48 private int AUDIO_FRAME_SIZE; |
3
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
49 |
6
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
50 private int frameCount = 0; |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
51 private double audioCount = 0.0; |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
52 private double audioIncr = 2 * Math.PI * 110.0 / SAMPLE_RATE; |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
53 private double audioIncr2 = 2 * Math.PI * 110.0 / SAMPLE_RATE / SAMPLE_RATE; |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
54 |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
55 private AVFrame* generatePicture(AVFrame* pict, int width, int height) |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
56 { |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
57 int x, y, i; |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
58 i = frameCount; |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
59 |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
60 /* Y */ |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
61 for(y = 0; y < height; y++) |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
62 { |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
63 for(x = 0; x < width; x++) |
3
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
64 { |
6
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
65 pict.data[0][y * pict.linesize[0] + x] = x + y + i * 3; |
3
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
66 } |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
67 } |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
68 |
6
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
69 /* Cb and Cr */ |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
70 for(y = 0; y < height / 2; y++) |
3
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
71 { |
6
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
72 for(x = 0; x < width / 2; x++) |
3
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
73 { |
6
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
74 pict.data[1][y * pict.linesize[1] + x] = 128 + y + i * 2; |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
75 pict.data[2][y * pict.linesize[2] + x] = 64 + x + i * 5; |
3
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
76 } |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
77 } |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
78 |
6
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
79 return pict; |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
80 } |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
81 |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
82 private short* generateAudio(short* samples) |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
83 { |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
84 int j, i, v; |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
85 short *q; |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
86 |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
87 q = samples; |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
88 for(j = 0; j < AUDIO_FRAME_SIZE; j++) |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
89 { |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
90 v = cast(int)(Math.sin(audioCount) * 10000); |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
91 for(i = 0; i < 2; i++) // 2 is number of channels |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
92 *q++ = v; |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
93 audioCount += audioIncr; |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
94 audioIncr += audioIncr2; |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
95 } |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
96 |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
97 return samples; |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
98 } |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
99 |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
100 public int main(char[][] args) |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
101 { |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
102 initLibs(); |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
103 scope Encoder enc = new Encoder("biff_happy.wmv"); |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
104 AUDIO_FRAME_SIZE = enc.audioContext.frame_size; |
8
71ebad05f542
It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents:
7
diff
changeset
|
105 |
3
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
106 // Allocate a video frame and audio buffer to store stuff |
8
71ebad05f542
It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents:
7
diff
changeset
|
107 AVFrame* frame = avpicture_alloc(PIX_FMT_YUV420P, WIDTH, HEIGHT); |
3
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
108 assert(frame !is null, "Could not allocate frame"); |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
109 scope(exit) if(frame) av_free(frame); |
6
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
110 short* samples = cast(short*) av_malloc(AUDIO_FRAME_SIZE * 2 * 2); // AUDIO_FRAME_SIZE * 2 * number of channels |
3
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
111 assert(frame !is null, "Could not allocate samples"); |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
112 scope(exit) if(samples) av_free(samples); |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
113 |
6
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
114 // Write header |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
115 enc.writeHeader(); |
3
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
116 |
8
71ebad05f542
It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents:
7
diff
changeset
|
117 while(true) |
3
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
118 { |
6
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
119 double audio_pts = cast(double) enc.audioStream.pts.val * enc.audioStream.time_base.num / enc.audioStream.time_base.den; |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
120 double video_pts = cast(double) enc.videoStream.pts.val * enc.videoStream.time_base.num / enc.videoStream.time_base.den; |
8
71ebad05f542
It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents:
7
diff
changeset
|
121 |
3
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
122 if(audio_pts >= STREAM_DURATION && video_pts >= STREAM_DURATION) |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
123 break; |
8
71ebad05f542
It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents:
7
diff
changeset
|
124 |
3
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
125 // Write interleaved audio & video |
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
126 if(audio_pts < video_pts) |
6
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
127 { |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
128 enc.writeAudioFrame(generateAudio(samples)); |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
129 } |
3
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
130 else |
6
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
131 { |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
132 if(frameCount >= STREAM_NB_FRAMES) |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
133 { |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
134 // no more frame to compress. The codec has a latency of a few |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
135 // frames if using B frames, so we get the last frames by |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
136 // passing the same picture again |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
137 } |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
138 else |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
139 { |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
140 generatePicture(frame, WIDTH, HEIGHT); |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
141 } |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
142 |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
143 enc.writeVideoFrame(frame); |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
144 frameCount++; |
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
145 } |
3
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
146 } |
8
71ebad05f542
It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents:
7
diff
changeset
|
147 |
71ebad05f542
It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents:
7
diff
changeset
|
148 // Write trailer |
6
270343d824ae
The test program now uses the Encoder class.
fraserofthenight
parents:
3
diff
changeset
|
149 enc.writeTrailer(); |
3
e6cf9f26d0e7
Added a separate "transcode" app for testing transcoding
fraserofthenight
parents:
diff
changeset
|
150 |
8
71ebad05f542
It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents:
7
diff
changeset
|
151 return 0; |
71ebad05f542
It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents:
7
diff
changeset
|
152 } |
71ebad05f542
It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents:
7
diff
changeset
|
153 +/ |