File: demux_smjpeg.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 (200 lines) | stat: -rw-r--r-- 5,762 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
/*****************************************************************
 * 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>

#define AUDIO_ID 0
#define VIDEO_ID 1


static const uint8_t signature[8] = { 0x00, 0x0a, 'S', 'M','J','P','E','G' };

static int probe_smjpeg(bgav_input_context_t * input)
  {
  uint8_t probe_data[8];
  if(bgav_input_get_data(input, probe_data, 8) < 8)
    return 0;
  if(!memcmp(probe_data, signature, 8))
    return 1;
  else
    return 0;
  }

static int open_smjpeg(bgav_demuxer_context_t * ctx)
  {
  int done = 0;
  uint32_t tmp_32;
  uint16_t tmp_16;
  uint8_t tmp_8;
  uint32_t fourcc, length;
  bgav_stream_t * s;
  /* Skip signature and version */
  
  bgav_input_skip(ctx->input, 12);

  /* Create track table */
  ctx->tt = bgav_track_table_create(1);
  
  
  /* Duration in milliseconds */
  if(!bgav_input_read_32_be(ctx->input, &tmp_32))
    return 0;

  gavl_track_set_duration(ctx->tt->cur->info, gavl_time_unscale(1000, tmp_32));
  
  while(!done)
    {
    if(!bgav_input_read_fourcc(ctx->input, &fourcc))
      return 0;
    switch(fourcc)
      {
      case BGAV_MK_FOURCC('_','T','X','T'):
        if(!bgav_input_read_32_be(ctx->input, &length))
          return 0;
        bgav_input_skip(ctx->input, length);
        break;
      case BGAV_MK_FOURCC('_','S','N','D'):
        if(!bgav_input_read_32_be(ctx->input, &length))
          return 0;

        s = bgav_track_add_audio_stream(ctx->tt->cur, ctx->opt);
        s->stream_id = AUDIO_ID;
        s->timescale = 1000;
        
        if(!bgav_input_read_16_be(ctx->input, &tmp_16))
          return 0;
        s->data.audio.format->samplerate = tmp_16;

        if(!bgav_input_read_data(ctx->input, &tmp_8, 1))
          return 0;
        s->data.audio.bits_per_sample = tmp_8;

        if(!bgav_input_read_data(ctx->input, &tmp_8, 1))
          return 0;
        s->data.audio.format->num_channels = tmp_8;

        if(!bgav_input_read_fourcc(ctx->input, &fourcc))
          return 0;
        
        if(fourcc == BGAV_MK_FOURCC('N','O','N','E'))
          s->fourcc = BGAV_WAVID_2_FOURCC(0x0001);
        else if(fourcc == BGAV_MK_FOURCC('A','P','C','M'))
          s->fourcc = BGAV_MK_FOURCC('S','M','J','A');
        if(length > 8)
          bgav_input_skip(ctx->input, length - 8);
        break;
      case BGAV_MK_FOURCC('_','V','I','D'):
        if(!bgav_input_read_32_be(ctx->input, &length))
          return 0;

        s = bgav_track_add_video_stream(ctx->tt->cur, ctx->opt);
        s->stream_id = VIDEO_ID;

        bgav_input_skip(ctx->input, 4); // Number of frames

        if(!bgav_input_read_16_be(ctx->input, &tmp_16))
          return 0;

        s->data.video.format->image_width = tmp_16;
        s->data.video.format->frame_width = tmp_16;

        if(!bgav_input_read_16_be(ctx->input, &tmp_16))
          return 0;

        s->data.video.format->image_height = tmp_16;
        s->data.video.format->frame_height = tmp_16;
        s->data.video.format->pixel_width  = 1;
        s->data.video.format->pixel_height = 1;
        s->data.video.format->timescale = 1000;
        s->data.video.format->framerate_mode = GAVL_FRAMERATE_VARIABLE;
        s->flags |= STREAM_NO_DURATIONS;

        if(!bgav_input_read_fourcc(ctx->input, &fourcc))
          return 0;

        s->fourcc = fourcc;
        
        if(length > 12)
          bgav_input_skip(ctx->input, length - 12);
        
        break;
      case BGAV_MK_FOURCC('H','E','N','D'):
        done = 1;
        break;
      }
    }
  ctx->data_start = ctx->input->position;
  ctx->flags |= BGAV_DEMUXER_HAS_DATA_START;
  return 1;
  }

static int next_packet_smjpeg(bgav_demuxer_context_t * ctx)
  {
  uint32_t fourcc, length, timestamp;
  bgav_stream_t * s;
  bgav_packet_t * p;
  
  if(!bgav_input_read_fourcc(ctx->input, &fourcc) ||
     (fourcc == BGAV_MK_FOURCC('D','O','N','E')))
    return 0;
  
  if(!bgav_input_read_32_be(ctx->input, &timestamp) ||
     !bgav_input_read_32_be(ctx->input, &length))
    return 0;
  
  if(fourcc == BGAV_MK_FOURCC('s','n','d','D'))
    s = bgav_track_find_stream(ctx, AUDIO_ID);
  else if(fourcc == BGAV_MK_FOURCC('v','i','d','D'))
    s = bgav_track_find_stream(ctx, VIDEO_ID);
  else
    return 0;
  
  if(!s)
    bgav_input_skip(ctx->input, length);
  else
    {
    p = bgav_stream_get_packet_write(s);
    
    bgav_packet_alloc(p, length);
    if(bgav_input_read_data(ctx->input, p->data, length) < length)
      return 0;

    p->data_size = length;
    p->pts = timestamp;
    bgav_stream_done_packet_write(s, p);
    }
  return 1;
  }

static void close_smjpeg(bgav_demuxer_context_t * ctx)
  {

  }

const bgav_demuxer_t bgav_demuxer_smjpeg =
  {
    .probe =       probe_smjpeg,
    .open =        open_smjpeg,
    .next_packet = next_packet_smjpeg,
    .close =       close_smjpeg
  };