From 62041710e1e6458607b7250dc1a0bcfd7c864f9b Mon Sep 17 00:00:00 2001 From: Matthias Blankertz Date: Fri, 12 Nov 2010 01:34:28 +0100 Subject: [PATCH] Proper encoding (sort of), attempts at metadata transfer. --- src/decode.c | 7 +++++-- src/decode.h | 10 +++++++++- src/test_encode.c | 33 +++++++++++++++++++++++---------- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/decode.c b/src/decode.c index 0ba0c63..380ceef 100644 --- a/src/decode.c +++ b/src/decode.c @@ -5,14 +5,15 @@ */ #include "common.h" +#include "decode.h" -struct decode_ctx { +/*struct decode_ctx { AVFormatContext *avfctx; int stream; AVCodec *avc; char *bod; int len, offs; -}; + };*/ struct decode_ctx *decode_open(const char *src) { @@ -40,6 +41,7 @@ struct decode_ctx *decode_open(const char *src) return NULL; } + dump_format(fd->avfctx, 0, src, 0); int i; @@ -56,6 +58,7 @@ struct decode_ctx *decode_open(const char *src) } fd->stream = i; + fd->metadata = fd->avfctx->streams[i]->metadata; fd->avc = avcodec_find_decoder(fd->avfctx->streams[fd->stream]->codec->codec_id); if(!fd->avc) { diff --git a/src/decode.h b/src/decode.h index 009726a..8e678c8 100644 --- a/src/decode.h +++ b/src/decode.h @@ -7,7 +7,15 @@ #ifndef TCFS_DECODE_H #define TCFS_DECODE_H 1 -struct decode_ctx; +struct decode_ctx +{ + AVFormatContext *avfctx; + int stream; + AVCodec *avc; + char *bod; + int len, offs; + void *metadata; +}; struct decode_ctx *decode_open(const char *src); int decode_read(char *buf, size_t size, struct decode_ctx *fd); diff --git a/src/test_encode.c b/src/test_encode.c index c1c775c..ba8e309 100644 --- a/src/test_encode.c +++ b/src/test_encode.c @@ -5,8 +5,10 @@ */ #include "common.h" +#include +#include <../../ffmpeg/libavformat/metadata.h> #include "util.h" -#include "encode.h" +#include "decode.h" struct options options = { "testbase", @@ -79,8 +81,7 @@ int main(int argc, char *argv[]) return -1; } - enum CodecID codecid = av_guess_codec(ofmt, NULL, "test.mp3", NULL, AVMEDIA_TYPE_AUDIO); - AVCodec *codec = avcodec_find_encoder(codecid); + AVCodec *codec = avcodec_find_encoder_by_name("libmp3lame"); if(!codec) { printf("Can't get codec.\n"); return -1; @@ -92,17 +93,25 @@ int main(int argc, char *argv[]) // st->codec->codec = codec; st->codec->codec_type = AVMEDIA_TYPE_AUDIO; - st->codec->codec_id = codecid; + // st->codec->codec_id = codecid; st->codec->channels = 2; st->codec->sample_fmt = AV_SAMPLE_FMT_S16; st->codec->sample_rate = 44100; st->codec->channel_layout = 0; - st->codec->flags |= CODEC_FLAG_QSCALE; - st->codec->global_quality = st->quality = 5; + // st->codec->flags |= CODEC_FLAG_QSCALE; + // st->codec->global_quality = 5; + st->codec->bit_rate = 256000; st->codec->time_base = (AVRational){1, 44100}; + st->codec->compression_level = FF_COMPRESSION_DEFAULT; choose_sample_fmt(st, codec); choose_sample_rate(st, codec); + avfctx->metadata = decode->metadata; + AVMetadataTag *tag = NULL; + while((tag=av_metadata_get(decode->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX))) { + printf("%s: %s\n", tag->key, tag->value); + } + if((ret=avcodec_open(st->codec, codec)) < 0) { char err[512]; av_strerror(ret, err, 512); @@ -134,28 +143,32 @@ int main(int argc, char *argv[]) } memset(ibuf, 0, sizeof(short)*st->codec->frame_size*st->codec->channels); + dump_format(avfctx, 0, "test.mp3", 1); - - uint8_t obuf[4096]; + int obuf_size = st->codec->frame_size*2*av_get_bits_per_sample_fmt(st->codec->sample_fmt)/8; + obuf_size = (obuf_sizecodec->frame_size*st->codec->channels); ret = decode_read(ibuf, sizeof(short)*st->codec->frame_size*st->codec->channels, decode); if(ret != sizeof(short)*st->codec->frame_size*st->codec->channels) done = 1; - if((ret = avcodec_encode_audio(st->codec, obuf, 4096, ibuf)) < 0) { + if((ret = avcodec_encode_audio(st->codec, obuf, obuf_size, ibuf)) < 0) { char err[512]; av_strerror(ret, err, 512); fprintf(stderr, "ffmpeg error: %s\n", err); free(avfctx); return NULL; } + // printf("ret: %u\n", ret); if(ret != 0) { AVPacket pack; av_new_packet(&pack, ret); memcpy(pack.data, obuf, ret); - av_write_frame(avfctx, &pack); + av_interleaved_write_frame(avfctx, &pack); } }