0

I try to use libmpeg2 library inside my programm. The mpeg2dec example provided with the library use a mpeg2 play with a mpeg file on the disk of the computer.

I would like to use data from the memory instead (an unsigned char array stored in the memory). Here is the detail of the function I would like to modify:

static void es_loop (unsigned char * data10r, unsigned int * size10r)

{
    buffer_size = 4096;

    uint8_t * buffer = (uint8_t *) malloc (buffer_size);
    uint8_t * end;

    if (buffer == NULL)
        exit (1);

    do {

        end = buffer + fread (buffer, 1, buffer_size, in_file);
        decode_mpeg2 (buffer, end);

    } while (end == buffer + buffer_size && !sigint);
    free (buffer);

}

where data10r is an unsigned char array containing the mpeg2 data in memory with the size of size10r.

in_file is a pointer to the mpeg2 file on the disk to be played. I would like to replace this file by the data10r unsigned char array in memory... buffer contains 4096 bytes of the video to be sent to the decoder

I have spent several hours to find a solution to change this function, without any succes for the moment.

Thanks by advance for your help,

Pierre

Santosh A
  • 5,173
  • 27
  • 37
PGibouin
  • 241
  • 1
  • 3
  • 14

1 Answers1

0

On linux with glibc, you can use fmemopen to get FILE* and then read from it with ordinary fread. Hence, you only need to add FILE *in_file = fmemopen(data10r, *size10r, "r"); at the beginning and fclose(in_file); at the end of function.

If you need to support other systems too, you can implement the same functionality with memcpy and manually calculating offset.

Do you even need to decode it by 4kb chunks if whole data is already in memory? Why not just run decoding on entire file? Judjing by your code, it is mere decode_mpeg2(data10r, data10r + *size10r); (that is the whole code of function).

keltar
  • 17,711
  • 2
  • 37
  • 42
  • Thanks very much for your help, `decode_mpeg2(data10r, data10r + *size10r);` worked fine and is much more simpler than the original code. I don't know why the creators of the libmpeg2 consider there is a need to read data by 4kb chunks. The whole file data sent to the mpeg decompressor is working fine. – PGibouin Jan 17 '15 at 10:56
  • If you're reading very big video from disk (say, 15 Gb), it may be just too big to load into memory. There could be many other reasons, like disk reading is slow and you need to start displaying video as soon as possible, before it fully decodes. – keltar Jan 17 '15 at 12:39