diff options
| author | Tim Redfern <tim@herge.(none)> | 2013-04-12 17:23:15 +0100 |
|---|---|---|
| committer | Tim Redfern <tim@herge.(none)> | 2013-04-12 17:23:15 +0100 |
| commit | 4c99697c528e11a4195b572bf9f72f80c2fe3ea6 (patch) | |
| tree | 2bde485776fe5717e5de647094ed7ea18130b49d | |
| parent | 31d5bb487a6a245c80fb2154a8eca99c9ff4e6e6 (diff) | |
adding container
| -rw-r--r-- | libavformat_output-example.c | 527 | ||||
| -rwxr-xr-x | rotord/rotor.cpp | 153 | ||||
| -rw-r--r-- | testint.c | 199 |
3 files changed, 862 insertions, 17 deletions
diff --git a/libavformat_output-example.c b/libavformat_output-example.c new file mode 100644 index 0000000..496b7f8 --- /dev/null +++ b/libavformat_output-example.c @@ -0,0 +1,527 @@ +/* + * Copyright (c) 2003 Fabrice Bellard + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/** + * @file + * libavformat API example. + * + * @example libavformat/output-example.c + * Output a media file in any supported libavformat format. + * The default codecs are used. + */ + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <math.h> + +#include "libavutil/mathematics.h" +#include "libavformat/avformat.h" +#include "libswscale/swscale.h" + +/* 5 seconds stream duration */ +#define STREAM_DURATION 5.0 +#define STREAM_FRAME_RATE 25 /* 25 images/s */ +#define STREAM_NB_FRAMES ((int)(STREAM_DURATION * STREAM_FRAME_RATE)) +#define STREAM_PIX_FMT AV_PIX_FMT_YUV420P /* default pix_fmt */ + +static int sws_flags = SWS_BICUBIC; + +/**************************************************************/ +/* audio output */ + +static float t, tincr, tincr2; +static int16_t *samples; +static int audio_input_frame_size; + +/* + * add an audio output stream + */ +static AVStream *add_audio_stream(AVFormatContext *oc, enum AVCodecID codec_id) +{ + AVCodecContext *c; + AVStream *st; + AVCodec *codec; + + /* find the audio encoder */ + codec = avcodec_find_encoder(codec_id); + if (!codec) { + fprintf(stderr, "codec not found\n"); + exit(1); + } + + st = avformat_new_stream(oc, codec); + if (!st) { + fprintf(stderr, "Could not alloc stream\n"); + exit(1); + } + + c = st->codec; + + /* put sample parameters */ + c->sample_fmt = AV_SAMPLE_FMT_S16; + c->bit_rate = 64000; + c->sample_rate = 44100; + c->channels = 2; + + // some formats want stream headers to be separate + if (oc->oformat->flags & AVFMT_GLOBALHEADER) + c->flags |= CODEC_FLAG_GLOBAL_HEADER; + + return st; +} + +static void open_audio(AVFormatContext *oc, AVStream *st) +{ + AVCodecContext *c; + + c = st->codec; + + /* open it */ + if (avcodec_open2(c, NULL, NULL) < 0) { + fprintf(stderr, "could not open codec\n"); + exit(1); + } + + /* init signal generator */ + t = 0; + tincr = 2 * M_PI * 110.0 / c->sample_rate; + /* increment frequency by 110 Hz per second */ + tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate; + + if (c->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) + audio_input_frame_size = 10000; + else + audio_input_frame_size = c->frame_size; + samples = av_malloc(audio_input_frame_size * + av_get_bytes_per_sample(c->sample_fmt) * + c->channels); +} + +/* Prepare a 16 bit dummy audio frame of 'frame_size' samples and + * 'nb_channels' channels. */ +static void get_audio_frame(int16_t *samples, int frame_size, int nb_channels) +{ + int j, i, v; + int16_t *q; + + q = samples; + for (j = 0; j < frame_size; j++) { + v = (int)(sin(t) * 10000); + for (i = 0; i < nb_channels; i++) + *q++ = v; + t += tincr; + tincr += tincr2; + } +} + +static void write_audio_frame(AVFormatContext *oc, AVStream *st) +{ + AVCodecContext *c; + AVPacket pkt = { 0 }; // data and size must be 0; + AVFrame *frame = avcodec_alloc_frame(); + int got_packet; + + av_init_packet(&pkt); + c = st->codec; + + get_audio_frame(samples, audio_input_frame_size, c->channels); + frame->nb_samples = audio_input_frame_size; + avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt, + (uint8_t *)samples, + audio_input_frame_size * + av_get_bytes_per_sample(c->sample_fmt) * + c->channels, 1); + + avcodec_encode_audio2(c, &pkt, frame, &got_packet); + if (!got_packet) + return; + + pkt.stream_index = st->index; + + /* Write the compressed frame to the media file. */ + if (av_interleaved_write_frame(oc, &pkt) != 0) { + fprintf(stderr, "Error while writing audio frame\n"); + exit(1); + } + avcodec_free_frame(&frame); +} + +static void close_audio(AVFormatContext *oc, AVStream *st) +{ + avcodec_close(st->codec); + + av_free(samples); +} + +/**************************************************************/ +/* video output */ + +static AVFrame *picture, *tmp_picture; +static int frame_count; + +/* Add a video output stream. */ +static AVStream *add_video_stream(AVFormatContext *oc, enum AVCodecID codec_id) +{ + AVCodecContext *c; + AVStream *st; + AVCodec *codec; + + /* find the video encoder */ + codec = avcodec_find_encoder(codec_id); + if (!codec) { + fprintf(stderr, "codec not found\n"); + exit(1); + } + + st = avformat_new_stream(oc, codec); + if (!st) { + fprintf(stderr, "Could not alloc stream\n"); + exit(1); + } + + c = st->codec; + + /* Put sample parameters. */ + c->bit_rate = 400000; + /* Resolution must be a multiple of two. */ + c->width = 352; + c->height = 288; + /* timebase: This is the fundamental unit of time (in seconds) in terms + * of which frame timestamps are represented. For fixed-fps content, + * timebase should be 1/framerate and timestamp increments should be + * identical to 1. */ + c->time_base.den = STREAM_FRAME_RATE; + c->time_base.num = 1; + c->gop_size = 12; /* emit one intra frame every twelve frames at most */ + c->pix_fmt = STREAM_PIX_FMT; + if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) { + /* just for testing, we also add B frames */ + c->max_b_frames = 2; + } + if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO) { + /* Needed to avoid using macroblocks in which some coeffs overflow. + * This does not happen with normal video, it just happens here as + * the motion of the chroma plane does not match the luma plane. */ + c->mb_decision = 2; + } + /* Some formats want stream headers to be separate. */ + if (oc->oformat->flags & AVFMT_GLOBALHEADER) + c->flags |= CODEC_FLAG_GLOBAL_HEADER; + + return st; +} + +static AVFrame *alloc_picture(enum AVPixelFormat pix_fmt, int width, int height) +{ + AVFrame *picture; + uint8_t *picture_buf; + int size; + + picture = avcodec_alloc_frame(); + if (!picture) + return NULL; + size = avpicture_get_size(pix_fmt, width, height); + picture_buf = av_malloc(size); + if (!picture_buf) { + av_free(picture); + return NULL; + } + avpicture_fill((AVPicture *)picture, picture_buf, + pix_fmt, width, height); + return picture; +} + +static void open_video(AVFormatContext *oc, AVStream *st) +{ + AVCodecContext *c; + + c = st->codec; + + /* open the codec */ + if (avcodec_open2(c, NULL, NULL) < 0) { + fprintf(stderr, "could not open codec\n"); + exit(1); + } + + /* Allocate the encoded raw picture. */ + picture = alloc_picture(c->pix_fmt, c->width, c->height); + if (!picture) { + fprintf(stderr, "Could not allocate picture\n"); + exit(1); + } + + /* If the output format is not YUV420P, then a temporary YUV420P + * picture is needed too. It is then converted to the required + * output format. */ + tmp_picture = NULL; + if (c->pix_fmt != AV_PIX_FMT_YUV420P) { + tmp_picture = alloc_picture(AV_PIX_FMT_YUV420P, c->width, c->height); + if (!tmp_picture) { + fprintf(stderr, "Could not allocate temporary picture\n"); + exit(1); + } + } +} + +/* Prepare a dummy image. */ +static void fill_yuv_image(AVFrame *pict, int frame_index, + int width, int height) +{ + int x, y, i; + + i = frame_index; + + /* Y */ + for (y = 0; y < height; y++) + for (x = 0; x < width; x++) + pict->data[0][y * pict->linesize[0] + x] = x + y + i * 3; + + /* Cb and Cr */ + for (y = 0; y < height / 2; y++) { + for (x = 0; x < width / 2; x++) { + pict->data[1][y * pict->linesize[1] + x] = 128 + y + i * 2; + pict->data[2][y * pict->linesize[2] + x] = 64 + x + i * 5; + } + } +} + +static void write_video_frame(AVFormatContext *oc, AVStream *st) +{ + int ret; + AVCodecContext *c; + static struct SwsContext *img_convert_ctx; + + c = st->codec; + + if (frame_count >= STREAM_NB_FRAMES) { + /* No more frames to compress. The codec has a latency of a few + * frames if using B-frames, so we get the last frames by + * passing the same picture again. */ + } else { + if (c->pix_fmt != AV_PIX_FMT_YUV420P) { + /* as we only generate a YUV420P picture, we must convert it + * to the codec pixel format if needed */ + if (img_convert_ctx == NULL) { + img_convert_ctx = sws_getContext(c->width, c->height, + AV_PIX_FMT_YUV420P, + c->width, c->height, + c->pix_fmt, + sws_flags, NULL, NULL, NULL); + if (img_convert_ctx == NULL) { + fprintf(stderr, + "Cannot initialize the conversion context\n"); + exit(1); + } + } + fill_yuv_image(tmp_picture, frame_count, c->width, c->height); + sws_scale(img_convert_ctx, tmp_picture->data, tmp_picture->linesize, + 0, c->height, picture->data, picture->linesize); + } else { + fill_yuv_image(picture, frame_count, c->width, c->height); + } + } + + if (oc->oformat->flags & AVFMT_RAWPICTURE) { + /* Raw video case - the API will change slightly in the near + * future for that. */ + AVPacket pkt; + av_init_packet(&pkt); + + pkt.flags |= AV_PKT_FLAG_KEY; + pkt.stream_index = st->index; + pkt.data = (uint8_t *)picture; + pkt.size = sizeof(AVPicture); + + ret = av_interleaved_write_frame(oc, &pkt); + } else { + AVPacket pkt = { 0 }; + int got_packet; + av_init_packet(&pkt); + + /* encode the image */ + ret = avcodec_encode_video2(c, &pkt, picture, &got_packet); + /* If size is zero, it means the image was buffered. */ + if (!ret && got_packet && pkt.size) { + if (pkt.pts != AV_NOPTS_VALUE) { + pkt.pts = av_rescale_q(pkt.pts, + c->time_base, st->time_base); + } + if (pkt.dts != AV_NOPTS_VALUE) { + pkt.dts = av_rescale_q(pkt.dts, + c->time_base, st->time_base); + } + pkt.stream_index = st->index; + + /* Write the compressed frame to the media file. */ + ret = av_interleaved_write_frame(oc, &pkt); + } else { + ret = 0; + } + } + if (ret != 0) { + fprintf(stderr, "Error while writing video frame\n"); + exit(1); + } + frame_count++; +} + +static void close_video(AVFormatContext *oc, AVStream *st) +{ + avcodec_close(st->codec); + av_free(picture->data[0]); + av_free(picture); + if (tmp_picture) { + av_free(tmp_picture->data[0]); + av_free(tmp_picture); + } +} + +/**************************************************************/ +/* media file output */ + +int main(int argc, char **argv) +{ + const char *filename; + AVOutputFormat *fmt; + AVFormatContext *oc; + AVStream *audio_st, *video_st; + double audio_pts, video_pts; + int i; + + /* Initialize libavcodec, and register all codecs and formats. */ + av_register_all(); + + if (argc != 2) { + printf("usage: %s output_file\n" + "API example program to output a media file with libavformat.\n" + "The output format is automatically guessed according to the file extension.\n" + "Raw images can also be output by using '%%d' in the filename\n" + "\n", argv[0]); + return 1; + } + + filename = argv[1]; + + /* Autodetect the output format from the name. default is MPEG. */ + fmt = av_guess_format(NULL, filename, NULL); + if (!fmt) { + printf("Could not deduce output format from file extension: using MPEG.\n"); + fmt = av_guess_format("mpeg", NULL, NULL); + } + if (!fmt) { + fprintf(stderr, "Could not find suitable output format\n"); + return 1; + } + + /* Allocate the output media context. */ + oc = avformat_alloc_context(); + if (!oc) { + fprintf(stderr, "Memory error\n"); + return 1; + } + oc->oformat = fmt; + snprintf(oc->filename, sizeof(oc->filename), "%s", filename); + + /* Add the audio and video streams using the default format codecs + * and initialize the codecs. */ + video_st = NULL; + audio_st = NULL; + if (fmt->video_codec != AV_CODEC_ID_NONE) { + video_st = add_video_stream(oc, fmt->video_codec); + } + if (fmt->audio_codec != AV_CODEC_ID_NONE) { + audio_st = add_audio_stream(oc, fmt->audio_codec); + } + + /* Now that all the parameters are set, we can open the audio and + * video codecs and allocate the necessary encode buffers. */ + if (video_st) + open_video(oc, video_st); + if (audio_st) + open_audio(oc, audio_st); + + av_dump_format(oc, 0, filename, 1); + + /* open the output file, if needed */ + if (!(fmt->flags & AVFMT_NOFILE)) { + if (avio_open(&oc->pb, filename, AVIO_FLAG_WRITE) < 0) { + fprintf(stderr, "Could not open '%s'\n", filename); + return 1; + } + } + + /* Write the stream header, if any. */ + avformat_write_header(oc, NULL); + + for (;;) { + /* Compute current audio and video time. */ + if (audio_st) + audio_pts = (double)audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den; + else + audio_pts = 0.0; + + if (video_st) + video_pts = (double)video_st->pts.val * video_st->time_base.num / + video_st->time_base.den; + else + video_pts = 0.0; + + if ((!audio_st || audio_pts >= STREAM_DURATION) && + (!video_st || video_pts >= STREAM_DURATION)) + break; + + /* write interleaved audio and video frames */ + if (!video_st || (video_st && audio_st && audio_pts < video_pts)) { + write_audio_frame(oc, audio_st); + } else { + write_video_frame(oc, video_st); + } + } + + /* Write the trailer, if any. The trailer must be written before you + * close the CodecContexts open when you wrote the header; otherwise + * av_write_trailer() may try to use memory that was freed on + * av_codec_close(). */ + av_write_trailer(oc); + + /* Close each codec. */ + if (video_st) + close_video(oc, video_st); + if (audio_st) + close_audio(oc, audio_st); + + /* Free the streams. */ + for (i = 0; i < oc->nb_streams; i++) { + av_freep(&oc->streams[i]->codec); + av_freep(&oc->streams[i]); + } + + if (!(fmt->flags & AVFMT_NOFILE)) + /* Close the output file. */ + avio_close(oc->pb); + + /* free the stream */ + av_free(oc); + + return 0; +} diff --git a/rotord/rotor.cpp b/rotord/rotor.cpp index e0c91dd..ee170f0 100755 --- a/rotord/rotor.cpp +++ b/rotord/rotor.cpp @@ -605,8 +605,6 @@ void Audio_analysis::print_features(){ cerr<<i.second<<" "<<i.first<<endl; } } -bool Video_output::render(const float duration, const float framerate,const string &output_filename,const string &audio_filename){ - //render out the network /* //testing signal routes cerr << "Rotor: Signal_output rendering " << duration << " seconds at " << framerate << " frames per second" << endl; @@ -621,6 +619,11 @@ bool Video_output::render(const float duration, const float framerate,const stri } return true; */ + +/* +bool Video_output::render(const float duration, const float framerate,const string &output_filename,const string &audio_filename){ + //render out the network + //set up output context //then iterate through frames //querying graph at each frame @@ -636,7 +639,6 @@ bool Video_output::render(const float duration, const float framerate,const stri cerr << "Rotor: rendering " << output_filename << " , " << duration << " seconds at " << framerate << " frames per second" << endl; - /* find the mpeg1 video encoder */ codec = avcodec_find_encoder(AV_CODEC_ID_H264); if (!codec) { cerr<< "codec not found" << endl; @@ -646,21 +648,21 @@ bool Video_output::render(const float duration, const float framerate,const stri c= avcodec_alloc_context3(codec); picture= avcodec_alloc_frame(); - /* put sample parameters */ + // put sample parameters / c->bit_rate = 400000; - /* resolution must be a multiple of two */ + // resolution must be a multiple of two / c->width = 640; c->height = 480; - /* frames per second */ + // frames per second / c->time_base= (AVRational){1,25}; - c->gop_size = 10; /* emit one intra frame every ten frames */ + c->gop_size = 10; // emit one intra frame every ten frames / c->max_b_frames=1; c->pix_fmt = PIX_FMT_YUV420P; //AV_PIX_FMT_RGB24 AVDictionary *options; //= NULL; causes a forward declaration error!? options=NULL; - /* open it */ + // open it / if (avcodec_open2(c, codec, &options) < 0) { cerr << "could not open codec" << endl; return false; @@ -672,11 +674,11 @@ bool Video_output::render(const float duration, const float framerate,const stri return false; } - /* alloc image and output buffer */ + // alloc image and output buffer/ outbuf_size = 100000; outbuf = malloc(outbuf_size); size = c->width * c->height; - picture_buf = malloc((size * 3) / 2); /* size for YUV 420 */ + picture_buf = malloc((size * 3) / 2); // size for YUV 420 / picture->data[0] = picture_buf; picture->data[1] = picture->data[0] + size; @@ -685,18 +687,18 @@ bool Video_output::render(const float duration, const float framerate,const stri picture->linesize[1] = c->width / 2; picture->linesize[2] = c->width / 2; - /* encode 1 second of video */ + // encode 1 second of video / for(i=0;i<250;i++) { fflush(stdout); - /* prepare a dummy image */ - /* Y */ + // prepare a dummy image / + // Y / for(y=0;y<c->height;y++) { for(x=0;x<c->width;x++) { picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3; } } - /* Cb and Cr */ + // Cb and Cr / for(y=0;y<c->height/2;y++) { for(x=0;x<c->width/2;x++) { picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2; @@ -704,13 +706,13 @@ bool Video_output::render(const float duration, const float framerate,const stri } } - /* encode the image */ + // encode the image / out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture); printf("encoding frame %3d (size=%5d)\n", i, out_size); fwrite(outbuf, 1, out_size, f); } - /* get the delayed frames */ + // get the delayed frames / for(; out_size; i++) { fflush(stdout); @@ -719,7 +721,7 @@ bool Video_output::render(const float duration, const float framerate,const stri fwrite(outbuf, 1, out_size, f); } - /* add sequence end code to have a real mpeg file */ + // add sequence end code to have a real mpeg file / outbuf[0] = 0x00; outbuf[1] = 0x00; outbuf[2] = 0x01; @@ -737,3 +739,120 @@ bool Video_output::render(const float duration, const float framerate,const stri return true; } +*/ +bool Video_output::render(const float duration, const float framerate,const string &output_filename,const string &audio_filename){ + + AVOutputFormat *fmt; + AVFormatContext *oc; + AVStream *audio_st, *video_st; + double audio_pts, video_pts; + int i; + + /* Initialize libavcodec, and register all codecs and formats. */ + av_register_all(); + //think about this: when to register and unregister? + + + /* Autodetect the output format from the name. default is MPEG. */ + fmt = av_guess_format(NULL, output_filename.c_str(), NULL); + if (!fmt) { + printf("Could not deduce output format from file extension: using MPEG.\n"); + fmt = av_guess_format("mpeg", NULL, NULL); + } + if (!fmt) { + cerr << "Rotor: could not find suitable output format" << endl; + return false; + } + + /* Allocate the output media context. */ + oc = avformat_alloc_context(); + if (!oc) { + cerr <<"Rotor: memory error"<< endl; + return false; + } + oc->oformat = fmt; + snprintf(oc->filename, sizeof(oc->filename), "%s", filename); + + /* Add the audio and video streams using the default format codecs + * and initialize the codecs. */ + video_st = NULL; + audio_st = NULL; + if (fmt->video_codec != AV_CODEC_ID_NONE) { + video_st = add_video_stream(oc, fmt->video_codec); + } + if (fmt->audio_codec != AV_CODEC_ID_NONE) { + audio_st = add_audio_stream(oc, fmt->audio_codec); + } + + /* Now that all the parameters are set, we can open the audio and + * video codecs and allocate the necessary encode buffers. */ + if (video_st) + open_video(oc, video_st); + if (audio_st) + open_audio(oc, audio_st); + + av_dump_format(oc, 0, filename, 1); + + /* open the output file, if needed */ + if (!(fmt->flags & AVFMT_NOFILE)) { + if (avio_open(&oc->pb, filename, AVIO_FLAG_WRITE) < 0) { + fprintf(stderr, "Could not open '%s'\n", filename); + return 1; + } + } + + /* Write the stream header, if any. */ + avformat_write_header(oc, NULL); + + for (;;) { + /* Compute current audio and video time. */ + if (audio_st) + audio_pts = (double)audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den; + else + audio_pts = 0.0; + + if (video_st) + video_pts = (double)video_st->pts.val * video_st->time_base.num / + video_st->time_base.den; + else + video_pts = 0.0; + + if ((!audio_st || audio_pts >= STREAM_DURATION) && + (!video_st || video_pts >= STREAM_DURATION)) + break; + + /* write interleaved audio and video frames */ + if (!video_st || (video_st && audio_st && audio_pts < video_pts)) { + write_audio_frame(oc, audio_st); + } else { + write_video_frame(oc, video_st); + } + } + + /* Write the trailer, if any. The trailer must be written before you + * close the CodecContexts open when you wrote the header; otherwise + * av_write_trailer() may try to use memory that was freed on + * av_codec_close(). */ + av_write_trailer(oc); + + /* Close each codec. */ + if (video_st) + close_video(oc, video_st); + if (audio_st) + close_audio(oc, audio_st); + + /* Free the streams. */ + for (i = 0; i < oc->nb_streams; i++) { + av_freep(&oc->streams[i]->codec); + av_freep(&oc->streams[i]); + } + + if (!(fmt->flags & AVFMT_NOFILE)) + /* Close the output file. */ + avio_close(oc->pb); + + /* free the stream */ + av_free(oc); + + return 0; +}
\ No newline at end of file diff --git a/testint.c b/testint.c new file mode 100644 index 0000000..6521ba3 --- /dev/null +++ b/testint.c @@ -0,0 +1,199 @@ +extern "C" { + #include "libavcodec/avcodec.h" + #include "libavformat/avformat.h" + #include "libavutil/opt.h" + #include "libavutil/channel_layout.h" + #include "libavutil/common.h" + #include "libavutil/imgutils.h" + #include "libavutil/mathematics.h" + #include "libavutil/samplefmt.h" + + #include <libavutil/imgutils.h> + #include <libavutil/samplefmt.h> + //#include <libavutil/timestamp.h> +} + +#include <libavutil/imgutils.h> +#include <libavutil/samplefmt.h> + +#include <iostream> +#include <fstream> + + +int main(int argc, char** argv) +{ + av_register_all(); + + if (argc<1) { + cerr<< "use: test audiofile" << endl; + return 1; + } + + + AVFrame* frame = avcodec_alloc_frame(); + if (!frame) + { + std::cout << "Error allocating the frame" << std::endl; + return false; + } + + AVFormatContext* formatContext = NULL; + if (avformat_open_input(&formatContext, filename.c_str(), NULL, NULL) != 0) + { + av_free(frame); + std::cout << "Error opening the file" << std::endl; + return false; + } + + + if (avformat_find_stream_info(formatContext, NULL) < 0) + { + av_free(frame); + av_close_input_file(formatContext); + std::cout << "Error finding the stream info" << std::endl; + return false; + } + + AVStream* audioStream = NULL; + // Find the audio stream (some container files can have multiple streams in them) + for (unsigned int i = 0; i < formatContext->nb_streams; ++i) + { + if (formatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) + { + audioStream = formatContext->streams[i]; + break; + } + } + + if (audioStream == NULL) + { + av_free(frame); + av_close_input_file(formatContext); + std::cout << "Could not find any audio stream in the file" << std::endl; + return false; + } + + AVCodecContext* codecContext = audioStream->codec; + + codecContext->codec = avcodec_find_decoder(codecContext->codec_id); + if (codecContext->codec == NULL) + { + av_free(frame); + av_close_input_file(formatContext); + std::cout << "Couldn't find a proper decoder" << std::endl; + return false; + } + else if (avcodec_open2(codecContext, codecContext->codec, NULL) != 0) + { + av_free(frame); + av_close_input_file(formatContext); + std::cout << "Couldn't open the context with the decoder" << std::endl; + return false; + } + + /* + + // + // why is the file truncated??? + //if(codecContext->codec->capabilities & CODEC_CAP_TRUNCATED) codecContext->codec->capabilities|=CODEC_FLAG_TRUNCATED; + // + // + + av_dump_format(formatContext, 0, 0, false); //avformat.h line 1256 + int samples = ((formatContext->duration + 5000)*codecContext->sample_rate)/AV_TIME_BASE; + + std::cout << "This stream has " << codecContext->channels << " channels, a sample rate of " << codecContext->sample_rate << "Hz and "<<samples <<" samples" << std::endl; + std::cout << "The data is in format " <<codecContext->sample_fmt<< " (aka "<< av_get_sample_fmt_name(codecContext->sample_fmt) << ") "<<std::endl; + + //we can now tell the processors the format + //we can work out the number of samples at this point + + for (auto p: processors) { + p->init(codecContext->channels,16,samples); + } + + AVPacket packet; + av_init_packet(&packet); + int sample_processed=0; + + bool diag=true; + + // Read the packets in a loop + while (true) + //while(sample_processed<samples) + { + int ret=av_read_frame(formatContext, &packet); + if (ret<0) { + cerr << "finished with code "<<ret <<(ret==AVERROR_EOF?" ,EOF":"")<<endl; + break; + } + //av_read_frame(formatContext, &packet); //hangs once the packets have been read + if (packet.stream_index == audioStream->index) + { + // Try to decode the packet into a frame + int frameFinished = 0; + int bytes = avcodec_decode_audio4(codecContext, frame, &frameFinished, &packet); + + // Some frames rely on multiple packets, so we have to make sure the frame is finished before + // we can use it + if (frameFinished) + { + // frame now has usable audio data in it. How it's stored in the frame depends on the format of + // the audio. If it's packed audio, all the data will be in frame->data[0]. If it's in planar format, + // the data will be in frame->data and possibly frame->extended_data. Look at frame->data, frame->nb_samples, + // frame->linesize, and other related fields on the FFmpeg docs. I don't know how you're actually using + // the audio data, so I won't add any junk here that might confuse you. Typically, if I want to find + // documentation on an FFmpeg structure or function, I just type "<name> doxygen" into google (like + // "AVFrame doxygen" for AVFrame's docs) + + //av_get_channel_layout_string (char *buf, int buf_size, int nb_channels, uint64_t channel_layout) + + if (diag) { + cerr << "first frame: "<<bytes << ", "<<frame->nb_samples<<" samples in "<<av_get_sample_fmt_name(frame->format)<<" format with channel layout "<<frame->channel_layout<< std::endl; + diag=false; + } + + //std::cout << "Got a frame: bytes " << bytes << ", "<<frame->nb_samples<<" samples"<<std::endl; + //now we can pass the data to the processor(s) + for (auto p: processors) { + sample_processed=p->process_frame(frame->data[0],frame->nb_samples); + } + + mutex.lock(); + progress=((double)sample_processed)/samples; + mutex.unlock(); + } + } + // You *must* call av_free_packet() after each call to av_read_frame() or else you'll leak memory + av_free_packet(&packet); //crashes here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! SIGSEV In _int_free (av=0xb4600010, p=0xb46025c8, have_lock=0) at malloc.c:4085 () + } + + // Some codecs will cause frames to be buffered up in the decoding process. If the CODEC_CAP_DELAY flag + // is set, there can be buffered up frames that need to be flushed, so we'll do that + if (codecContext->codec->capabilities & CODEC_CAP_DELAY) + { + av_init_packet(&packet); + // Decode all the remaining frames in the buffer, until the end is reached + int frameFinished = 0; + int bytes = avcodec_decode_audio4(codecContext, frame, &frameFinished, &packet); + while (bytes >= 0 && frameFinished) + { + for (auto p: processors) { + p->process_frame(frame->data[0],frame->nb_samples); + } + mutex.lock(); + progress=((double)sample_processed)/samples; + mutex.unlock(); + } + } + + cerr << "finished processed: "<<sample_processed << " samples of "<<samples<<" , "<<((double)sample_processed*100)/samples<<"%"<< std::endl; + */ + + // Clean up! + av_free(frame); + avcodec_close(codecContext); + av_close_input_file(formatContext); + + return 0; +} |
