util/demuxer: Fix the memory leak
[glsdk/omapdrmtest.git] / util / demux.c
index 3aee5f3c44f036479ce44d77b2cfd59a038e3f4c..eec057872ec583c68a2c0b24c9e9c4ed22f4492a 100644 (file)
  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include <libavformat/avformat.h>
-#include <libavcodec/avcodec.h>
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
 
+
+#include "demux.h"
 #include "util.h"
 
-struct demux {
-       AVFormatContext *afc;
-       AVStream *st;
-       AVCodecContext *cc;
-       AVBitStreamFilterContext *bsf;
-};
+char mpeg4head[45] = {0,0,0,0};
+int esds_length;
+unsigned char *esds_data;
+extern int first_in_buff;
+int get_esds_offset(const char *filename);
+
+
 
 static AVFormatContext *
 open_file(const char *filename)
@@ -85,7 +89,7 @@ static struct demux * open_stream(const char * filename, int *width, int *height
        AVBitStreamFilterContext *bsf = NULL;
        struct demux *demux;
 
-       if (cc->codec_id != CODEC_ID_H264) {
+       if ((cc->codec_id != CODEC_ID_H264) &&  (cc->codec_id != CODEC_ID_MPEG2VIDEO) && ( cc->codec_id !=  CODEC_ID_MPEG4)){
                ERROR("could not open '%s': unsupported codec %d", filename, cc->codec_id);
                return NULL;
        }
@@ -116,9 +120,59 @@ struct demux * demux_init(const char * filename, int *width, int *height)
 {
        av_register_all();
        avcodec_register_all();
+       if(get_esds_offset(filename))
+               return NULL;
        return open_stream(filename, width, height);
 }
 
+int get_esds_offset(const char *filename) {
+       FILE *inputStream;
+       int i=0;
+       unsigned char *buffer = NULL;
+       unsigned long fileLen;
+       int esds_index=0;
+       inputStream = fopen(filename,"rb");
+       if (!inputStream) {
+               printf("Unable to open %s\n",filename);
+               return -1;
+       }
+       // Get the length
+       fseek(inputStream, 0, SEEK_END);
+       fileLen=ftell(inputStream);
+       fseek(inputStream, 0, SEEK_SET);
+       buffer=malloc(fileLen);
+       if (!buffer) {
+               printf("Memory error!\n");
+               fclose(inputStream);
+               return -1;
+       }
+       // File to buffer
+       fread(buffer, fileLen, 1, inputStream);
+       fclose(inputStream);
+       printf("Buffer starts: %p\n", &buffer[0]);
+       printf("Buffer ends: %p\n", &buffer[fileLen]);
+       //Determines offset of known_string
+       for(i=0; i<fileLen; i++) {
+               if((buffer[i] == 0x65) && (buffer[++i] == 0x73))
+               if((buffer[++i] == 0x64) && (buffer[i+1] == 0x73)) {
+                       printf("data in buffer = %x  %x  %x %d \n",buffer[i],buffer[i+1],buffer[i+2],buffer[i+27]);
+                       esds_index = i+27;
+                       break;
+               }
+       }
+       esds_length= buffer[esds_index];
+       esds_data=malloc(esds_length);
+       for(i=1; i<=esds_length; i++) {
+               //printf(" index= %x \n",buffer[esds_index+i]);
+               esds_data[i-1]=buffer[esds_index+i];
+               printf(" index= %x \n",esds_data[i-1]);
+       }
+       free(buffer);
+       return 0;
+}
+
+
+
 int demux_read(struct demux *demux, char *input, int size)
 {
        AVPacket pk = {};
@@ -130,7 +184,6 @@ int demux_read(struct demux *demux, char *input, int size)
 
                        if (demux->bsf) {
                                int ret;
-                               MSG("%p %d bytes", pk.data, pk.size);
                                ret = av_bitstream_filter_filter(demux->bsf, demux->cc,
                                                NULL, &buf, &bufsize, pk.data, pk.size, 0);
                                if (ret < 0) {
@@ -145,7 +198,15 @@ int demux_read(struct demux *demux, char *input, int size)
                        if (bufsize > size)
                                bufsize = size;
 
-                       memcpy(input, buf, bufsize);
+                        if(first_in_buff == 1) {
+                               memcpy(input,esds_data,esds_length);
+                               memcpy(input+esds_length, buf, bufsize);
+                               first_in_buff =0;
+                               bufsize = bufsize+esds_length;
+                       }
+                       else {
+                               memcpy(input, buf, bufsize);
+                       }
 
                        if (demux->bsf)
                                av_free(buf);
@@ -160,10 +221,16 @@ int demux_read(struct demux *demux, char *input, int size)
        return 0;
 }
 
+int demux_rewind(struct demux *demux)
+{
+       return av_seek_frame(demux->afc, demux->st->index, 0, AVSEEK_FLAG_FRAME);
+}
+
 void demux_deinit(struct demux *demux)
 {
        av_close_input_file(demux->afc);
        if (demux->bsf)
                av_bitstream_filter_close(demux->bsf);
+       free(esds_data);
        free(demux);
 }