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
|
/*****************************************************************
* gmerlin-avdecoder - a general purpose multimedia decoding library
*
* Copyright (c) 2001 - 2012 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 <string.h>
#include <stdlib.h>
#include <avdec_private.h>
#include <gavl/gavf.h>
#include <gavl/compression.h>
typedef struct
{
gavf_io_t * io;
gavf_t * dec;
} gavf_demuxer_t;
static int probe_gavf(bgav_input_context_t * input)
{
uint8_t probe_data[8];
if(bgav_input_get_data(input, probe_data, 8) < 8)
return 0;
if(!strncmp((char*)probe_data, "GAVFFIDX", 8) ||
!strncmp((char*)probe_data, "GAVFPHDR", 8))
return 1;
return 0;
}
static int read_func(void * priv, uint8_t * data, int len)
{
return bgav_input_read_data(priv, data, len);
}
static int64_t seek_func(void * priv, int64_t pos, int whence)
{
bgav_input_context_t * ctx = priv;
bgav_input_seek(ctx, pos, whence);
// fprintf(stderr, "Seek func: %ld\n", pos);
return ctx->position;
}
static int init_track(bgav_track_t * track,
gavf_t * dec,
const bgav_options_t * opt)
{
int i;
bgav_stream_t * s;
const gavl_audio_format_t * afmt;
const gavl_video_format_t * vfmt;
const int64_t * pts;
const gavf_program_header_t * ph;
ph = gavf_get_program_header(dec);
gavl_dictionary_copy(track->metadata, &ph->m);
for(i = 0; i < ph->num_streams; i++)
{
s = NULL;
afmt = NULL;
vfmt = NULL;
switch(ph->streams[i].type)
{
case GAVL_STREAM_AUDIO:
s = bgav_track_add_audio_stream(track, opt);
afmt = &ph->streams[i].format.audio;
break;
case GAVL_STREAM_VIDEO:
s = bgav_track_add_video_stream(track, opt);
vfmt = &ph->streams[i].format.video;
break;
case GAVL_STREAM_TEXT:
s = bgav_track_add_text_stream(track, opt, BGAV_UTF8);
gavl_dictionary_get_int(&ph->streams[i].m, GAVL_META_STREAM_SAMPLE_TIMESCALE, &s->timescale);
break;
case GAVL_STREAM_OVERLAY:
s = bgav_track_add_overlay_stream(track, opt);
vfmt = &ph->streams[i].format.video;
break;
case GAVL_STREAM_MSG:
case GAVL_STREAM_NONE:
break;
}
bgav_stream_set_from_gavl(s, &ph->streams[i].ci,
afmt, vfmt,
&ph->streams[i].m);
pts = gavf_first_pts(dec);
if(pts)
{
s->stats.pts_start = pts[i];
pts = gavf_end_pts(dec);
if(pts)
s->stats.pts_end = pts[i];
}
s->stream_id = ph->streams[i].id;
}
return 1;
}
static int open_gavf(bgav_demuxer_context_t * ctx)
{
gavf_options_t * opt;
uint32_t flags = 0;
gavf_demuxer_t * priv = calloc(1, sizeof(*priv));
const gavf_program_header_t * ph;
gavl_time_t duration;
ctx->priv = priv;
priv->dec = gavf_create();
opt = gavf_get_options(priv->dec);
if(ctx->opt->dump_headers)
flags |= GAVF_OPT_FLAG_DUMP_HEADERS;
if(ctx->opt->dump_indices)
flags |= GAVF_OPT_FLAG_DUMP_INDICES;
gavf_options_set_flags(opt, flags);
priv->io =
gavf_io_create(read_func,
NULL,
(ctx->input->flags & BGAV_INPUT_CAN_SEEK_BYTE) ? seek_func : 0,
NULL,
NULL,
ctx->input);
if(!gavf_open_read(priv->dec, priv->io))
return 0;
ctx->tt = bgav_track_table_create(1);
if(!init_track(ctx->tt->cur, priv->dec,
ctx->opt))
return 0;
ph = gavf_get_program_header(priv->dec);
if(gavl_dictionary_get_long(&ph->m, GAVL_META_APPROX_DURATION, &duration))
{
if(ctx->input->flags & BGAV_INPUT_CAN_SEEK_BYTE)
ctx->flags |= BGAV_DEMUXER_CAN_SEEK;
}
gavl_dictionary_set_string(ctx->tt->cur->metadata,
GAVL_META_FORMAT, "GAVF");
return 1;
}
static int select_track_gavf(bgav_demuxer_context_t * ctx, int track)
{
gavf_demuxer_t * priv;
priv = ctx->priv;
gavf_reset(priv->dec);
return 1;
}
static void seek_gavf(bgav_demuxer_context_t * ctx, int64_t time, int scale)
{
int i;
const int64_t * sync_pts;
gavf_demuxer_t * priv;
const gavf_program_header_t * ph;
bgav_stream_t * s;
priv = ctx->priv;
sync_pts = gavf_seek(priv->dec, time, scale);
ph = gavf_get_program_header(priv->dec);
for(i = 0; i < ph->num_streams; i++)
{
s = bgav_track_find_stream(ctx, ph->streams[i].id);
if(s)
STREAM_SET_SYNC(s, sync_pts[i]);
}
}
static int next_packet_gavf(bgav_demuxer_context_t * ctx)
{
bgav_packet_t * bp;
gavl_packet_t gp;
const gavf_packet_header_t * ph;
gavf_demuxer_t * priv;
bgav_stream_t * s;
priv = ctx->priv;
ph = gavf_packet_read_header(priv->dec);
if(!ph)
{
// fprintf(stderr, "Reading packet header failed\n");
return 0;
}
s = bgav_track_find_stream(ctx, ph->stream_id);
if(!s)
{
gavf_packet_skip(priv->dec);
return 1;
}
gavl_packet_init(&gp);
bp = bgav_stream_get_packet_write(s);
gp.data = bp->data;
gp.data_alloc = bp->data_alloc;
if(!gavf_packet_read_packet(priv->dec, &gp))
{
// fprintf(stderr, "Reading packet failed\n");
return 0;
}
bp->data = gp.data;
bp->data_alloc = gp.data_alloc;
bp->data_size = gp.data_len;
bgav_packet_from_gavl(&gp, bp);
bgav_stream_done_packet_write(s, bp);
return 1;
}
static void resync_gavf(bgav_demuxer_context_t * ctx, bgav_stream_t * s)
{
}
static void close_gavf(bgav_demuxer_context_t * ctx)
{
gavf_demuxer_t * priv;
priv = ctx->priv;
gavf_close(priv->dec);
gavf_io_destroy(priv->io);
free(priv);
}
const bgav_demuxer_t bgav_demuxer_gavf =
{
.probe = probe_gavf,
.open = open_gavf,
.select_track = select_track_gavf,
.next_packet = next_packet_gavf,
.seek = seek_gavf,
.resync = resync_gavf,
.close = close_gavf
};
|