Proper encoding (sort of), attempts at metadata transfer.

This commit is contained in:
2010-11-12 01:34:28 +01:00
parent 3b5e792499
commit 62041710e1
3 changed files with 37 additions and 13 deletions

View File

@@ -5,14 +5,15 @@
*/ */
#include "common.h" #include "common.h"
#include "decode.h"
struct decode_ctx { /*struct decode_ctx {
AVFormatContext *avfctx; AVFormatContext *avfctx;
int stream; int stream;
AVCodec *avc; AVCodec *avc;
char *bod; char *bod;
int len, offs; int len, offs;
}; };*/
struct decode_ctx *decode_open(const char *src) struct decode_ctx *decode_open(const char *src)
{ {
@@ -40,6 +41,7 @@ struct decode_ctx *decode_open(const char *src)
return NULL; return NULL;
} }
dump_format(fd->avfctx, 0, src, 0); dump_format(fd->avfctx, 0, src, 0);
int i; int i;
@@ -56,6 +58,7 @@ struct decode_ctx *decode_open(const char *src)
} }
fd->stream = i; fd->stream = i;
fd->metadata = fd->avfctx->streams[i]->metadata;
fd->avc = avcodec_find_decoder(fd->avfctx->streams[fd->stream]->codec->codec_id); fd->avc = avcodec_find_decoder(fd->avfctx->streams[fd->stream]->codec->codec_id);
if(!fd->avc) { if(!fd->avc) {

View File

@@ -7,7 +7,15 @@
#ifndef TCFS_DECODE_H #ifndef TCFS_DECODE_H
#define TCFS_DECODE_H 1 #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); struct decode_ctx *decode_open(const char *src);
int decode_read(char *buf, size_t size, struct decode_ctx *fd); int decode_read(char *buf, size_t size, struct decode_ctx *fd);

View File

@@ -5,8 +5,10 @@
*/ */
#include "common.h" #include "common.h"
#include <libavformat/avformat.h>
#include <../../ffmpeg/libavformat/metadata.h>
#include "util.h" #include "util.h"
#include "encode.h" #include "decode.h"
struct options options = { struct options options = {
"testbase", "testbase",
@@ -79,8 +81,7 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
enum CodecID codecid = av_guess_codec(ofmt, NULL, "test.mp3", NULL, AVMEDIA_TYPE_AUDIO); AVCodec *codec = avcodec_find_encoder_by_name("libmp3lame");
AVCodec *codec = avcodec_find_encoder(codecid);
if(!codec) { if(!codec) {
printf("Can't get codec.\n"); printf("Can't get codec.\n");
return -1; return -1;
@@ -92,17 +93,25 @@ int main(int argc, char *argv[])
// st->codec->codec = codec; // st->codec->codec = codec;
st->codec->codec_type = AVMEDIA_TYPE_AUDIO; st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
st->codec->codec_id = codecid; // st->codec->codec_id = codecid;
st->codec->channels = 2; st->codec->channels = 2;
st->codec->sample_fmt = AV_SAMPLE_FMT_S16; st->codec->sample_fmt = AV_SAMPLE_FMT_S16;
st->codec->sample_rate = 44100; st->codec->sample_rate = 44100;
st->codec->channel_layout = 0; st->codec->channel_layout = 0;
st->codec->flags |= CODEC_FLAG_QSCALE; // st->codec->flags |= CODEC_FLAG_QSCALE;
st->codec->global_quality = st->quality = 5; // st->codec->global_quality = 5;
st->codec->bit_rate = 256000;
st->codec->time_base = (AVRational){1, 44100}; st->codec->time_base = (AVRational){1, 44100};
st->codec->compression_level = FF_COMPRESSION_DEFAULT;
choose_sample_fmt(st, codec); choose_sample_fmt(st, codec);
choose_sample_rate(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) { if((ret=avcodec_open(st->codec, codec)) < 0) {
char err[512]; char err[512];
av_strerror(ret, 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); memset(ibuf, 0, sizeof(short)*st->codec->frame_size*st->codec->channels);
dump_format(avfctx, 0, "test.mp3", 1);
int obuf_size = st->codec->frame_size*2*av_get_bits_per_sample_fmt(st->codec->sample_fmt)/8;
obuf_size = (obuf_size<FF_MIN_BUFFER_SIZE)?(FF_MIN_BUFFER_SIZE):(obuf_size);
uint8_t obuf[4096]; uint8_t obuf[obuf_size];
int done = 0; int done = 0;
while(!done) { while(!done) {
memset(ibuf, 0, sizeof(short)*st->codec->frame_size*st->codec->channels); memset(ibuf, 0, sizeof(short)*st->codec->frame_size*st->codec->channels);
ret = decode_read(ibuf, sizeof(short)*st->codec->frame_size*st->codec->channels, decode); 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) if(ret != sizeof(short)*st->codec->frame_size*st->codec->channels)
done = 1; 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]; char err[512];
av_strerror(ret, err, 512); av_strerror(ret, err, 512);
fprintf(stderr, "ffmpeg error: %s\n", err); fprintf(stderr, "ffmpeg error: %s\n", err);
free(avfctx); free(avfctx);
return NULL; return NULL;
} }
// printf("ret: %u\n", ret);
if(ret != 0) { if(ret != 0) {
AVPacket pack; AVPacket pack;
av_new_packet(&pack, ret); av_new_packet(&pack, ret);
memcpy(pack.data, obuf, ret); memcpy(pack.data, obuf, ret);
av_write_frame(avfctx, &pack); av_interleaved_write_frame(avfctx, &pack);
} }
} }