comparison libmpdemux/demux_mkv.cpp @ 11393:6bacd63a80a7

Proper usage of zlib for decompression.
author mosu
date Wed, 05 Nov 2003 15:04:50 +0000
parents 9165908f87d3
children 27e5c86c968a
comparison
equal deleted inserted replaced
11392:6736b7fd66a8 11393:6bacd63a80a7
236 // Stuff for VobSubs 236 // Stuff for VobSubs
237 mkv_sh_sub_t sh_sub; 237 mkv_sh_sub_t sh_sub;
238 238
239 // Generic content encoding support. 239 // Generic content encoding support.
240 vector<mkv_content_encoding_t> *c_encodings; 240 vector<mkv_content_encoding_t> *c_encodings;
241 #ifdef HAVE_ZLIB
242 z_stream zstream;
243 bool zstream_initiated;
244 #endif
245 } mkv_track_t; 241 } mkv_track_t;
246 242
247 typedef struct mkv_demuxer { 243 typedef struct mkv_demuxer {
248 float duration, last_pts; 244 float duration, last_pts;
249 uint64_t last_filepos; 245 uint64_t last_filepos;
717 return (things_found == 3); 713 return (things_found == 3);
718 } 714 }
719 715
720 static bool reverse_encodings(mkv_track_t *track, unsigned char *&data, 716 static bool reverse_encodings(mkv_track_t *track, unsigned char *&data,
721 uint32_t &size, uint32_t type) { 717 uint32_t &size, uint32_t type) {
722 int new_size; 718 int new_size, n;
723 unsigned char *new_data, *old_data; 719 unsigned char *new_data, *old_data;
724 bool modified; 720 bool modified;
725 vector<mkv_content_encoding_t>::iterator ce; 721 vector<mkv_content_encoding_t>::iterator ce;
726 722
727 if (track->c_encodings->size() == 0) 723 if (track->c_encodings->size() == 0)
736 continue; 732 continue;
737 733
738 #ifdef HAVE_ZLIB 734 #ifdef HAVE_ZLIB
739 if (ce->comp_algo == 0) { 735 if (ce->comp_algo == 0) {
740 int result; 736 int result;
737 z_stream zstream;
741 738
742 old_data = new_data; 739 old_data = new_data;
743 new_data = (unsigned char *)safemalloc(new_size * 20); 740
744 741 zstream.zalloc = (alloc_func)0;
745 track->zstream.zalloc = (alloc_func)0; 742 zstream.zfree = (free_func)0;
746 track->zstream.zfree = (free_func)0; 743 zstream.opaque = (voidpf)0;
747 track->zstream.opaque = (voidpf)0; 744 result = inflateInit(&zstream);
748 inflateInit(&track->zstream);
749 track->zstream.next_in = (Bytef *)old_data;
750 track->zstream.next_out = (Bytef *)new_data;
751 track->zstream.avail_in = new_size;
752 track->zstream.avail_out = 20 * new_size;
753 result = inflate(&track->zstream, Z_FULL_FLUSH);
754 if (result != Z_OK) { 745 if (result != Z_OK) {
755 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] Zlib decompression failed. " 746 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] Zlib initialization failed. "
756 "Result: %d\n", result); 747 "Result: %d\n", result);
757 safefree(new_data); 748 safefree(new_data);
758 data = old_data; 749 data = old_data;
759 size = new_size; 750 size = new_size;
760 inflateEnd(&track->zstream);
761 return modified; 751 return modified;
762 } 752 }
753 zstream.next_in = (Bytef *)old_data;
754 zstream.avail_in = new_size;
755
756 n = 0;
757 new_data = NULL;
758 do {
759 n++;
760 new_data = (unsigned char *)realloc(new_data, n * 4000);
761 zstream.next_out = (Bytef *)&new_data[(n - 1) * 4000];
762 zstream.avail_out = 4000;
763 result = inflate(&zstream, Z_NO_FLUSH);
764 if ((result != Z_OK) && (result != Z_STREAM_END)) {
765 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] Zlib decompression failed. "
766 "Result: %d \n", result);
767 safefree(new_data);
768 data = old_data;
769 size = new_size;
770 inflateEnd(&zstream);
771 return modified;
772 }
773 } while ((zstream.avail_out == 0) &&
774 (zstream.avail_in != 0) && (result != Z_STREAM_END));
763 775
764 mp_msg(MSGT_DEMUX, MSGL_DBG2, "[mkv] zlib decompression: from %d to " 776 mp_msg(MSGT_DEMUX, MSGL_DBG2, "[mkv] zlib decompression: from %d to "
765 "%d\n", new_size, 20 * new_size - track->zstream.avail_out); 777 "%d \n", new_size, zstream.total_out);
766 new_size = 20 * new_size - track->zstream.avail_out; 778 new_size = zstream.total_out;
767 inflateEnd(&track->zstream); 779 inflateEnd(&zstream);
768 780
769 if (modified) 781 if (modified)
770 safefree(old_data); 782 safefree(old_data);
771 modified = true; 783 modified = true;
772 } 784 }