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 "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) {

View File

@@ -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);

View File

@@ -5,8 +5,10 @@
*/
#include "common.h"
#include <libavformat/avformat.h>
#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_size<FF_MIN_BUFFER_SIZE)?(FF_MIN_BUFFER_SIZE):(obuf_size);
uint8_t obuf[obuf_size];
int done = 0;
while(!done) {
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);
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);
}
}