11590
|
1 /*
|
|
2 LMLM4 MPEG4 Compression Card stream & file parser by Maxim Yevtyushkin <max@linuxmedialabs.com>
|
|
3 Based on SMJPEG file parser by Alex Beregszaszi
|
|
4
|
|
5 */
|
|
6
|
|
7 #include <stdio.h>
|
|
8 #include <stdlib.h>
|
|
9 #include <unistd.h>
|
|
10 #include <string.h> /* strtok */
|
|
11
|
|
12 #include "config.h"
|
|
13 #include "mp_msg.h"
|
|
14 #include "help_mp.h"
|
|
15
|
|
16 #include "stream.h"
|
|
17 #include "demuxer.h"
|
|
18 #include "stheader.h"
|
|
19 #include "bswap.h"
|
|
20
|
|
21 typedef struct __FrameInfo
|
|
22 {
|
|
23 ssize_t frameSize;
|
|
24 ssize_t paddingSize;
|
|
25 int frameType;
|
|
26 int channelNo;
|
|
27 } FrameInfo;
|
|
28
|
|
29 #define FRAMETYPE_I 0
|
|
30 #define FRAMETYPE_P 1
|
|
31 #define FRAMETYPE_B 2
|
|
32 #define FRAMETYPE_AUDIO_MPEG1L2 4
|
|
33 #define FRAMETYPE_AUDIO_ULAW 5
|
|
34 #define FRAMETYPE_AUDIO_ADPCM 6
|
|
35
|
|
36 #define PACKET_BLOCK_SIZE 0x00000200
|
|
37 #define PACKET_BLOCK_LAST 0x000001FF
|
|
38 #define PACKET_BLOCK_MASK 0xFFFFFE00
|
|
39
|
|
40 #define MAX_PACKET_SIZE 1048576 // 1 Mb
|
|
41
|
|
42 #define STREAM_START_CODE_SIZE 4
|
|
43
|
|
44 /*
|
|
45 // codes in MSB first
|
|
46 static unsigned int start_code [] =
|
|
47 {
|
|
48 0xB0010000, // VISUAL_OBJECT_SEQUENCE_START_CODE
|
|
49 0xB6010000, // VOP_START_CODE
|
|
50 0x04C4FDFF, // MPEG1LAYERII_START_CODE
|
|
51 0x00000000 // end of start codes list
|
|
52 };
|
|
53 */
|
|
54
|
|
55 static int imeHeaderValid(FrameInfo *frame)
|
|
56 {
|
|
57 if ( frame->channelNo > 7 ||
|
|
58 frame->frameSize > MAX_PACKET_SIZE || frame->frameSize <= 0)
|
|
59 {
|
|
60 mp_msg(MSGT_DEMUX, MSGL_V, "Invalid packet in LMLM4 stream: ch=%d size=%d\n", frame->channelNo, frame->frameSize);
|
|
61 return 0;
|
|
62 }
|
|
63 switch (frame->frameType) {
|
|
64 case FRAMETYPE_I:
|
|
65 case FRAMETYPE_P:
|
|
66 case FRAMETYPE_B:
|
|
67 case FRAMETYPE_AUDIO_MPEG1L2:
|
|
68 case FRAMETYPE_AUDIO_ULAW:
|
|
69 case FRAMETYPE_AUDIO_ADPCM:
|
|
70 break;
|
|
71 default:
|
|
72 mp_msg(MSGT_DEMUX, MSGL_V, "Invalid packet in LMLM4 stream (wrong packet type %d)\n", frame->frameType);
|
|
73 return 0;
|
|
74 }
|
|
75 return 1;
|
|
76 }
|
|
77
|
|
78 /*
|
|
79 int searchMPEG4Stream(demuxer_t* demuxer, IME6400Header *imeHeader)
|
|
80 {
|
|
81 void *data;
|
|
82 ssize_t imeHeaderSize = sizeof(IME6400Header);
|
|
83 ssize_t dataSize = sizeof(IME6400Header) * 3;
|
|
84 ssize_t ptr = imeHeaderSize * 2;
|
|
85 int errNo, startCodeNo;
|
|
86 off_t pos;
|
|
87
|
|
88 data = malloc(dataSize);
|
|
89
|
|
90 imeHeaderSwap(imeHeader);
|
|
91 memcpy(data + imeHeaderSize, imeHeader, imeHeaderSize);
|
|
92
|
|
93 // printHex(data + imeHeaderSize, imeHeaderSize);
|
|
94
|
|
95 while ((errNo = stream_read(demuxer->stream, data + imeHeaderSize * 2 , imeHeaderSize)) == imeHeaderSize)
|
|
96 {
|
|
97 // printHex(data + imeHeaderSize * 2, imeHeaderSize);
|
|
98
|
|
99 pos = stream_tell(demuxer->stream);
|
|
100 while (dataSize - ptr >= STREAM_START_CODE_SIZE) {
|
|
101 startCodeNo = 0;
|
|
102 while (start_code[startCodeNo])
|
|
103 {
|
|
104 if (memcmp(&start_code[startCodeNo], data + ptr, STREAM_START_CODE_SIZE) == 0) // start code match
|
|
105 {
|
|
106 memcpy(imeHeader, data + ptr - imeHeaderSize, imeHeaderSize);
|
|
107 imeHeaderSwap(imeHeader);
|
|
108 if (imeHeaderValid(imeHeader))
|
|
109 {
|
|
110 stream_seek(demuxer->stream, pos - (dataSize - ptr));
|
|
111 free(data);
|
|
112 return 0;
|
|
113 }
|
|
114 }
|
|
115 startCodeNo++;
|
|
116 }
|
|
117 ptr++;
|
|
118 }
|
|
119 memcpy(data,data + imeHeaderSize, imeHeaderSize * 2);
|
|
120 ptr -= imeHeaderSize;
|
|
121 }
|
|
122
|
|
123 free(data);
|
|
124 return errNo;
|
|
125 }
|
|
126 */
|
|
127
|
|
128 static int getFrame(demuxer_t *demuxer, FrameInfo *frameInfo)
|
|
129 {
|
|
130 unsigned int packetSize;
|
|
131
|
|
132 frameInfo->channelNo = stream_read_word(demuxer->stream);
|
|
133 frameInfo->frameType = stream_read_word(demuxer->stream);
|
|
134 packetSize=stream_read_dword(demuxer->stream);
|
|
135
|
|
136 if(stream_eof(demuxer->stream)){
|
|
137 frameInfo->frameSize = 0;
|
|
138 return 0;
|
|
139 }
|
|
140
|
|
141 frameInfo->frameSize = packetSize - 8; //sizeof(IME6400Header);
|
|
142 frameInfo->paddingSize = (packetSize & PACKET_BLOCK_LAST) ? PACKET_BLOCK_SIZE - (packetSize & PACKET_BLOCK_LAST) : 0;
|
|
143
|
|
144 mp_msg(MSGT_DEMUX, MSGL_DBG2, "typ: %d chan: %d size: %d pad: %d\n",
|
|
145 frameInfo->frameType,
|
|
146 frameInfo->channelNo,
|
|
147 frameInfo->frameSize,
|
|
148 frameInfo->paddingSize);
|
|
149
|
|
150 if(!imeHeaderValid(frameInfo)){
|
|
151 // skip this packet
|
|
152 stream_skip(demuxer->stream,PACKET_BLOCK_SIZE-8);
|
|
153 frameInfo->frameSize = 0;
|
|
154 return -1;
|
|
155 }
|
|
156
|
|
157 return 1;
|
|
158 }
|
|
159
|
16175
|
160 static int lmlm4_check_file(demuxer_t* demuxer)
|
11590
|
161 {
|
|
162 FrameInfo frameInfo;
|
|
163 unsigned int first;
|
|
164
|
|
165 mp_msg(MSGT_DEMUX, MSGL_V, "Checking for LMLM4 Stream Format\n");
|
|
166
|
|
167 if(getFrame(demuxer, &frameInfo)!=1){
|
|
168 stream_skip(demuxer->stream,-8);
|
11790
|
169 mp_msg(MSGT_DEMUX, MSGL_V, "LMLM4 Stream Format not found\n");
|
11590
|
170 return 0;
|
|
171 }
|
|
172 first=stream_read_dword(demuxer->stream);
|
|
173 stream_skip(demuxer->stream,-12);
|
|
174
|
|
175 mp_msg(MSGT_DEMUXER,MSGL_V,"LMLM4: first=0x%08X\n",first);
|
|
176
|
|
177 switch(frameInfo.frameType){
|
|
178 case FRAMETYPE_AUDIO_MPEG1L2:
|
|
179 if( (first & 0xffe00000) != 0xffe00000 ){
|
|
180 mp_msg(MSGT_DEMUXER,MSGL_V,"LMLM4: not mpeg audio\n");
|
|
181 return 0;
|
|
182 }
|
|
183 if((4-((first>>17)&3))!=2){
|
|
184 mp_msg(MSGT_DEMUXER,MSGL_V,"LMLM4: not layer-2\n");
|
|
185 return 0;
|
|
186 }
|
|
187 if(((first>>10)&0x3)==3){
|
|
188 mp_msg(MSGT_DEMUXER,MSGL_V,"LMLM4: invalid audio sampelrate\n");
|
|
189 return 0;
|
|
190 }
|
|
191 mp_msg(MSGT_DEMUXER,MSGL_V,"LMLM4: first packet is audio, header checks OK!\n");
|
|
192 break;
|
|
193 // TODO: add checks for video header too, for case of disabled audio
|
|
194 }
|
|
195
|
|
196
|
|
197 // stream_reset(demuxer->stream);
|
|
198 mp_msg(MSGT_DEMUX, MSGL_V, "LMLM4 Stream Format found\n");
|
|
199
|
16175
|
200 return DEMUXER_TYPE_LMLM4;
|
11590
|
201 }
|
|
202
|
|
203 static int video = 0;
|
|
204 static int frames= 0;
|
|
205
|
|
206 // return value:
|
|
207 // 0 = EOF or no stream found
|
|
208 // 1 = successfully read a packet
|
16175
|
209 static int demux_lmlm4_fill_buffer(demuxer_t *demux, demux_stream_t *ds)
|
11590
|
210 {
|
|
211 FrameInfo frameInfo;
|
|
212 double pts;
|
|
213 int id=1;
|
|
214 int ret;
|
|
215
|
|
216 hdr:
|
|
217 demux->filepos = stream_tell(demux->stream);
|
17366
|
218 mp_msg(MSGT_DEMUX, MSGL_DBG2, "fpos = %"PRId64"\n", (int64_t)demux->filepos);
|
11590
|
219
|
|
220 ret=getFrame(demux, &frameInfo);
|
|
221 if(ret<=0) return ret; // EOF/error
|
|
222
|
|
223 pts=demux->video->sh ? frames*((sh_video_t*)(demux->video->sh))->frametime : 0;
|
|
224
|
|
225 switch(frameInfo.frameType){
|
|
226 case FRAMETYPE_AUDIO_MPEG1L2:
|
|
227 mp_dbg(MSGT_DEMUX, MSGL_DBG2, "Audio Packet\n");
|
|
228 if (!video)
|
|
229 {
|
|
230 stream_skip(demux->stream, frameInfo.frameSize + frameInfo.paddingSize);
|
|
231 mp_msg(MSGT_DEMUX, MSGL_V, "Skip Audio Packet\n");
|
|
232 return -1; //goto hdr;
|
|
233 }
|
|
234 if(demux->audio->id==-1){
|
|
235 if(!demux->a_streams[id]) new_sh_audio(demux,id);
|
|
236 demux->audio->id=id;
|
|
237 demux->audio->sh=demux->a_streams[id];
|
|
238 ((sh_audio_t*)(demux->audio->sh))->format=0x50; // mpeg audio layer 1/2
|
|
239 }
|
|
240 if(demux->audio->id==id)
|
|
241 ds_read_packet(demux->audio, demux->stream, frameInfo.frameSize,
|
|
242 pts, demux->filepos, 0);
|
|
243 else
|
|
244 stream_skip(demux->stream,frameInfo.frameSize);
|
|
245 break;
|
|
246 case FRAMETYPE_I:
|
|
247 if (!video) {
|
|
248 video = 1;
|
|
249 mp_dbg(MSGT_DEMUX, MSGL_DBG2, "First Video Packet\n");
|
|
250 }
|
|
251 case FRAMETYPE_P:
|
|
252 frames=(frames+1)&(1024*1024-1); // wrap around at 4 hrs to avoid inaccurate float calculations
|
|
253 if (!video)
|
|
254 {
|
|
255 stream_skip(demux->stream, frameInfo.frameSize + frameInfo.paddingSize);
|
|
256 mp_msg(MSGT_DEMUX, MSGL_V, "Skip Video P Packet\n");
|
|
257 return -1; //goto hdr;
|
|
258 }
|
|
259 mp_dbg(MSGT_DEMUX, MSGL_DBG2, "Video Packet\n");
|
|
260 if(demux->video->id==-1){
|
|
261 if(!demux->v_streams[id]) new_sh_video(demux,id);
|
|
262 demux->video->id=id;
|
|
263 demux->video->sh=demux->v_streams[id];
|
|
264 ((sh_video_t*)(demux->video->sh))->format=0x10000004; // mpeg4-ES
|
|
265 }
|
|
266 if(demux->video->id==id)
|
|
267 ds_read_packet(demux->video, demux->stream, frameInfo.frameSize,
|
|
268 pts, demux->filepos, 0);
|
|
269 break;
|
|
270 default:
|
|
271 stream_skip(demux->stream,frameInfo.frameSize);
|
|
272 }
|
|
273
|
|
274 stream_skip(demux->stream, frameInfo.paddingSize);
|
|
275
|
|
276 return 1;
|
|
277 }
|
|
278
|
16175
|
279 static demuxer_t* demux_open_lmlm4(demuxer_t* demuxer){
|
|
280 sh_audio_t *sh_audio=NULL;
|
|
281 sh_video_t *sh_video=NULL;
|
11590
|
282
|
|
283 #if 0
|
|
284 sh_video_t* sh_video;
|
|
285 sh_audio_t* sh_audio;
|
|
286 unsigned int htype = 0, hleng;
|
|
287 int i = 0;
|
|
288
|
|
289 sh_video = new_sh_video(demuxer, 0);
|
|
290 demuxer->video->sh = sh_video;
|
|
291 sh_video->ds = demuxer->video;
|
|
292 sh_video->disp_w = 640;
|
|
293 sh_video->disp_h = 480;
|
|
294 sh_video->format = mmioFOURCC('D','I','V','X');
|
|
295
|
|
296 sh_video->bih = malloc(sizeof(BITMAPINFOHEADER));
|
|
297 memset(sh_video->bih, 0, sizeof(BITMAPINFOHEADER));
|
|
298
|
|
299 /* these are false values */
|
|
300 sh_video->bih->biSize = 40;
|
|
301 sh_video->bih->biWidth = sh_video->disp_w;
|
|
302 sh_video->bih->biHeight = sh_video->disp_h;
|
|
303 sh_video->bih->biPlanes = 3;
|
|
304 sh_video->bih->biBitCount = 16;
|
|
305 sh_video->bih->biCompression = sh_video->format;
|
|
306 sh_video->bih->biSizeImage = sh_video->disp_w*sh_video->disp_h;
|
|
307
|
|
308 sh_audio = new_sh_audio(demuxer, 0);
|
|
309 demuxer->audio->sh = sh_audio;
|
|
310 sh_audio->ds = demuxer->audio;
|
|
311
|
|
312 sh_audio->wf = malloc(sizeof(WAVEFORMATEX));
|
|
313 memset(sh_audio->wf, 0, sizeof(WAVEFORMATEX));
|
|
314
|
|
315 sh_audio->samplerate = 48000;
|
|
316 sh_audio->wf->wBitsPerSample = 16;
|
|
317 sh_audio->channels = 2;
|
|
318 sh_audio->format = 0x50;
|
|
319 sh_audio->wf->wFormatTag = sh_audio->format;
|
|
320 sh_audio->wf->nChannels = sh_audio->channels;
|
|
321 sh_audio->wf->nSamplesPerSec = sh_audio->samplerate;
|
|
322 sh_audio->wf->nAvgBytesPerSec = sh_audio->wf->nChannels*
|
|
323 sh_audio->wf->wBitsPerSample*sh_audio->wf->nSamplesPerSec/8;
|
|
324 sh_audio->wf->nBlockAlign = sh_audio->channels *2;
|
|
325 sh_audio->wf->cbSize = 0;
|
|
326
|
|
327 #endif
|
|
328
|
|
329 demuxer->seekable = 0;
|
|
330
|
16175
|
331 if(!ds_fill_buffer(demuxer->video)){
|
|
332 mp_msg(MSGT_DEMUXER,MSGL_INFO,"LMLM4: " MSGTR_MissingVideoStream);
|
|
333 demuxer->video->sh=NULL;
|
|
334 } else {
|
|
335 sh_video=demuxer->video->sh;sh_video->ds=demuxer->video;
|
|
336 }
|
|
337 if(demuxer->audio->id!=-2) {
|
|
338 if(!ds_fill_buffer(demuxer->audio)){
|
|
339 mp_msg(MSGT_DEMUXER,MSGL_INFO,"LMLM4: " MSGTR_MissingAudioStream);
|
|
340 demuxer->audio->sh=NULL;
|
|
341 } else {
|
|
342 sh_audio=demuxer->audio->sh;sh_audio->ds=demuxer->audio;
|
|
343 }
|
|
344 }
|
11590
|
345
|
16175
|
346 return demuxer;
|
11590
|
347 }
|
|
348
|
16175
|
349 static void demux_close_lmlm4(demuxer_t *demuxer)
|
11590
|
350 {
|
|
351 // printf("Close LMLM4 Stream\n");
|
|
352 return;
|
|
353 }
|
16175
|
354
|
|
355
|
|
356 demuxer_desc_t demuxer_desc_lmlm4 = {
|
|
357 "LMLM4 MPEG4 Compression Card stream demuxer",
|
|
358 "lmlm4",
|
|
359 "RAW LMLM4",
|
|
360 "Maxim Yevtyushkin",
|
|
361 "",
|
|
362 DEMUXER_TYPE_LMLM4,
|
|
363 0, // unsafe autodetect
|
|
364 lmlm4_check_file,
|
|
365 demux_lmlm4_fill_buffer,
|
|
366 demux_open_lmlm4,
|
|
367 demux_close_lmlm4,
|
|
368 NULL,
|
|
369 NULL
|
|
370 };
|