Permalink
Browse files

sample/transcode fully functions now!

  • Loading branch information...
drbrain committed Sep 6, 2009
1 parent 48c08af commit b36b87854529de87e8c9231d8e797c99ccce9fdc
View
@@ -1,4 +1,3 @@
*.swp
/TAGS
-/Thumbs Up!.flv
-/Thumbs Up!.mp4
+/Thumbs Up!.*
View
@@ -10,7 +10,7 @@ abort "#{$PROGRAM_NAME} filename" unless file
input = FFMPEG::FormatContext.new file
puts input.filename
-puts "#{input.duration.to_f / FFMPEG::TIME_BASE} seconds"
+puts "#{input.duration.to_f / FFMPEG::TIME_BASE} seconds #{input.bit_rate} bps"
puts
puts 'streams:'
@@ -75,7 +75,7 @@ class FFMPEG::CodecContext
# :method: decode_audio
builder.c <<-C
- VALUE decode_audio(VALUE buffer, VALUE packet) {
+ int decode_audio(VALUE buffer, VALUE packet) {
AVCodecContext *codec_context;
AVPacket *pkt;
int16_t *samples;
@@ -95,13 +95,17 @@ class FFMPEG::CodecContext
ffmpeg_check_error(bytes_used);
+ /* FFMPEG source mentions some codecs seem to overflow */
+ if (frame_size < 0)
+ frame_size = 0;
+
#ifdef rb_str_set_len
rb_str_set_len(buffer, frame_size);
#else
RSTRING(buffer)->len = frame_size;
#endif
- return INT2NUM(bytes_used);
+ return bytes_used;
}
C
@@ -149,6 +153,30 @@ class FFMPEG::CodecContext
}
C
+ ##
+ # :method: encode_audio
+
+ builder.c <<-C
+ int encode_audio(VALUE samples, VALUE encoded) {
+ AVCodecContext *codec_context;
+ int used;
+
+ Data_Get_Struct(self, AVCodecContext, codec_context);
+
+ used = avcodec_encode_audio(codec_context,
+ (uint8_t *)RSTRING_PTR(encoded),
+ (int)RSTRING_LEN(encoded),
+ (const short *)RSTRING_PTR(samples));
+
+ if (used < 0)
+ rb_raise(rb_path2class("FFMPEG::Error"), "audio encoding failed");
+
+ rb_str_set_len(encoded, used);
+
+ return used;
+ }
+ C
+
##
# :method: encode_video
@@ -284,6 +312,7 @@ class FFMPEG::CodecContext
builder.accessor :bit_rate, 'int'
builder.accessor :bit_rate_tolerance, 'int'
+ builder.accessor :channels, 'int'
builder.accessor :codec_id, 'int'
builder.accessor :gop_size, 'int'
builder.accessor :flags, 'int'
@@ -296,8 +325,8 @@ class FFMPEG::CodecContext
builder.accessor :sample_rate, 'int'
builder.accessor :width, 'int'
- builder.reader :channels, 'int'
builder.reader :_codec_type, 'int', :codec_type
+ builder.reader :frame_size, 'int'
builder.reader :sample_format, 'int', :sample_fmt
builder.reader :codec_name, 'char *'
View
@@ -9,44 +9,57 @@ class FFMPEG::FIFO
builder.c_singleton <<-C
VALUE allocate() {
- return Data_Wrap_Struct(self, NULL, av_fifo_free, NULL);
+ AVFifoBuffer *fifo;
+
+ fifo = av_fifo_alloc(0);
+
+ if (!fifo)
+ rb_raise(rb_eNoMemError, "could not allocate FFMPEG::FIFO");
+
+ return Data_Wrap_Struct(self, NULL, av_fifo_free, fifo);
}
C
##
- # :method: initialize
+ # :method: drain
builder.c <<-C
- void initialize(unsigned int bytes) {
+ void drain(int bytes) {
AVFifoBuffer *fifo;
Data_Get_Struct(self, AVFifoBuffer, fifo);
- fifo = av_fifo_alloc(bytes);
-
- if (!fifo)
- rb_raise(rb_eNoMemError, "could not allocate FFMPEG::FIFO");
+ av_fifo_drain(fifo, bytes);
}
C
##
- # :method: drain
+ # :method: read
builder.c <<-C
- void drain(int bytes) {
+ VALUE read(VALUE buffer, int bytes) {
AVFifoBuffer *fifo;
+ int r;
+
+ buffer = rb_str_to_str(buffer);
+
+ if (RSTRING_LEN(buffer) < bytes)
+ rb_raise(rb_eArgError, "read size smaller than buffer");
Data_Get_Struct(self, AVFifoBuffer, fifo);
+
+ r = av_fifo_generic_read(fifo, (void *)RSTRING_PTR(buffer), bytes,
+ NULL);
- av_fifo_drain(fifo, bytes);
+ return buffer;
}
C
##
- # :method: realloc2
+ # :method: realloc
- builder.c <<-C
- int realloc2(unsigned int bytes) {
+ builder.c <<-C, :method_name => :realloc
+ int fifo_realloc(unsigned int bytes) {
AVFifoBuffer *fifo;
int ret;
@@ -99,6 +112,33 @@ class FFMPEG::FIFO
return av_fifo_space(fifo);
}
C
+
+ ##
+ # :method: write
+
+ builder.c <<-C
+ int write(VALUE buffer) {
+ AVFifoBuffer *fifo;
+ int written;
+
+ Data_Get_Struct(self, AVFifoBuffer, fifo);
+
+ written = av_fifo_generic_write(fifo, (void *)RSTRING_PTR(buffer),
+ (int)RSTRING_LEN(buffer), NULL);
+
+ return written;
+ }
+ C
+
+ end
+
+ def initialize(bytes)
+ realloc bytes
+ end
+
+ def inspect
+ '#<%s:0x%x size %d>' % [self.class, object_id, size]
end
+
end
Oops, something went wrong.

0 comments on commit b36b878

Please sign in to comment.