[avbin commit] r46 - in trunk: example include src

1 view
Skip to first unread message

codesite...@google.com

unread,
Nov 27, 2008, 4:41:40 AM11/27/08
to avbin-...@googlegroups.com
Author: Alex.Holkner
Date: Thu Nov 27 01:41:18 2008
New Revision: 46

Added:
trunk/example/
trunk/example/Makefile
trunk/example/avbin_dump.c
Modified:
trunk/include/avbin.h
trunk/src/avbin.c

Log:
Add frame_rate feature (requires version 8). Add avbin_dump example.

Added: trunk/example/Makefile
==============================================================================
--- (empty file)
+++ trunk/example/Makefile Thu Nov 27 01:41:18 2008
@@ -0,0 +1,9 @@
+# $Id:$
+
+# Makefile for avbin_dump.c. Requires Linux (modifications for other
+# platforms should be straightforward).
+
+CFLAGS=-I../include
+LDFLAGS=-lavbin -lm -lpthread
+
+avbin_dump : avbin_dump.c

Added: trunk/example/avbin_dump.c
==============================================================================
--- (empty file)
+++ trunk/example/avbin_dump.c Thu Nov 27 01:41:18 2008
@@ -0,0 +1,130 @@
+/* $Id:$ */
+
+/* Example use of AVbin.
+ *
+ * Prints out stream details, then exits.
+ *
+ * TODO: Optionally print verbose decoding information.
+ * TODO: Clean up, comment.
+ *
+ * Originally by Micah Richert
+ * Modified by Alex Holkner
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <avbin.h>
+
+int main(int argc, char** argv)
+{
+ if (avbin_init())
+ exit(-1);
+
+ printf("%d, %d\n", avbin_get_version(), avbin_get_ffmpeg_revision());
+
+ if (argc < 2)
+ exit(-1);
+
+ AVbinFile* file = avbin_open_filename(argv[1]);
+ if (!file)
+ exit(-1);
+
+ AVbinFileInfo fileinfo;
+ fileinfo.structure_size = sizeof(fileinfo);
+
+ if (avbin_file_info(file, &fileinfo))
+ exit(-2);
+
+ printf("#streams %d\n",fileinfo.n_streams);
+ printf("duration %lldus (%d:%02d:%02d)\n",
+ fileinfo.duration,
+ fileinfo.duration / (1000000L * 60 * 60),
+ (fileinfo.duration / (1000000L * 60)) % 60,
+ (fileinfo.duration / 1000000L) % 60);
+
+ AVbinStream* video_stream = NULL;
+ AVbinStream* audio_stream = NULL;
+ int video_stream_index = -1;
+ int audio_stream_index = -1;
+ int width, height;
+ int have_frame_rate = avbin_have_feature("frame_rate");
+
+ int stream_index;
+ for (stream_index=0; stream_index<fileinfo.n_streams; stream_index++)
+ {
+ AVbinStreamInfo8 streaminfo;
+ streaminfo.structure_size = sizeof(streaminfo);
+
+ avbin_stream_info(file, stream_index, (AVbinStreamInfo *)
&streaminfo);
+
+ if (streaminfo.type == AVBIN_STREAM_TYPE_VIDEO)
+ {
+ printf("video stream at %d, height %d,
width %d\n",stream_index,streaminfo.video.height,streaminfo.video.width);
+ if (have_frame_rate)
+ printf("frame rate %d / %d (approximately %.2f)\n",
+ streaminfo.video.frame_rate_num,
+ streaminfo.video.frame_rate_den,
+ streaminfo.video.frame_rate_num / (float)
+ streaminfo.video.frame_rate_den);
+ width = streaminfo.video.width;
+ height = streaminfo.video.height;
+ video_stream_index = stream_index;
+ video_stream = avbin_open_stream(file, stream_index);
+ }
+ if (streaminfo.type == AVBIN_STREAM_TYPE_AUDIO)
+ {
+ printf("audio stream at %d, rate %d, bits %d,
chan %d\n",stream_index,streaminfo.audio.sample_rate,streaminfo.audio.sample_bits,streaminfo.audio.channels);
+ audio_stream_index = stream_index;
+ audio_stream = avbin_open_stream(file, stream_index);
+ }
+ }
+
+ exit(0);
+
+ /* TODO enable the following optionally */
+
+ AVbinPacket packet;
+ packet.structure_size = sizeof(packet);
+
+ while (!avbin_read(file, &packet))
+ {
+ if (packet.stream_index == video_stream_index)
+ {
+ uint8_t* video_buffer = (uint8_t*) malloc(width*height*3);
+ if (avbin_decode_video(video_stream, packet.data,
packet.size,video_buffer)<=0) printf("could not read video packet\n");
+ else printf("read video frame\n");
+
+ // do something with video_buffer
+
+ free(video_buffer);
+ }
+ if (packet.stream_index == audio_stream_index)
+ {
+ uint8_t audio_buffer[1024*1024];
+ int bytesleft = sizeof(audio_buffer);
+ int bytesout = bytesleft;
+ int bytesread;
+ uint8_t* audio_data = audio_buffer;
+ while ((bytesread = avbin_decode_audio(audio_stream,
packet.data, packet.size, audio_data, &bytesout)) > 0)
+ {
+ packet.data += bytesread;
+ packet.size -= bytesread;
+ audio_data += bytesout;
+ bytesleft -= bytesout;
+ bytesout = bytesleft;
+ }
+
+ int nrBytes = audio_data-audio_buffer;
+
+ printf("read audio packet of size %d bytes\n",nrBytes);
+
+ // do something with audio_buffer ... but don't free it since
it is a local array
+ }
+ }
+
+ if (video_stream) avbin_close_stream(video_stream);
+ if (audio_stream) avbin_close_stream(audio_stream);
+
+ avbin_close_file(file);
+}

Modified: trunk/include/avbin.h
==============================================================================
--- trunk/include/avbin.h (original)
+++ trunk/include/avbin.h Thu Nov 27 01:41:18 2008
@@ -218,10 +218,18 @@

/**
* Aspect-ratio of each pixel. The aspect is given by dividing
- * sample_aspect_num by asmple_aspect_den.
+ * sample_aspect_num by sample_aspect_den.
*/
unsigned int sample_aspect_num;
unsigned int sample_aspect_den;
+
+ /** Frame rate, in frames per second. The frame rate is given
by
+ * dividing frame_rate_num by frame_rate_den.
+ *
+ * @version Version 8. Requires frame_rate feature.
+ */
+ unsigned int frame_rate_num;
+ unsigned int frame_rate_den;
} video;

struct {
@@ -252,6 +260,84 @@
} AVbinStreamInfo;

/**
+ * Stream details, version 8.
+ *
+ * A stream is a single audio track or video. Most audio files contain one
+ * audio stream. Most video files contain one audio stream and one video
+ * stream. More than one audio stream may indicate the presence of
multiple
+ * languages which can be selected (however at this time AVbin does not
+ * provide language information).
+ */
+typedef struct _AVbinStreamInfo8 {
+ /**
+ * Size of this structure, in bytes. This must be filled in by the
+ * application before passing to AVbin.
+ */
+ size_t structure_size;
+
+ /**
+ * The type of stream; either audio or video.
+ */
+ AVbinStreamType type;
+
+ union {
+ struct {
+ /**
+ * Width of the video image, in pixels. This is the width
+ * of actual video data, and is not necessarily the size the
+ * video is to be displayed at (see sample_aspect_num).
+ */
+ unsigned int width;
+
+ /**
+ * Height of the video image, in pixels.
+ */
+ unsigned int height;
+
+ /**
+ * Aspect-ratio of each pixel. The aspect is given by dividing
+ * sample_aspect_num by sample_aspect_den.
+ */
+ unsigned int sample_aspect_num;
+ unsigned int sample_aspect_den;
+
+ /** Frame rate, in frames per second. The frame rate is given
by
+ * dividing frame_rate_num by frame_rate_den.
+ *
+ * @version Version 8. Requires frame_rate extension.
+ */
+ unsigned int frame_rate_num;
+ unsigned int frame_rate_den;
+ } video;
+
+ struct {
+ /**
+ * Data type of audio samples.
+ */
+ AVbinSampleFormat sample_format;
+
+ /**
+ * Number of samples per second, in Hz.
+ */
+ unsigned int sample_rate;
+
+ /**
+ * Number of bits per sample; typically 8 or 16.
+ */
+ unsigned int sample_bits;
+
+ /**
+ * Number of interleaved audio channels. Typically 1 for
+ * monoaural, 2 for stereo. Higher channel numbers are used
for
+ * surround sound, however AVbin does not currently provide a
way
+ * to access the arrangement of these channels.
+ */
+ unsigned int channels;
+ } audio;
+ };
+} AVbinStreamInfo8;
+
+/**
* A single packet of stream data.
*
* The structure size must be initialised before passing to avbin_read.
The
@@ -322,8 +408,9 @@
* Determine if AVbin includes a requested feature.
*
* When future versions of AVbin include more functionality, that
- * functionality can be tested for by calling this function. Currently
there
- * are no features to test for.
+ * functionality can be tested for by calling this function. The following
+ * features can be tested for:
+ * - "frame_rate"
*
* @retval 1 The feature is present
* @retval 0 The feature is not present

Modified: trunk/src/avbin.c
==============================================================================
--- trunk/src/avbin.c (original)
+++ trunk/src/avbin.c Thu Nov 27 01:41:18 2008
@@ -85,6 +85,8 @@

int avbin_have_feature(const char *feature)
{
+ if (strcmp(feature, "frame_rate") == 0)
+ return 1;
return 0;
}

@@ -187,11 +189,16 @@
AVbinStreamInfo *info)
{
AVCodecContext *context = file->context->streams[stream_index]->codec;
+ AVbinStreamInfo8 *info_8 = NULL;

- /* This is the first version, so anything smaller is an error. */
+ /* Error if not large enough for version 1 */
if (info->structure_size < sizeof *info)
return AVBIN_RESULT_ERROR;

+ /* Version 8 adds frame_rate feature */
+ if (info->structure_size >= sizeof(AVbinStreamInfo8))
+ info_8 = (AVbinStreamInfo8 *) info;
+
switch (context->codec_type)
{
case CODEC_TYPE_VIDEO:
@@ -200,6 +207,12 @@
info->video.height = context->height;
info->video.sample_aspect_num =
context->sample_aspect_ratio.num;
info->video.sample_aspect_den =
context->sample_aspect_ratio.den;
+ if (info_8)
+ {
+ /* Take reciprocal of time_base (period) to get frame_rate
*/
+ info_8->video.frame_rate_num = context->time_base.den;
+ info_8->video.frame_rate_den = context->time_base.num;
+ }
break;
case CODEC_TYPE_AUDIO:
info->type = AVBIN_STREAM_TYPE_AUDIO;

Reply all
Reply to author
Forward
0 new messages