1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
/*****************************************************************
* gmerlin - a general purpose multimedia framework and applications
*
* Copyright (c) 2001 - 2010 Members of the Gmerlin project
* gmerlin-general@lists.sourceforge.net
* http://gmerlin.sourceforge.net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* *****************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <config.h>
#include <gavl/gavl.h>
#include <gavl/gavldsp.h>
#include <gmerlin/fileformat.h>
#include <gmerlin/serialize.h>
static int read_stdio(void * priv, uint8_t * data, int len)
{
FILE * f = priv;
return fread(data, 1, len, f);
}
static int write_stdio(void * priv, const uint8_t * data, int len)
{
FILE * f = priv;
return fwrite(data, 1, len, f);
}
static int64_t ftell_stdio(void * priv)
{
FILE * f = priv;
return ftell(f);
}
static int64_t seek_stdio(void * priv, int64_t pos, int whence)
{
FILE * f = priv;
fseek(f, pos, whence);
return ftell(f);
}
static void close_stdio(void * priv)
{
FILE * f = priv;
if(f)
fclose(f);
}
int bg_f_io_open_stdio_read(bg_f_io_t * io, const char * filename)
{
FILE * f;
f = fopen(filename, "r");
if(!f)
return 0;
io->data = f;
io->read_callback = read_stdio;
io->seek_callback = seek_stdio;
io->ftell_callback = ftell_stdio;
io->close_callback = close_stdio;
return 1;
}
int bg_f_io_open_stdio_write(bg_f_io_t * io, const char * filename)
{
FILE * f;
f = fopen(filename, "w");
if(!f)
return 0;
io->data = f;
io->write_callback = write_stdio;
io->seek_callback = seek_stdio;
io->ftell_callback = ftell_stdio;
io->close_callback = close_stdio;
return 1;
}
void bg_f_io_close(bg_f_io_t * io)
{
if(io->close_callback)
io->close_callback(io->data);
if(io->buffer)
free(io->buffer);
}
static void bg_f_io_buffer_alloc(bg_f_io_t * io, int len)
{
if(len <= io->buffer_alloc)
return;
io->buffer_alloc = len + 512;
io->buffer = realloc(io->buffer, io->buffer_alloc);
}
static inline int read_32(bg_f_io_t * io, uint32_t * val)
{
uint8_t data[4];
if(io->read_callback(io->data, data, 4) < 4)
return 0;
*val = ((data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]);
return 1;
}
static inline int read_64(bg_f_io_t * io, uint64_t * val)
{
uint8_t data[8];
if(io->read_callback(io->data, data, 8) < 8)
return 0;
*val = data[0]; *val <<= 8;
*val |= data[1]; *val <<= 8;
*val |= data[2]; *val <<= 8;
*val |= data[3]; *val <<= 8;
*val |= data[4]; *val <<= 8;
*val |= data[5]; *val <<= 8;
*val |= data[6]; *val <<= 8;
*val |= data[7];
return 1;
}
static inline int write_32(bg_f_io_t * io, uint32_t val)
{
uint8_t data[4];
data[0] = (val & 0xff000000) >> 24;
data[1] = (val & 0xff0000) >> 16;
data[2] = (val & 0xff00) >> 8;
data[3] = (val & 0xff);
return io->write_callback(io->data, data, 4);
}
static inline int write_64(bg_f_io_t * io, uint64_t val)
{
uint8_t data[8];
data[0] = (val & 0xff00000000000000LL) >> 56;
data[1] = (val & 0x00ff000000000000LL) >> 48;
data[2] = (val & 0x0000ff0000000000LL) >> 40;
data[3] = (val & 0x000000ff00000000LL) >> 32;
data[4] = (val & 0x00000000ff000000LL) >> 24;
data[5] = (val & 0x0000000000ff0000LL) >> 16;
data[6] = (val & 0x000000000000ff00LL) >> 8;
data[7] = (val & 0x00000000000000ffLL);
return io->write_callback(io->data, data, 8);
}
int bg_f_chunk_read(bg_f_io_t * io, bg_f_chunk_t * ch)
{
ch->start = io->ftell_callback(io->data);
if(!read_32(io, &ch->type) ||
!read_64(io, &ch->size))
return 0;
return 1;
}
int bg_f_chunk_write_header(bg_f_io_t * io, bg_f_chunk_t * ch, bg_f_chunk_type_t type)
{
ch->type = type;
ch->start = io->ftell_callback(io->data);
if(!write_32(io, ch->type) ||
!write_64(io, ch->size))
return 0;
return 1;
}
int bg_f_chunk_write_footer(bg_f_io_t * io, bg_f_chunk_t * ch)
{
int64_t end_pos = io->ftell_callback(io->data);
io->seek_callback(io->data, ch->start + 4, SEEK_SET);
ch->size = end_pos - ch->start - 12;
if(!write_64(io, ch->size))
return 0;
io->seek_callback(io->data, end_pos, SEEK_SET);
return 1;
}
int bg_f_signature_read(bg_f_io_t * io, bg_f_chunk_t * ch, bg_f_signature_t * sig)
{
if(!read_32(io, &sig->type))
return 0;
return 1;
}
int bg_f_signature_write(bg_f_io_t * io, bg_f_signature_t * sig)
{
bg_f_chunk_t ch;
if(!bg_f_chunk_write_header(io, &ch, CHUNK_TYPE_SIGNATURE))
return 0;
if(!write_32(io, sig->type))
return 0;
if(!bg_f_chunk_write_footer(io, &ch))
return 0;
return 1;
}
int bg_f_audio_format_read(bg_f_io_t * io, bg_f_chunk_t * ch, gavl_audio_format_t * f,
int * big_endian)
{
bg_f_io_buffer_alloc(io, ch->size);
if(io->read_callback(io->data, io->buffer, ch->size) < ch->size)
return 0;
bg_deserialize_audio_format(f, io->buffer, ch->size, big_endian);
return 1;
}
int bg_f_audio_format_write(bg_f_io_t * io, const gavl_audio_format_t * f)
{
bg_f_chunk_t ch;
int len;
if(!bg_f_chunk_write_header(io, &ch, CHUNK_TYPE_AUDIO_FORMAT))
return 0;
len = bg_serialize_audio_format(f, NULL, 0);
bg_f_io_buffer_alloc(io, len);
len = bg_serialize_audio_format(f, io->buffer, len);
if(io->write_callback(io->data, io->buffer, len) < len)
return 0;
if(!bg_f_chunk_write_footer(io, &ch))
return 0;
return 1;
}
int bg_f_video_format_read(bg_f_io_t * io, bg_f_chunk_t * ch, gavl_video_format_t * f,
int * big_endian)
{
bg_f_io_buffer_alloc(io, ch->size);
if(io->read_callback(io->data, io->buffer, ch->size) < ch->size)
return 0;
bg_deserialize_video_format(f, io->buffer, ch->size, big_endian);
return 1;
}
int bg_f_video_format_write(bg_f_io_t * io, const gavl_video_format_t * f)
{
bg_f_chunk_t ch;
int len;
if(!bg_f_chunk_write_header(io, &ch, CHUNK_TYPE_VIDEO_FORMAT))
return 0;
len = bg_serialize_video_format(f, NULL, 0);
bg_f_io_buffer_alloc(io, len);
len = bg_serialize_video_format(f, io->buffer, len);
if(io->write_callback(io->data, io->buffer, len) < len)
return 0;
if(!bg_f_chunk_write_footer(io, &ch))
return 0;
return 1;
}
int bg_f_audio_frame_read(bg_f_io_t * io, bg_f_chunk_t * ch,
const gavl_audio_format_t * format,
gavl_audio_frame_t * f, int big_endian,
gavl_dsp_context_t * ctx)
{
int len;
len = bg_serialize_audio_frame_header(format, f, NULL, 0);
bg_f_io_buffer_alloc(io, len);
if(io->read_callback(io->data, io->buffer, len) < len)
return 0;
bg_deserialize_audio_frame_header(format, f, io->buffer, len);
return bg_deserialize_audio_frame(ctx,
format, f, io->read_callback,
io->data, big_endian);
}
int bg_f_audio_frame_write(bg_f_io_t * io,
const gavl_audio_format_t * format,
const gavl_audio_frame_t * f)
{
int len;
bg_f_chunk_t ch;
if(!bg_f_chunk_write_header(io, &ch, CHUNK_TYPE_AUDIO_FRAME))
return 0;
len = bg_serialize_audio_frame_header(format, f, NULL, 0);
bg_f_io_buffer_alloc(io, len);
bg_serialize_audio_frame_header(format, f, io->buffer, len);
if(io->write_callback(io->data, io->buffer, len) < len)
return 0;
if(!bg_serialize_audio_frame(format,
f, io->write_callback,
io->data))
return 0;
if(!bg_f_chunk_write_footer(io, &ch))
return 0;
return 1;
}
int bg_f_video_frame_read(bg_f_io_t * io, bg_f_chunk_t * ch,
const gavl_video_format_t * format,
gavl_video_frame_t * f, int big_endian,
gavl_dsp_context_t * ctx)
{
int len;
len = bg_serialize_video_frame_header(format, f, NULL, 0);
bg_f_io_buffer_alloc(io, len);
if(io->read_callback(io->data, io->buffer, len) < len)
return 0;
bg_deserialize_video_frame_header(format, f, io->buffer, len);
return bg_deserialize_video_frame(ctx,
format, f, io->read_callback,
io->data, big_endian);
}
int bg_f_video_frame_write(bg_f_io_t * io,
const gavl_video_format_t * format,
const gavl_video_frame_t * f)
{
int len;
bg_f_chunk_t ch;
if(!bg_f_chunk_write_header(io, &ch, CHUNK_TYPE_VIDEO_FRAME))
return 0;
len = bg_serialize_video_frame_header(format, f, NULL, 0);
bg_f_io_buffer_alloc(io, len);
bg_serialize_video_frame_header(format, f, io->buffer, len);
if(io->write_callback(io->data, io->buffer, len) < len)
return 0;
if(!bg_serialize_video_frame(format,
f, io->write_callback,
io->data))
return 0;
if(!bg_f_chunk_write_footer(io, &ch))
return 0;
return 1;
}
|