File: demux_avs.c

package info (click to toggle)
gmerlin-avdecoder 2.0.0~svn6298~dfsg0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,112 kB
  • sloc: ansic: 94,580; sh: 705; makefile: 705; awk: 43; sed: 16
file content (331 lines) | stat: -rw-r--r-- 9,441 bytes parent folder | download | duplicates (3)
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
/*****************************************************************
 * 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 <avdec_private.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#define LOG_DOMAIN "avs"

#define AUDIO_ID 0
#define VIDEO_ID 1

#define AVS_VIDEO     0x01
#define AVS_AUDIO     0x02
#define AVS_PALETTE   0x03
#define AVS_GAME_DATA 0x04

typedef struct
  {
  uint8_t type;
  uint32_t size;
  uint8_t frequency_divisor;
  uint8_t data_packing;
  } audio_header_t;

static int read_audio_header(bgav_input_context_t * input,
                             audio_header_t * ah)
  {
  return
    bgav_input_read_data(input, &ah->type, 1) &&
    bgav_input_read_24_le(input, &ah->size) &&
    bgav_input_read_data(input, &ah->frequency_divisor, 1) &&
    bgav_input_read_data(input, &ah->data_packing, 1);
  }
#if 0
static void dump_audio_header(audio_header_t * ah)
  {
  bgav_dprintf("AVS audio header\n");
  bgav_dprintf("  .type =              %d\n", ah->type);
  bgav_dprintf("  size:              %d\n", ah->size);
  bgav_dprintf("  frequency_divisor: %d\n", ah->frequency_divisor);
  bgav_dprintf("  data_packing:      %d\n", ah->data_packing);
  }
#endif
static const uint8_t avs_sig[10] =
  { 0x77, 0x57, 0x10, 0x00, 0x3e, 0x01, 0xc6, 0x00, 0x08, 0x00 };

typedef struct
  {
  int audio_bytes_remaining;
  int need_audio_format;
  } avs_priv_t;

static int probe_avs(bgav_input_context_t * input)
  {
  uint8_t data[10];
  if(bgav_input_get_data(input, data, 10) < 10)
    return 0;

  if(!memcmp(data, avs_sig, 10))
    return 1;
  
  return 0;
  }

static int next_packet_avs(bgav_demuxer_context_t * ctx)
  {
  audio_header_t ah;
  uint8_t  block_header[4];
  uint16_t frame_size;
  uint16_t block_size;
  uint16_t block_type;
  int64_t frame_start;
  int bytes_to_read;
  bgav_stream_t * as;
  bgav_stream_t * vs;
  
  avs_priv_t * priv;
  priv = ctx->priv;
  
  /* We process an entire frame */

  frame_start = ctx->input->position;
  
  /* Check if we have data left */
  if(!bgav_input_read_16_le(ctx->input, &frame_size) ||
     !frame_size)
    return 0;
  
  /* Actual frame size */
  if(!bgav_input_read_16_le(ctx->input, &frame_size))
    return 0;

  if(priv->need_audio_format)
    {
    as = bgav_track_find_stream_all(ctx->tt->cur, AUDIO_ID);
    vs = bgav_track_find_stream_all(ctx->tt->cur, VIDEO_ID);
    }
  else
    {
    as = bgav_track_find_stream(ctx, AUDIO_ID);
    vs = bgav_track_find_stream(ctx, VIDEO_ID);
    }
  
  while(ctx->input->position - frame_start < frame_size)
    {
    if(bgav_input_read_data(ctx->input, block_header, 4) < 4)
      return 0;
    block_type = GAVL_PTR_2_16LE(&block_header[0]);
    block_size = GAVL_PTR_2_16LE(&block_header[2]);
    switch(block_type >> 8)
      {
      case 0x01: /* Video data */

        if(!vs || priv->need_audio_format)
          {
          bgav_input_skip(ctx->input, block_size - 4);
          break;
          }
        if(!vs->packet)
          {
          vs->packet =
            bgav_stream_get_packet_write(vs);
          vs->packet->data_size = 0;
          }

        bgav_packet_alloc(vs->packet, vs->packet->data_size + block_size);

        memcpy(vs->packet->data + vs->packet->data_size, block_header, 4);
        vs->packet->data_size += 4;
        
        if(bgav_input_read_data(ctx->input,
                                vs->packet->data + vs->packet->data_size,
                                block_size - 4) < block_size - 4)
          return 0;
        vs->packet->data_size += (block_size - 4);
        vs->packet->pts = vs->in_position;
        bgav_stream_done_packet_write(vs, vs->packet);
        vs->packet = NULL;
        break;
      case 0x03: /* Palette data */

        if(!vs || priv->need_audio_format)
          {
          bgav_input_skip(ctx->input, block_size - 4);
          break;
          }
        if(vs->packet)
          {
          gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN,
                   "2 Palette blocks without intermediate video block");
          return 0;
          }
        vs->packet =
          bgav_stream_get_packet_write(vs);
        vs->packet->data_size = 0;
        
        bgav_packet_alloc(vs->packet, block_size);
        
        memcpy(vs->packet->data + vs->packet->data_size, block_header, 4);
        vs->packet->data_size += 4;

        if(bgav_input_read_data(ctx->input,
                                vs->packet->data + vs->packet->data_size,
                                block_size - 4) < block_size - 4)
          return 0;
        vs->packet->data_size += block_size - 4;
        break;
      case 0x02: /* Audio data */

        if(priv->need_audio_format)
          {
          /* Initialize audio stream */
          as = bgav_track_add_audio_stream(ctx->tt->cur, ctx->opt);
          as->stream_id = AUDIO_ID;
          as->fourcc = BGAV_WAVID_2_FOURCC(0x0001);
          as->data.audio.bits_per_sample = 8;
          as->data.audio.format->num_channels = 1;
          }
        if(!as)
          {
          bgav_input_skip(ctx->input, block_size - 4);
          break;
          }
        block_size -= 4;
        
        while(block_size)
          {
          /* Read audio header */
          if(!as->packet)
            {
            if(block_size < 6)
              {
              bgav_input_skip(ctx->input, block_size);
              break;
              }
            read_audio_header(ctx->input, &ah);
            block_size -= 6;
            
            if(!as->data.audio.format->samplerate)
              as->data.audio.format->samplerate =
                1000000 / (256 - ah.frequency_divisor);
            else
              {
              as->packet =
                bgav_stream_get_packet_write(as);
              as->packet->data_size = 0;
              }
            //            dump_audio_header(&ah);
            priv->audio_bytes_remaining = ah.size - 2;
            }
          /* Read data */
          bytes_to_read = priv->audio_bytes_remaining;
          if(bytes_to_read > block_size)
            bytes_to_read = block_size;

          if(as->packet)
            {
            bgav_packet_alloc(as->packet,
                              as->packet->data_size + bytes_to_read);
            if(bgav_input_read_data(ctx->input,
                                    as->packet->data + as->packet->data_size,
                                    bytes_to_read) < bytes_to_read)
              return 0;
            
            as->packet->data_size += bytes_to_read;
            }
          else
            bgav_input_skip(ctx->input, bytes_to_read);
          
          priv->audio_bytes_remaining -= bytes_to_read;
          block_size -= bytes_to_read;
          
          if(!priv->audio_bytes_remaining && as->packet)
            {
            bgav_stream_done_packet_write(as, as->packet);
            as->packet = NULL;
            }
          }
        break;
      case 0x04: /* Game data */
        bgav_input_skip(ctx->input, block_size - 4);
        break;
      default:
        bgav_input_skip(ctx->input, block_size - 4);
        break;
      }
    }
  
  return 1;
  }

static int open_avs(bgav_demuxer_context_t * ctx)
  {
  uint8_t header[16];
  bgav_stream_t * s;
  avs_priv_t * priv;
  
  if(bgav_input_read_data(ctx->input, header, 16) < 16)
    return 0;

  priv = calloc(1, sizeof(*priv));
  ctx->priv = priv;
  
  ctx->tt = bgav_track_table_create(1);
  
  /* Initialize video stream */
  s = bgav_track_add_video_stream(ctx->tt->cur, ctx->opt);
  s->stream_id = VIDEO_ID;
  s->fourcc = BGAV_MK_FOURCC('A','V','S',' ');
  s->data.video.format->image_width = 318;
  s->data.video.format->frame_width = 318;

  s->data.video.format->image_height = 198;
  s->data.video.format->frame_height = 198;

  s->data.video.format->pixel_width  = 1;
  s->data.video.format->pixel_height = 1;

  s->data.video.format->timescale = GAVL_PTR_2_16LE(&header[10]);
  s->data.video.format->frame_duration = 1;
  s->data.video.depth = 8;
  
  s->stats.pts_end = GAVL_PTR_2_32LE(&header[12]);
  
  ctx->data_start = ctx->input->position;
  ctx->flags |= BGAV_DEMUXER_HAS_DATA_START;
  
#if 1
  priv->need_audio_format = 1;
  if(!next_packet_avs(ctx))
    return 0;
  priv->need_audio_format = 0;
#endif
  return 1;
  }


static void close_avs(bgav_demuxer_context_t * ctx)
  {
  avs_priv_t * priv;
  priv = ctx->priv;
  free(priv);
  }

const bgav_demuxer_t bgav_demuxer_avs =
  {
    .probe =       probe_avs,
    .open =        open_avs,
    .next_packet = next_packet_avs,
    .close =       close_avs
  };