libextractor

GNU libextractor
Log | Files | Refs | Submodules | README | LICENSE

commit 69c6e75967b5f27751b0442f297f342ebc222315
parent fff067577af897c6e4f4b7886aec56c4870ced56
Author: Christian Grothoff <christian@grothoff.org>
Date:   Thu, 20 Dec 2018 22:47:36 +0100

fix indentation and memory leaks

Diffstat:
Msrc/plugins/ole2_extractor.c | 2+-
Msrc/plugins/previewopus_extractor.c | 317++++++++++++++++++++++++++++++++++++++++++-------------------------------------
Msrc/plugins/thumbnailffmpeg_extractor.c | 39+++++++++++++++++++++------------------
3 files changed, 190 insertions(+), 168 deletions(-)

diff --git a/src/plugins/ole2_extractor.c b/src/plugins/ole2_extractor.c @@ -1,6 +1,6 @@ /* This file is part of libextractor. - Copyright (C) 2004, 2005, 2006, 2007, 2009, 2012 Vidyut Samanta and Christian Grothoff + Copyright (C) 2004, 2005, 2006, 2007, 2009, 2012, 2018 Vidyut Samanta and Christian Grothoff libextractor is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published diff --git a/src/plugins/previewopus_extractor.c b/src/plugins/previewopus_extractor.c @@ -178,16 +178,19 @@ seek_cb (void *opaque, * @param pBufferSize , amount to write * @return 0 on error */ -static int writePacket(void *opaque, unsigned char *pBuffer, int pBufferSize) { - - int sizeToCopy = pBufferSize; - if( (totalSize + pBufferSize) > HARD_LIMIT_SIZE) - sizeToCopy = HARD_LIMIT_SIZE - totalSize; +static int +writePacket (void *opaque, + unsigned char *pBuffer, + int pBufferSize) +{ + int sizeToCopy = pBufferSize; - memcpy(buffer + totalSize, pBuffer, sizeToCopy); - totalSize+= sizeToCopy; + if( (totalSize + pBufferSize) > HARD_LIMIT_SIZE) + sizeToCopy = HARD_LIMIT_SIZE - totalSize; - return sizeToCopy; + memcpy (buffer + totalSize, pBuffer, sizeToCopy); + totalSize += sizeToCopy; + return sizeToCopy; } @@ -201,19 +204,17 @@ static int open_output_file( AVFormatContext **output_format_context, AVCodecContext **output_codec_context) { - AVStream *stream = NULL; - AVCodec *output_codec = NULL; - AVIOContext *io_ctx; - int error; - - - unsigned char *iob; + AVStream *stream = NULL; + AVCodec *output_codec = NULL; + AVIOContext *io_ctx; + int error; + unsigned char *iob; if (NULL == (iob = av_malloc (16 * 1024))) return AVERROR_EXIT; if (NULL == (io_ctx = avio_alloc_context (iob, 16 * 1024, AVIO_FLAG_WRITE, NULL, - NULL, + NULL, &writePacket /* no writing */, NULL))) { @@ -228,96 +229,105 @@ static int open_output_file( (*output_format_context)->pb = io_ctx; /** Guess the desired container format based on the file extension. */ - if (!((*output_format_context)->oformat = av_guess_format(NULL, "file.ogg", - NULL))) { + if (!((*output_format_context)->oformat = av_guess_format (NULL, + "file.ogg", + NULL))) + { #if DEBUG - fprintf(stderr, "Could not find output file format\n"); + fprintf(stderr, "Could not find output file format\n"); #endif - goto cleanup; + goto cleanup; } - - /** Find the encoder to be used by its name. */ - if (!(output_codec = avcodec_find_encoder(AV_CODEC_ID_OPUS))) { + /** Find the encoder to be used by its name. */ + if (!(output_codec = avcodec_find_encoder(AV_CODEC_ID_OPUS))) + { #if DEBUG - fprintf(stderr, "Could not find an OPUS encoder.\n"); + fprintf(stderr, "Could not find an OPUS encoder.\n"); #endif - goto cleanup; + goto cleanup; } - /** Create a new audio stream in the output file container. */ - if (!(stream = avformat_new_stream(*output_format_context, output_codec))) { + /** Create a new audio stream in the output file container. */ + if (!(stream = avformat_new_stream(*output_format_context, output_codec))) + { #if DEBUG - fprintf(stderr, "Could not create new stream\n"); + fprintf(stderr, "Could not create new stream\n"); #endif - error = AVERROR(ENOMEM); - goto cleanup; + error = AVERROR(ENOMEM); + goto cleanup; } - /** Save the encoder context for easiert access later. */ - *output_codec_context = stream->codec; + /** Save the encoder context for easiert access later. */ + *output_codec_context = stream->codec; - - /** - * Set the basic encoder parameters. - * The input file's sample rate is used to avoid a sample rate conversion. - */ - (*output_codec_context)->channels = OUTPUT_CHANNELS; - (*output_codec_context)->channel_layout = av_get_default_channel_layout(OUTPUT_CHANNELS); - (*output_codec_context)->sample_rate = 48000; //Opus need 48000 - (*output_codec_context)->sample_fmt = AV_SAMPLE_FMT_S16; - (*output_codec_context)->bit_rate = OUTPUT_BIT_RATE; - - - /** Open the encoder for the audio stream to use it later. */ - if ((error = avcodec_open2(*output_codec_context, output_codec, NULL)) < 0) { + /** + * Set the basic encoder parameters. + * The input file's sample rate is used to avoid a sample rate conversion. + */ + (*output_codec_context)->channels = OUTPUT_CHANNELS; + (*output_codec_context)->channel_layout = av_get_default_channel_layout(OUTPUT_CHANNELS); + (*output_codec_context)->sample_rate = 48000; //Opus need 48000 + (*output_codec_context)->sample_fmt = AV_SAMPLE_FMT_S16; + (*output_codec_context)->bit_rate = OUTPUT_BIT_RATE; + + /** Open the encoder for the audio stream to use it later. */ + if ((error = avcodec_open2(*output_codec_context, output_codec, NULL)) < 0) + { #if DEBUG - fprintf(stderr, "Could not open output codec (error '%s')\n", - get_error_text(error)); + fprintf(stderr, "Could not open output codec (error '%s')\n", + get_error_text(error)); #endif - goto cleanup; + goto cleanup; } - - return 0; + return 0; cleanup: - return error < 0 ? error : AVERROR_EXIT; + av_free (io_ctx); + return error < 0 ? error : AVERROR_EXIT; } + /** Initialize one data packet for reading or writing. */ -static void init_packet(AVPacket *packet) +static void +init_packet(AVPacket *packet) { - av_init_packet(packet); - /** Set the packet data and size so that it is recognized as being empty. */ - packet->data = NULL; - packet->size = 0; + av_init_packet(packet); + /** Set the packet data and size so that it is recognized as being empty. */ + packet->data = NULL; + packet->size = 0; } + /** Initialize one audio frame for reading from the input file */ -static int init_input_frame(AVFrame **frame) +static int +init_input_frame(AVFrame **frame) { #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55,28,1) - *frame = av_frame_alloc (); + *frame = av_frame_alloc (); #else - *frame = avcodec_alloc_frame(); + *frame = avcodec_alloc_frame(); #endif - if (NULL == *frame) { + if (NULL == *frame) + { #if DEBUG - fprintf(stderr, "Could not allocate input frame\n"); + fprintf(stderr, "Could not allocate input frame\n"); #endif - return AVERROR(ENOMEM); + return AVERROR(ENOMEM); } - return 0; + return 0; } + /** * Initialize the audio resampler based on the input and output codec settings. * If the input and output sample formats differ, a conversion is required * libavresample takes care of this, but requires initialization. */ -static int init_resampler(AVCodecContext *input_codec_context, - AVCodecContext *output_codec_context, - AVAudioResampleContext **resample_context) +static int +init_resampler (AVCodecContext *input_codec_context, + AVCodecContext *output_codec_context, + AVAudioResampleContext **resample_context) { /** * Only initialize the resampler if it is necessary, i.e., @@ -872,7 +882,8 @@ extract_audio (struct EXTRACTOR_ExtractContext *ec) totalSize =0; if (NULL == (iob = av_malloc (16 * 1024))) return; - if (NULL == (io_ctx = avio_alloc_context (iob, 16 * 1024, + if (NULL == (io_ctx = avio_alloc_context (iob, + 16 * 1024, 0, ec, &read_cb, NULL /* no writing */, @@ -889,7 +900,10 @@ extract_audio (struct EXTRACTOR_ExtractContext *ec) format_ctx->pb = io_ctx; options = NULL; if (0 != avformat_open_input (&format_ctx, "<no file>", NULL, &options)) - return; + { + av_free (io_ctx); + return; + } av_dict_free (&options); if (0 > avformat_find_stream_info (format_ctx, NULL)) { @@ -958,11 +972,14 @@ extract_audio (struct EXTRACTOR_ExtractContext *ec) /** Open the output file for writing. */ - if (open_output_file( codec_ctx,&output_format_context, &output_codec_context)) + if (open_output_file (codec_ctx, + &output_format_context, + &output_codec_context)) goto cleanup; /** Initialize the resampler to be able to convert audio sample formats. */ - if (init_resampler(codec_ctx, output_codec_context, - &resample_context)) + if (init_resampler (codec_ctx, + output_codec_context, + &resample_context)) goto cleanup; /** Initialize the FIFO buffer to store audio samples to be encoded. */ if (init_fifo(&fifo)) @@ -991,8 +1008,6 @@ extract_audio (struct EXTRACTOR_ExtractContext *ec) #endif } - - /* if duration is known, seek to first tried, * else use 10 sec into stream */ @@ -1010,83 +1025,88 @@ extract_audio (struct EXTRACTOR_ExtractContext *ec) * Loop as long as we have input samples to read or output samples * to write; abort as soon as we have neither. */ - while (1) { - /** Use the encoder's desired frame size for processing. */ - const int output_frame_size = output_codec_context->frame_size; - int finished = 0; - - /** - * Make sure that there is one frame worth of samples in the FIFO - * buffer so that the encoder can do its work. - * Since the decoder's and the encoder's frame size may differ, we - * need to FIFO buffer to store as many frames worth of input samples - * that they make up at least one frame worth of output samples. - */ + while (1) + { + /** Use the encoder's desired frame size for processing. */ + const int output_frame_size = output_codec_context->frame_size; + int finished = 0; - while ((av_audio_fifo_size(fifo) < output_frame_size)) { /** - * Decode one frame worth of audio samples, convert it to the - * output sample format and put it into the FIFO buffer. + * Make sure that there is one frame worth of samples in the FIFO + * buffer so that the encoder can do its work. + * Since the decoder's and the encoder's frame size may differ, we + * need to FIFO buffer to store as many frames worth of input samples + * that they make up at least one frame worth of output samples. */ + while ((av_audio_fifo_size(fifo) < output_frame_size)) + { + /** + * Decode one frame worth of audio samples, convert it to the + * output sample format and put it into the FIFO buffer. + */ + if (read_decode_convert_and_store (fifo, + format_ctx, + codec_ctx, + output_codec_context, + resample_context, + audio_stream_index, + &finished)) + { + goto cleanup; + } + + /** + * If we are at the end of the input file, we continue + * encoding the remaining audio samples to the output file. + */ + if (finished) + break; + } - if (read_decode_convert_and_store(fifo, format_ctx,codec_ctx, - output_codec_context, - resample_context,audio_stream_index, &finished)){ - - goto cleanup; - - } + /* Already over our limit*/ + if (totalSize >= MAX_SIZE) + finished = 1; /** - * If we are at the end of the input file, we continue - * encoding the remaining audio samples to the output file. + * If we have enough samples for the encoder, we encode them. + * At the end of the file, we pass the remaining samples to + * the encoder. */ - if (finished) - break; - } - - /* Already over our limit*/ - if(totalSize >= MAX_SIZE) - finished = 1; - - /** - * If we have enough samples for the encoder, we encode them. - * At the end of the file, we pass the remaining samples to - * the encoder. - */ - - while (av_audio_fifo_size(fifo) >= output_frame_size || - (finished && av_audio_fifo_size(fifo) > 0)){ + while (av_audio_fifo_size(fifo) >= output_frame_size || + (finished && av_audio_fifo_size(fifo) > 0)) + { + /** + * Take one frame worth of audio samples from the FIFO buffer, + * encode it and write it to the output file. + */ + if (load_encode_and_write (fifo, + output_format_context, + output_codec_context)) + goto cleanup; + } /** - * Take one frame worth of audio samples from the FIFO buffer, - * encode it and write it to the output file. + * If we are at the end of the input file and have encoded + * all remaining samples, we can exit this loop and finish. */ - - - if (load_encode_and_write(fifo,output_format_context, output_codec_context)) - goto cleanup; - } - /** - * If we are at the end of the input file and have encoded - * all remaining samples, we can exit this loop and finish. - */ - if (finished) { - int data_written; - /** Flush the encoder as it may have delayed frames. */ - do { - encode_audio_frame(NULL, output_format_context, output_codec_context, &data_written); - } while (data_written); - break; + if (finished) + { + int data_written; + /** Flush the encoder as it may have delayed frames. */ + do { + encode_audio_frame (NULL, + output_format_context, + output_codec_context, + &data_written); + } while (data_written); + break; + } } - } /** Write the trailer of the output file container. */ if (write_output_file_trailer(output_format_context)) goto cleanup; - - ec->proc (ec->cls, "previewopus", EXTRACTOR_METATYPE_AUDIO_PREVIEW, @@ -1095,33 +1115,32 @@ extract_audio (struct EXTRACTOR_ExtractContext *ec) buffer, totalSize); - #if OUTPUT_FILE { FILE *f; - f = fopen("example.opus", "wb"); - if (!f) { - fprintf(stderr, "Could not open %s\n", "file"); - exit(1); - } - - fwrite(buffer, 1, totalSize, f); + f = fopen ("example.opus", "wb"); + if (!f) + { + fprintf (stderr, "Could not open %s\n", "file"); + exit(1); + } + fwrite (buffer, 1, totalSize, f); fclose(f); } #endif cleanup: av_free (frame); - - free(buffer); + free (buffer); if (fifo) av_audio_fifo_free(fifo); - if (resample_context) { - avresample_close(resample_context); - avresample_free(&resample_context); - } + if (resample_context) + { + avresample_close(resample_context); + avresample_free(&resample_context); + } if (output_codec_context) avcodec_close(output_codec_context); diff --git a/src/plugins/thumbnailffmpeg_extractor.c b/src/plugins/thumbnailffmpeg_extractor.c @@ -635,7 +635,8 @@ extract_video (struct EXTRACTOR_ExtractContext *ec) if (NULL == (iob = av_malloc (16 * 1024))) return; - if (NULL == (io_ctx = avio_alloc_context (iob, 16 * 1024, + if (NULL == (io_ctx = avio_alloc_context (iob, + 16 * 1024, 0, ec, &read_cb, NULL /* no writing */, @@ -652,7 +653,10 @@ extract_video (struct EXTRACTOR_ExtractContext *ec) format_ctx->pb = io_ctx; options = NULL; if (0 != avformat_open_input (&format_ctx, "<no file>", NULL, &options)) - return; + { + av_free (io_ctx); + return; + } av_dict_free (&options); if (0 > avformat_find_stream_info (format_ctx, NULL)) { @@ -789,7 +793,6 @@ extract_video (struct EXTRACTOR_ExtractContext *ec) &encoded_thumbnail, MAX_THUMB_BYTES); if (err > 0) { - ec->proc (ec->cls, "thumbnailffmpeg", EXTRACTOR_METATYPE_THUMBNAIL, @@ -797,22 +800,22 @@ extract_video (struct EXTRACTOR_ExtractContext *ec) "image/png", (const char*) encoded_thumbnail, err); - #if OUTPUT_FILE - FILE *f; - #ifdef USE_JPEG - f = fopen("thumb.jpg", "wb"); - #else - f = fopen("thumb.png", "wb"); - #endif - if (!f) { - fprintf(stderr, "Could not open %s\n", "file"); - exit(1); - } - - fwrite(encoded_thumbnail, 1, err, f); - fclose(f); +#if OUTPUT_FILE + FILE *f; +#ifdef USE_JPEG + f = fopen("thumb.jpg", "wb"); +#else + f = fopen("thumb.png", "wb"); +#endif + if (!f) + { + fprintf(stderr, "Could not open %s\n", "file"); + exit(1); + } - #endif + fwrite(encoded_thumbnail, 1, err, f); + fclose(f); +#endif av_free (encoded_thumbnail); } #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55,28,1)