annotate src/impl/hoofbaby/codec/decoder.d @ 9:05c88622db6b default tip

- Adhoc transcoder now doesn't rely on Platinum - Moved the adhoc transcoder to the main source tree; it can be moved back into test once it's more stable
author fraserofthenight
date Tue, 14 Jul 2009 22:04:05 -0700
parents 71ebad05f542
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
1 /**
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
2 * Hoofbaby -- http://www.dsource.org/projects/hoofbaby
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
3 * Copyright (C) 2009 Robert Fraser
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
4 *
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
5 * This program is free software; you can redistribute it andor
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
6 * modify it under the terms of the GNU General Public License
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
7 * as published by the Free Software Foundation; either version 2
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
8 * of the License, or (at your option) any later version.
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
9 *
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
13 * GNU General Public License for more details.
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
14 */
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
15
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
16 module hoofbaby.codec.decoder;
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
17
8
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
18 import tango.io.Stdout;
7
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
19 import tango.stdc.stringz;
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
20
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
21 import hoofbaby.codec.libav.avutil;
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
22 import hoofbaby.codec.libav.avcodec;
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
23 import hoofbaby.codec.libav.avformat;
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
24
9
05c88622db6b - Adhoc transcoder now doesn't rely on Platinum
fraserofthenight
parents: 8
diff changeset
25 public final class Decoder
7
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
26 {
8
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
27 private char[] file;
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
28
7
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
29 private AVFormatContext* formatContext;
8
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
30 private bool fileOpen = false;
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
31
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
32 private AVStream* videoStream;
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
33 private AVCodec* videoCodec;
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
34 private AVCodecContext* videoContext;
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
35 private bool videoOpen = false;
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 private AVStream* audioStream;
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
38 private AVCodec* audioCodec;
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
39 private AVCodecContext* audioContext;
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
40 private bool audioOpen = false;
7
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
41
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
42 public this(char[] inFilename)
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
43 {
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
44 int ret;
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
45
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
46 // Open file & find stream info
8
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
47 file = inFilename;
7
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
48 char* filename = toStringz(inFilename);
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
49 ret = av_open_input_file(&formatContext, filename, null, 0, null);
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
50 assert(ret == 0, "Could not open input file");
8
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
51 fileOpen = true;
7
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
52 ret = av_find_stream_info(formatContext);
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
53 assert(ret >= 0, "Could not find stream info");
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
54 dump_format(formatContext, 0, filename, false);
8
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
55
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
56 // Find the first video stream (don't worry about the rest until a much later version)
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
57 for(int i = 0; i < formatContext.nb_streams; i++)
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
58 {
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
59 AVStream* str = formatContext.streams[i];
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
60 if(str !is null && str.codec.codec_type == CODEC_TYPE_VIDEO)
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
61 {
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
62 videoStream = str;
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
63 break;
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
64 }
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
65 }
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
66 assert(videoStream !is null, "Could not find video stream");
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
67 videoContext = videoStream.codec;
9
05c88622db6b - Adhoc transcoder now doesn't rely on Platinum
fraserofthenight
parents: 8
diff changeset
68 assert(videoContext !is null, "Null codec context from non-null video stream");
8
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
69 videoCodec = avcodec_find_decoder(videoContext.codec_id);
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
70 assert(videoCodec !is null, "Unsupported video format");
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
71 ret = avcodec_open(videoContext, videoCodec);
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
72 assert(ret >= 0, "Could not open video decoder");
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
73 videoOpen = true;
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
74
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
75 // Find the first audio stream (NEXTVERSION deal with multiple audio streams)
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
76 for(int i = 0; i < formatContext.nb_streams; i++)
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
77 {
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
78 AVStream* str = formatContext.streams[i];
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
79 if(str !is null && str.codec.codec_type == CODEC_TYPE_AUDIO)
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
80 {
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
81 audioStream = str;
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
82 break;
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
83 }
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
84 }
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
85 assert(audioStream !is null, "Could not find audio stream");
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
86 audioContext = audioStream.codec;
9
05c88622db6b - Adhoc transcoder now doesn't rely on Platinum
fraserofthenight
parents: 8
diff changeset
87 assert(audioContext !is null, "Null codec context from non-null audio stream");
8
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
88 audioCodec = avcodec_find_decoder(audioContext.codec_id);
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
89 assert(audioCodec !is null, "Unsupported audio format");
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
90 ret = avcodec_open(audioContext, audioCodec);
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
91 assert(ret >= 0, "Could not open audio decoder");
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
92 audioOpen = true;
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
93
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
94 scope(failure) freeResources();
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
95 }
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
96
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
97 public ~this()
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
98 {
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
99 freeResources();
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
100 }
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
101
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
102 private void freeResources()
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
103 {
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
104 if(videoOpen) avcodec_close(videoContext);
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
105 if(audioOpen) avcodec_close(audioContext);
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
106 if(fileOpen) av_close_input_file(formatContext);
71ebad05f542 It seems to be successfully opening input files... Now to transcode!
fraserofthenight
parents: 7
diff changeset
107 if(formatContext) av_free(formatContext);
7
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
108 }
9fdfe4a64a13 Added the beginnings of a decoder... Soon I'll have a crappier version of ffmpeg written in D!
fraserofthenight
parents:
diff changeset
109 }