From d2458728cd226a2651005acfd3241e1a5f77782c Mon Sep 17 00:00:00 2001 From: deva Date: Tue, 26 Apr 2005 09:32:28 +0000 Subject: Tested some audio output code... didn't work... tesing it again later. --- src/dv1394.cc | 6 +- src/error.cc | 7 +- src/mov_encoder.cc | 184 ++++++++++++++++++++++++++++++++++++++++++++--------- src/mov_encoder.h | 3 + 4 files changed, 165 insertions(+), 35 deletions(-) diff --git a/src/dv1394.cc b/src/dv1394.cc index d4912e4..004cb3d 100644 --- a/src/dv1394.cc +++ b/src/dv1394.cc @@ -69,7 +69,7 @@ static int raw_reader( raw1394handle_t handle, int channel, size_t length, quadl if(!framedata) { // We're fucked - // errobj->pushError("Were fucked: %s.\n", strerror( errno ) ); + fprintf(stderr, "Framedata allocation error: %s.\n", strerror( errno ) ); fflush(stderr); exit(1); } } @@ -142,12 +142,12 @@ dv1394::dv1394(Error *e, int port, int channel) dv1394::~dv1394() { // Close firewire connection. - raw1394_destroy_handle(handle); + if(handle) raw1394_destroy_handle(handle); } unsigned char *dv1394::readFrame() { - // Firewiare port not correctly opened. + // Firewire port not correctly opened. if(!handle) return NULL; unsigned char *ptr; diff --git a/src/error.cc b/src/error.cc index 34c8e33..d1d351d 100644 --- a/src/error.cc +++ b/src/error.cc @@ -66,12 +66,11 @@ void Error::pushError(char *fmt, ...) char buf[1024]; pthread_mutex_lock(&mutex); + // Beginning of safezone va_list argp; va_start(argp, fmt); - fprintf(stderr, "New Error: ["); - vfprintf(stderr, fmt, argp); - fprintf(stderr, "]\n"); fflush(stderr); + fprintf(stderr, "New Error: ["); vfprintf(stderr, fmt, argp); fprintf(stderr, "]\n"); fflush(stderr); vsprintf(buf, fmt, argp); va_end(argp); @@ -80,6 +79,8 @@ void Error::pushError(char *fmt, ...) err->errstr = string(buf); err->prev = lastError; lastError = err; + + // End of safezone pthread_mutex_unlock(&mutex); } diff --git a/src/mov_encoder.cc b/src/mov_encoder.cc index 2f1810f..52702a5 100644 --- a/src/mov_encoder.cc +++ b/src/mov_encoder.cc @@ -37,7 +37,7 @@ MovEncoder::MovEncoder(const char *filename) AVCodec *enc_codec; if(!(efc = av_alloc_format_context())) { - fprintf(stderr, "Could not alloc output format context\n"); + fprintf(stderr, "Could not alloc output format context\n"); fflush(stderr); exit(1); } @@ -45,21 +45,21 @@ MovEncoder::MovEncoder(const char *filename) //efc->oformat = guess_format(NULL, filename, NULL); if(!(st = av_new_stream(efc, 0))) { - fprintf(stderr, "Could not alloc stream\n"); + fprintf(stderr, "Could not alloc stream\n"); fflush(stderr); switch((int)st) { - case AVERROR_UNKNOWN : printf("unknown error\n"); + case AVERROR_UNKNOWN : fprintf(stderr, "unknown error\n"); fflush(stderr); break; - case AVERROR_IO : printf("i/o error\n"); + case AVERROR_IO : fprintf(stderr, "i/o error\n"); fflush(stderr); break; - case AVERROR_NUMEXPECTED : printf("number syntax expected in filename\n"); + case AVERROR_NUMEXPECTED : fprintf(stderr, "number syntax expected in filename\n"); fflush(stderr); break; - case AVERROR_INVALIDDATA : printf("invalid data found\n"); + case AVERROR_INVALIDDATA : fprintf(stderr, "invalid data found\n"); fflush(stderr); break; - case AVERROR_NOMEM : printf("not enough memory\n"); + case AVERROR_NOMEM : fprintf(stderr, "not enough memory\n"); fflush(stderr); break; - case AVERROR_NOFMT : printf("unknown format\n"); + case AVERROR_NOFMT : fprintf(stderr, "unknown format\n"); fflush(stderr); break; - case AVERROR_NOTSUPP : printf("operation not supported\n"); + case AVERROR_NOTSUPP : fprintf(stderr, "operation not supported\n"); fflush(stderr); break; } exit(1); @@ -67,7 +67,7 @@ MovEncoder::MovEncoder(const char *filename) enc_codec = avcodec_find_encoder(CODEC_ID_MPEG2VIDEO); if(!enc_codec) { - printf("Unsupported codec for output stream\n"); + fprintf(stderr, "Unsupported codec for output stream\n"); fflush(stderr); exit(1); } avcodec_get_context_defaults(&st->codec); @@ -109,28 +109,28 @@ MovEncoder::MovEncoder(const char *filename) ecc->me_method = ME_EPZS; if(avcodec_open(&st->codec, enc_codec) < 0) { - printf("Error while opening codec for stream\n"); + fprintf(stderr, "Error while opening codec for stream\n"); fflush(stderr); exit(1); } if(url_fopen(&efc->pb, filename, URL_RDWR) < 0) { - fprintf(stderr, "Could not open '%s'\n", filename); + fprintf(stderr, "Could not open '%s'\n", filename); fflush(stderr); exit(1); } if(av_set_parameters(efc, NULL) < 0) { - fprintf(stderr, "%s: Invalid encoding parameters\n", filename); + fprintf(stderr, "%s: Invalid encoding parameters\n", filename); fflush(stderr); exit(1); } dump_format(efc, 0, filename, 1); if(av_write_header(efc) < 0) { - fprintf(stderr, "Could not write header for output file \n"); + fprintf(stderr, "Could not write header for output file \n"); fflush(stderr); exit(1); } - video_buffer = (unsigned char *)av_malloc(VIDEO_BUFFER_SIZE); ALLOC(dcc, "mov_encoder, video_buffer"); + video_buffer = (unsigned char *)av_malloc(VIDEO_BUFFER_SIZE); av_init_packet(&epkt); @@ -141,20 +141,20 @@ MovEncoder::MovEncoder(const char *filename) //////////////////// DECODE INIT AVCodec *deccodec; // AVCodecContext *dcc= NULL; - printf("Video decoding\n"); + fprintf(stderr, "Video decoding\n"); - /* find the dvvideo decoder */ + // find the dvvideo decoder deccodec = avcodec_find_decoder(CODEC_ID_DVVIDEO); if (!deccodec) { - fprintf(stderr, "codec not found\n"); + fprintf(stderr, "codec not found\n"); fflush(stderr); exit(1); } - dcc= avcodec_alloc_context(); ALLOC(dcc, "mov_encoder, dcc"); + dcc= avcodec_alloc_context(); - /* open it */ + // open it if (avcodec_open(dcc, deccodec) < 0) { - fprintf(stderr, "could not open codec\n"); + fprintf(stderr, "could not open codec\n"); fflush(stderr); exit(1); } } @@ -166,32 +166,41 @@ MovEncoder::~MovEncoder() } void MovEncoder::encode(Frame *dvframe) -{ +{ + encode_video(dvframe); + // encode_audio(dvframe); +} + + +void MovEncoder::encode_video(Frame *dvframe) +{ + int16_t audiobuffer[48000]; int ret; - AVFrame *rawframe = avcodec_alloc_frame();ALLOC(dcc, "mov_encoder, rawframe"); + AVFrame *rawframe = avcodec_alloc_frame(); - ///////////////////////// DECODE uint8_t *ptr; int got_picture = 1; + int got_sound = 1; int len; ptr = (uint8_t *)dvframe->data; len = dvframe->size; + ///////////////////////// DECODE VIDEO + ret = avcodec_decode_video(dcc,//&dfc->streams[0]->codec, rawframe, &got_picture, ptr, len); - if(!ret) { - printf("Decoder fuckup!\n"); + fprintf(stderr, "Decoder fuckup during video decoding!\n"); fflush(stderr); return; } - - ///////////////////////// ENCODE + + ///////////////////////// ENCODE VIDEO ret = avcodec_encode_video(ecc, video_buffer, VIDEO_BUFFER_SIZE, rawframe); if(!ret) { - printf("MovEncoder fuckup!\n"); + fprintf(stderr, "MovEncoder fuckup during video encoding!\n"); fflush(stderr); return; } @@ -206,3 +215,120 @@ void MovEncoder::encode(Frame *dvframe) av_free(rawframe); FREE(rawframe); } + +#define INBUF_SIZE 48000 +void MovEncoder::encode_audio(Frame *dvframe) +{ + uint8_t *decbuf; + + ///////////////////////// DECODE AUDIO + { + AVCodec *codec; + AVCodecContext *c= NULL; + int out_size, size, len; + uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr; + + printf("Audio decoding\n"); + + /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */ + memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE); + + /* find the mpeg audio decoder */ + codec = avcodec_find_decoder(CODEC_ID_DVAUDIO); + if (!codec) { + fprintf(stderr, "codec not found\n"); + exit(1); + } + + c= avcodec_alloc_context(); + + /* open it */ + if (avcodec_open(c, codec) < 0) { + fprintf(stderr, "could not open codec\n"); + exit(1); + } + + decbuf = (uint8_t*)malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE); + + + /* decode until eof */ + size = dvframe->size; + inbuf_ptr = dvframe->data; + + while (size > 0) { + len = avcodec_decode_audio(c, (short *)decbuf, &out_size, + inbuf_ptr, size); + if (len < 0) { + fprintf(stderr, "Error while decoding\n"); + exit(1); + } + if (out_size > 0) { + /* if a frame has been decoded, output it */ + } + size -= len; + inbuf_ptr += len; + } + + avcodec_close(c); + av_free(c); + } + ///////////////////////// ENCODE AUDIO + { + char filename[]="audio.mp2"; + AVCodec *codec; + AVCodecContext *c= NULL; + int frame_size, i, j, out_size, outbuf_size; + FILE *f; + short *samples; + float t, tincr; + uint8_t *outbuf; + + printf("Audio encoding\n"); + + /* find the MP2 encoder */ + codec = avcodec_find_encoder(CODEC_ID_MP2); + if (!codec) { + fprintf(stderr, "codec not found\n"); + exit(1); + } + + c= avcodec_alloc_context(); + + /* put sample parameters */ + c->bit_rate = 64000; + c->sample_rate = 44100; + c->channels = 2; + + /* open it */ + if (avcodec_open(c, codec) < 0) { + fprintf(stderr, "could not open codec\n"); + exit(1); + } + + /* the codec gives us the frame size, in samples */ + frame_size = c->frame_size; + samples = (short int*)malloc(frame_size * 2 * c->channels); + outbuf_size = 10000; + outbuf = (uint8_t*)malloc(outbuf_size); + + f = fopen(filename, "a"); + if (!f) { + fprintf(stderr, "could not open %s\n", filename); + exit(1); + } + + // encode the sample + out_size = avcodec_encode_audio(c, outbuf, outbuf_size, (const short int*)decbuf); + fwrite(outbuf, 1, out_size, f); + + fclose(f); + free(outbuf); + free(samples); + + avcodec_close(c); + av_free(c); + } + + // Don't free outputbuffer until this point! + free(decbuf); +} diff --git a/src/mov_encoder.h b/src/mov_encoder.h index ecdb95e..45d7676 100644 --- a/src/mov_encoder.h +++ b/src/mov_encoder.h @@ -41,6 +41,9 @@ class MovEncoder { void encode(Frame *frame); private: + void encode_video(Frame *frame); + void encode_audio(Frame *frame); + // Decoder AVFormatContext *dfc; AVCodecContext *dcc; -- cgit v1.2.3