aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2018-12-20 22:47:36 +0100
committerChristian Grothoff <christian@grothoff.org>2018-12-20 22:47:36 +0100
commit69c6e75967b5f27751b0442f297f342ebc222315 (patch)
tree10c299fe772fd0de40acaac9757eadf278003c35
parentfff067577af897c6e4f4b7886aec56c4870ced56 (diff)
downloadlibextractor-69c6e75967b5f27751b0442f297f342ebc222315.tar.gz
libextractor-69c6e75967b5f27751b0442f297f342ebc222315.zip
fix indentation and memory leaks
-rw-r--r--src/plugins/ole2_extractor.c2
-rw-r--r--src/plugins/previewopus_extractor.c317
-rw-r--r--src/plugins/thumbnailffmpeg_extractor.c39
3 files changed, 190 insertions, 168 deletions
diff --git a/src/plugins/ole2_extractor.c b/src/plugins/ole2_extractor.c
index 072ffc5..53fa1b9 100644
--- a/src/plugins/ole2_extractor.c
+++ b/src/plugins/ole2_extractor.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of libextractor. 2 This file is part of libextractor.
3 Copyright (C) 2004, 2005, 2006, 2007, 2009, 2012 Vidyut Samanta and Christian Grothoff 3 Copyright (C) 2004, 2005, 2006, 2007, 2009, 2012, 2018 Vidyut Samanta and Christian Grothoff
4 4
5 libextractor is free software; you can redistribute it and/or modify 5 libextractor is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published 6 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
index 84c72de..4e137ec 100644
--- a/src/plugins/previewopus_extractor.c
+++ b/src/plugins/previewopus_extractor.c
@@ -178,16 +178,19 @@ seek_cb (void *opaque,
178 * @param pBufferSize , amount to write 178 * @param pBufferSize , amount to write
179 * @return 0 on error 179 * @return 0 on error
180 */ 180 */
181static int writePacket(void *opaque, unsigned char *pBuffer, int pBufferSize) { 181static int
182 182writePacket (void *opaque,
183 int sizeToCopy = pBufferSize; 183 unsigned char *pBuffer,
184 if( (totalSize + pBufferSize) > HARD_LIMIT_SIZE) 184 int pBufferSize)
185 sizeToCopy = HARD_LIMIT_SIZE - totalSize; 185{
186 int sizeToCopy = pBufferSize;
186 187
187 memcpy(buffer + totalSize, pBuffer, sizeToCopy); 188 if( (totalSize + pBufferSize) > HARD_LIMIT_SIZE)
188 totalSize+= sizeToCopy; 189 sizeToCopy = HARD_LIMIT_SIZE - totalSize;
189 190
190 return sizeToCopy; 191 memcpy (buffer + totalSize, pBuffer, sizeToCopy);
192 totalSize += sizeToCopy;
193 return sizeToCopy;
191} 194}
192 195
193 196
@@ -201,19 +204,17 @@ static int open_output_file(
201 AVFormatContext **output_format_context, 204 AVFormatContext **output_format_context,
202 AVCodecContext **output_codec_context) 205 AVCodecContext **output_codec_context)
203{ 206{
204 AVStream *stream = NULL; 207 AVStream *stream = NULL;
205 AVCodec *output_codec = NULL; 208 AVCodec *output_codec = NULL;
206 AVIOContext *io_ctx; 209 AVIOContext *io_ctx;
207 int error; 210 int error;
208 211 unsigned char *iob;
209
210 unsigned char *iob;
211 212
212 if (NULL == (iob = av_malloc (16 * 1024))) 213 if (NULL == (iob = av_malloc (16 * 1024)))
213 return AVERROR_EXIT; 214 return AVERROR_EXIT;
214 if (NULL == (io_ctx = avio_alloc_context (iob, 16 * 1024, 215 if (NULL == (io_ctx = avio_alloc_context (iob, 16 * 1024,
215 AVIO_FLAG_WRITE, NULL, 216 AVIO_FLAG_WRITE, NULL,
216 NULL, 217 NULL,
217 &writePacket /* no writing */, 218 &writePacket /* no writing */,
218 NULL))) 219 NULL)))
219 { 220 {
@@ -228,96 +229,105 @@ static int open_output_file(
228 (*output_format_context)->pb = io_ctx; 229 (*output_format_context)->pb = io_ctx;
229 230
230 /** Guess the desired container format based on the file extension. */ 231 /** Guess the desired container format based on the file extension. */
231 if (!((*output_format_context)->oformat = av_guess_format(NULL, "file.ogg", 232 if (!((*output_format_context)->oformat = av_guess_format (NULL,
232 NULL))) { 233 "file.ogg",
234 NULL)))
235 {
233#if DEBUG 236#if DEBUG
234 fprintf(stderr, "Could not find output file format\n"); 237 fprintf(stderr, "Could not find output file format\n");
235#endif 238#endif
236 goto cleanup; 239 goto cleanup;
237 } 240 }
238 241
239 242 /** Find the encoder to be used by its name. */
240 /** Find the encoder to be used by its name. */ 243 if (!(output_codec = avcodec_find_encoder(AV_CODEC_ID_OPUS)))
241 if (!(output_codec = avcodec_find_encoder(AV_CODEC_ID_OPUS))) { 244 {
242#if DEBUG 245#if DEBUG
243 fprintf(stderr, "Could not find an OPUS encoder.\n"); 246 fprintf(stderr, "Could not find an OPUS encoder.\n");
244#endif 247#endif
245 goto cleanup; 248 goto cleanup;
246 } 249 }
247 250
248 /** Create a new audio stream in the output file container. */ 251 /** Create a new audio stream in the output file container. */
249 if (!(stream = avformat_new_stream(*output_format_context, output_codec))) { 252 if (!(stream = avformat_new_stream(*output_format_context, output_codec)))
253 {
250#if DEBUG 254#if DEBUG
251 fprintf(stderr, "Could not create new stream\n"); 255 fprintf(stderr, "Could not create new stream\n");
252#endif 256#endif
253 error = AVERROR(ENOMEM); 257 error = AVERROR(ENOMEM);
254 goto cleanup; 258 goto cleanup;
255 } 259 }
256 260
257 /** Save the encoder context for easiert access later. */ 261 /** Save the encoder context for easiert access later. */
258 *output_codec_context = stream->codec; 262 *output_codec_context = stream->codec;
259 263
260 264 /**
261 /** 265 * Set the basic encoder parameters.
262 * Set the basic encoder parameters. 266 * The input file's sample rate is used to avoid a sample rate conversion.
263 * The input file's sample rate is used to avoid a sample rate conversion. 267 */
264 */ 268 (*output_codec_context)->channels = OUTPUT_CHANNELS;
265 (*output_codec_context)->channels = OUTPUT_CHANNELS; 269 (*output_codec_context)->channel_layout = av_get_default_channel_layout(OUTPUT_CHANNELS);
266 (*output_codec_context)->channel_layout = av_get_default_channel_layout(OUTPUT_CHANNELS); 270 (*output_codec_context)->sample_rate = 48000; //Opus need 48000
267 (*output_codec_context)->sample_rate = 48000; //Opus need 48000 271 (*output_codec_context)->sample_fmt = AV_SAMPLE_FMT_S16;
268 (*output_codec_context)->sample_fmt = AV_SAMPLE_FMT_S16; 272 (*output_codec_context)->bit_rate = OUTPUT_BIT_RATE;
269 (*output_codec_context)->bit_rate = OUTPUT_BIT_RATE; 273
270 274 /** Open the encoder for the audio stream to use it later. */
271 275 if ((error = avcodec_open2(*output_codec_context, output_codec, NULL)) < 0)
272 /** Open the encoder for the audio stream to use it later. */ 276 {
273 if ((error = avcodec_open2(*output_codec_context, output_codec, NULL)) < 0) {
274#if DEBUG 277#if DEBUG
275 fprintf(stderr, "Could not open output codec (error '%s')\n", 278 fprintf(stderr, "Could not open output codec (error '%s')\n",
276 get_error_text(error)); 279 get_error_text(error));
277#endif 280#endif
278 goto cleanup; 281 goto cleanup;
279 } 282 }
280 283 return 0;
281 return 0;
282 284
283cleanup: 285cleanup:
284 return error < 0 ? error : AVERROR_EXIT; 286 av_free (io_ctx);
287 return error < 0 ? error : AVERROR_EXIT;
285} 288}
286 289
290
287/** Initialize one data packet for reading or writing. */ 291/** Initialize one data packet for reading or writing. */
288static void init_packet(AVPacket *packet) 292static void
293init_packet(AVPacket *packet)
289{ 294{
290 av_init_packet(packet); 295 av_init_packet(packet);
291 /** Set the packet data and size so that it is recognized as being empty. */ 296 /** Set the packet data and size so that it is recognized as being empty. */
292 packet->data = NULL; 297 packet->data = NULL;
293 packet->size = 0; 298 packet->size = 0;
294} 299}
295 300
301
296/** Initialize one audio frame for reading from the input file */ 302/** Initialize one audio frame for reading from the input file */
297static int init_input_frame(AVFrame **frame) 303static int
304init_input_frame(AVFrame **frame)
298{ 305{
299#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55,28,1) 306#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55,28,1)
300 *frame = av_frame_alloc (); 307 *frame = av_frame_alloc ();
301#else 308#else
302 *frame = avcodec_alloc_frame(); 309 *frame = avcodec_alloc_frame();
303#endif 310#endif
304 if (NULL == *frame) { 311 if (NULL == *frame)
312 {
305#if DEBUG 313#if DEBUG
306 fprintf(stderr, "Could not allocate input frame\n"); 314 fprintf(stderr, "Could not allocate input frame\n");
307#endif 315#endif
308 return AVERROR(ENOMEM); 316 return AVERROR(ENOMEM);
309 } 317 }
310 return 0; 318 return 0;
311} 319}
312 320
321
313/** 322/**
314 * Initialize the audio resampler based on the input and output codec settings. 323 * Initialize the audio resampler based on the input and output codec settings.
315 * If the input and output sample formats differ, a conversion is required 324 * If the input and output sample formats differ, a conversion is required
316 * libavresample takes care of this, but requires initialization. 325 * libavresample takes care of this, but requires initialization.
317 */ 326 */
318static int init_resampler(AVCodecContext *input_codec_context, 327static int
319 AVCodecContext *output_codec_context, 328init_resampler (AVCodecContext *input_codec_context,
320 AVAudioResampleContext **resample_context) 329 AVCodecContext *output_codec_context,
330 AVAudioResampleContext **resample_context)
321{ 331{
322 /** 332 /**
323 * Only initialize the resampler if it is necessary, i.e., 333 * Only initialize the resampler if it is necessary, i.e.,
@@ -872,7 +882,8 @@ extract_audio (struct EXTRACTOR_ExtractContext *ec)
872 totalSize =0; 882 totalSize =0;
873 if (NULL == (iob = av_malloc (16 * 1024))) 883 if (NULL == (iob = av_malloc (16 * 1024)))
874 return; 884 return;
875 if (NULL == (io_ctx = avio_alloc_context (iob, 16 * 1024, 885 if (NULL == (io_ctx = avio_alloc_context (iob,
886 16 * 1024,
876 0, ec, 887 0, ec,
877 &read_cb, 888 &read_cb,
878 NULL /* no writing */, 889 NULL /* no writing */,
@@ -889,7 +900,10 @@ extract_audio (struct EXTRACTOR_ExtractContext *ec)
889 format_ctx->pb = io_ctx; 900 format_ctx->pb = io_ctx;
890 options = NULL; 901 options = NULL;
891 if (0 != avformat_open_input (&format_ctx, "<no file>", NULL, &options)) 902 if (0 != avformat_open_input (&format_ctx, "<no file>", NULL, &options))
892 return; 903 {
904 av_free (io_ctx);
905 return;
906 }
893 av_dict_free (&options); 907 av_dict_free (&options);
894 if (0 > avformat_find_stream_info (format_ctx, NULL)) 908 if (0 > avformat_find_stream_info (format_ctx, NULL))
895 { 909 {
@@ -958,11 +972,14 @@ extract_audio (struct EXTRACTOR_ExtractContext *ec)
958 972
959 973
960 /** Open the output file for writing. */ 974 /** Open the output file for writing. */
961 if (open_output_file( codec_ctx,&output_format_context, &output_codec_context)) 975 if (open_output_file (codec_ctx,
976 &output_format_context,
977 &output_codec_context))
962 goto cleanup; 978 goto cleanup;
963 /** Initialize the resampler to be able to convert audio sample formats. */ 979 /** Initialize the resampler to be able to convert audio sample formats. */
964 if (init_resampler(codec_ctx, output_codec_context, 980 if (init_resampler (codec_ctx,
965 &resample_context)) 981 output_codec_context,
982 &resample_context))
966 goto cleanup; 983 goto cleanup;
967 /** Initialize the FIFO buffer to store audio samples to be encoded. */ 984 /** Initialize the FIFO buffer to store audio samples to be encoded. */
968 if (init_fifo(&fifo)) 985 if (init_fifo(&fifo))
@@ -991,8 +1008,6 @@ extract_audio (struct EXTRACTOR_ExtractContext *ec)
991#endif 1008#endif
992 } 1009 }
993 1010
994
995
996 /* if duration is known, seek to first tried, 1011 /* if duration is known, seek to first tried,
997 * else use 10 sec into stream */ 1012 * else use 10 sec into stream */
998 1013
@@ -1010,83 +1025,88 @@ extract_audio (struct EXTRACTOR_ExtractContext *ec)
1010 * Loop as long as we have input samples to read or output samples 1025 * Loop as long as we have input samples to read or output samples
1011 * to write; abort as soon as we have neither. 1026 * to write; abort as soon as we have neither.
1012 */ 1027 */
1013 while (1) { 1028 while (1)
1014 /** Use the encoder's desired frame size for processing. */ 1029 {
1015 const int output_frame_size = output_codec_context->frame_size; 1030 /** Use the encoder's desired frame size for processing. */
1016 int finished = 0; 1031 const int output_frame_size = output_codec_context->frame_size;
1017 1032 int finished = 0;
1018 /**
1019 * Make sure that there is one frame worth of samples in the FIFO
1020 * buffer so that the encoder can do its work.
1021 * Since the decoder's and the encoder's frame size may differ, we
1022 * need to FIFO buffer to store as many frames worth of input samples
1023 * that they make up at least one frame worth of output samples.
1024 */
1025 1033
1026 while ((av_audio_fifo_size(fifo) < output_frame_size)) {
1027 /** 1034 /**
1028 * Decode one frame worth of audio samples, convert it to the 1035 * Make sure that there is one frame worth of samples in the FIFO
1029 * output sample format and put it into the FIFO buffer. 1036 * buffer so that the encoder can do its work.
1037 * Since the decoder's and the encoder's frame size may differ, we
1038 * need to FIFO buffer to store as many frames worth of input samples
1039 * that they make up at least one frame worth of output samples.
1030 */ 1040 */
1031 1041
1042 while ((av_audio_fifo_size(fifo) < output_frame_size))
1043 {
1044 /**
1045 * Decode one frame worth of audio samples, convert it to the
1046 * output sample format and put it into the FIFO buffer.
1047 */
1048 if (read_decode_convert_and_store (fifo,
1049 format_ctx,
1050 codec_ctx,
1051 output_codec_context,
1052 resample_context,
1053 audio_stream_index,
1054 &finished))
1055 {
1056 goto cleanup;
1057 }
1058
1059 /**
1060 * If we are at the end of the input file, we continue
1061 * encoding the remaining audio samples to the output file.
1062 */
1063 if (finished)
1064 break;
1065 }
1032 1066
1033 if (read_decode_convert_and_store(fifo, format_ctx,codec_ctx, 1067 /* Already over our limit*/
1034 output_codec_context, 1068 if (totalSize >= MAX_SIZE)
1035 resample_context,audio_stream_index, &finished)){ 1069 finished = 1;
1036
1037 goto cleanup;
1038
1039 }
1040 1070
1041 /** 1071 /**
1042 * If we are at the end of the input file, we continue 1072 * If we have enough samples for the encoder, we encode them.
1043 * encoding the remaining audio samples to the output file. 1073 * At the end of the file, we pass the remaining samples to
1074 * the encoder.
1044 */ 1075 */
1045 if (finished)
1046 break;
1047 }
1048
1049 /* Already over our limit*/
1050 if(totalSize >= MAX_SIZE)
1051 finished = 1;
1052
1053 1076
1054 /** 1077 while (av_audio_fifo_size(fifo) >= output_frame_size ||
1055 * If we have enough samples for the encoder, we encode them. 1078 (finished && av_audio_fifo_size(fifo) > 0))
1056 * At the end of the file, we pass the remaining samples to 1079 {
1057 * the encoder. 1080 /**
1058 */ 1081 * Take one frame worth of audio samples from the FIFO buffer,
1059 1082 * encode it and write it to the output file.
1060 while (av_audio_fifo_size(fifo) >= output_frame_size || 1083 */
1061 (finished && av_audio_fifo_size(fifo) > 0)){ 1084 if (load_encode_and_write (fifo,
1085 output_format_context,
1086 output_codec_context))
1087 goto cleanup;
1088 }
1062 /** 1089 /**
1063 * Take one frame worth of audio samples from the FIFO buffer, 1090 * If we are at the end of the input file and have encoded
1064 * encode it and write it to the output file. 1091 * all remaining samples, we can exit this loop and finish.
1065 */ 1092 */
1066 1093 if (finished)
1067 1094 {
1068 if (load_encode_and_write(fifo,output_format_context, output_codec_context)) 1095 int data_written;
1069 goto cleanup; 1096 /** Flush the encoder as it may have delayed frames. */
1070 } 1097 do {
1071 /** 1098 encode_audio_frame (NULL,
1072 * If we are at the end of the input file and have encoded 1099 output_format_context,
1073 * all remaining samples, we can exit this loop and finish. 1100 output_codec_context,
1074 */ 1101 &data_written);
1075 if (finished) { 1102 } while (data_written);
1076 int data_written; 1103 break;
1077 /** Flush the encoder as it may have delayed frames. */ 1104 }
1078 do {
1079 encode_audio_frame(NULL, output_format_context, output_codec_context, &data_written);
1080 } while (data_written);
1081 break;
1082 } 1105 }
1083 }
1084 1106
1085 /** Write the trailer of the output file container. */ 1107 /** Write the trailer of the output file container. */
1086 if (write_output_file_trailer(output_format_context)) 1108 if (write_output_file_trailer(output_format_context))
1087 goto cleanup; 1109 goto cleanup;
1088
1089
1090 ec->proc (ec->cls, 1110 ec->proc (ec->cls,
1091 "previewopus", 1111 "previewopus",
1092 EXTRACTOR_METATYPE_AUDIO_PREVIEW, 1112 EXTRACTOR_METATYPE_AUDIO_PREVIEW,
@@ -1095,33 +1115,32 @@ extract_audio (struct EXTRACTOR_ExtractContext *ec)
1095 buffer, 1115 buffer,
1096 totalSize); 1116 totalSize);
1097 1117
1098
1099#if OUTPUT_FILE 1118#if OUTPUT_FILE
1100 { 1119 {
1101 FILE *f; 1120 FILE *f;
1102 1121
1103 f = fopen("example.opus", "wb"); 1122 f = fopen ("example.opus", "wb");
1104 if (!f) { 1123 if (!f)
1105 fprintf(stderr, "Could not open %s\n", "file"); 1124 {
1106 exit(1); 1125 fprintf (stderr, "Could not open %s\n", "file");
1107 } 1126 exit(1);
1108 1127 }
1109 fwrite(buffer, 1, totalSize, f); 1128 fwrite (buffer, 1, totalSize, f);
1110 fclose(f); 1129 fclose(f);
1111 } 1130 }
1112#endif 1131#endif
1113 1132
1114 cleanup: 1133 cleanup:
1115 av_free (frame); 1134 av_free (frame);
1116 1135 free (buffer);
1117 free(buffer);
1118 1136
1119 if (fifo) 1137 if (fifo)
1120 av_audio_fifo_free(fifo); 1138 av_audio_fifo_free(fifo);
1121 if (resample_context) { 1139 if (resample_context)
1122 avresample_close(resample_context); 1140 {
1123 avresample_free(&resample_context); 1141 avresample_close(resample_context);
1124 } 1142 avresample_free(&resample_context);
1143 }
1125 if (output_codec_context) 1144 if (output_codec_context)
1126 avcodec_close(output_codec_context); 1145 avcodec_close(output_codec_context);
1127 1146
diff --git a/src/plugins/thumbnailffmpeg_extractor.c b/src/plugins/thumbnailffmpeg_extractor.c
index 1b7347a..72af79f 100644
--- a/src/plugins/thumbnailffmpeg_extractor.c
+++ b/src/plugins/thumbnailffmpeg_extractor.c
@@ -635,7 +635,8 @@ extract_video (struct EXTRACTOR_ExtractContext *ec)
635 635
636 if (NULL == (iob = av_malloc (16 * 1024))) 636 if (NULL == (iob = av_malloc (16 * 1024)))
637 return; 637 return;
638 if (NULL == (io_ctx = avio_alloc_context (iob, 16 * 1024, 638 if (NULL == (io_ctx = avio_alloc_context (iob,
639 16 * 1024,
639 0, ec, 640 0, ec,
640 &read_cb, 641 &read_cb,
641 NULL /* no writing */, 642 NULL /* no writing */,
@@ -652,7 +653,10 @@ extract_video (struct EXTRACTOR_ExtractContext *ec)
652 format_ctx->pb = io_ctx; 653 format_ctx->pb = io_ctx;
653 options = NULL; 654 options = NULL;
654 if (0 != avformat_open_input (&format_ctx, "<no file>", NULL, &options)) 655 if (0 != avformat_open_input (&format_ctx, "<no file>", NULL, &options))
655 return; 656 {
657 av_free (io_ctx);
658 return;
659 }
656 av_dict_free (&options); 660 av_dict_free (&options);
657 if (0 > avformat_find_stream_info (format_ctx, NULL)) 661 if (0 > avformat_find_stream_info (format_ctx, NULL))
658 { 662 {
@@ -789,7 +793,6 @@ extract_video (struct EXTRACTOR_ExtractContext *ec)
789 &encoded_thumbnail, MAX_THUMB_BYTES); 793 &encoded_thumbnail, MAX_THUMB_BYTES);
790 if (err > 0) 794 if (err > 0)
791 { 795 {
792
793 ec->proc (ec->cls, 796 ec->proc (ec->cls,
794 "thumbnailffmpeg", 797 "thumbnailffmpeg",
795 EXTRACTOR_METATYPE_THUMBNAIL, 798 EXTRACTOR_METATYPE_THUMBNAIL,
@@ -797,22 +800,22 @@ extract_video (struct EXTRACTOR_ExtractContext *ec)
797 "image/png", 800 "image/png",
798 (const char*) encoded_thumbnail, 801 (const char*) encoded_thumbnail,
799 err); 802 err);
800 #if OUTPUT_FILE 803#if OUTPUT_FILE
801 FILE *f; 804 FILE *f;
802 #ifdef USE_JPEG 805#ifdef USE_JPEG
803 f = fopen("thumb.jpg", "wb"); 806 f = fopen("thumb.jpg", "wb");
804 #else 807#else
805 f = fopen("thumb.png", "wb"); 808 f = fopen("thumb.png", "wb");
806 #endif 809#endif
807 if (!f) { 810 if (!f)
808 fprintf(stderr, "Could not open %s\n", "file"); 811 {
809 exit(1); 812 fprintf(stderr, "Could not open %s\n", "file");
810 } 813 exit(1);
811 814 }
812 fwrite(encoded_thumbnail, 1, err, f);
813 fclose(f);
814 815
815 #endif 816 fwrite(encoded_thumbnail, 1, err, f);
817 fclose(f);
818#endif
816 av_free (encoded_thumbnail); 819 av_free (encoded_thumbnail);
817 } 820 }
818#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55,28,1) 821#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55,28,1)