1 /*
2 * Some routines for handling I/O from/to different video
3 * file formats (currently AVI, Quicktime)
4 *
5 * These routines are isolated here in an extra file
6 * in order to be able to handle more formats in the future.
7 *
8 * Copyright (C) 2000 Rainer Johanni <Rainer@Johanni.de>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include <config.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <fcntl.h>
31 #include "lav_io.h"
32
33 #ifdef HAVE_LIBDV
34 #include <libdv/dv.h>
35 #endif
36
37 extern int AVI_errno;
38
39 static char video_format=' ';
40 static int internal_error=0;
41 int libdv_pal_yv12 = -1;
42 uint16_t reorder_16(uint16_t todo, int big_endian);
43
44 #define ERROR_JPEG 1
45 #define ERROR_MALLOC 2
46 #define ERROR_FORMAT 3
47 #define ERROR_NOAUDIO 4
48
49 static unsigned long jpeg_field_size = 0;
50 static unsigned long jpeg_quant_offset = 0;
51 static unsigned long jpeg_huffman_offset = 0;
52 static unsigned long jpeg_image_offset = 0;
53 static unsigned long jpeg_scan_offset = 0;
54 static unsigned long jpeg_data_offset = 0;
55 static unsigned long jpeg_padded_len = 0;
56 static unsigned long jpeg_app0_offset = 0;
57 static unsigned long jpeg_app1_offset = 0;
58
59 #define M_SOF0 0xC0
60 #define M_SOF1 0xC1
61 #define M_DHT 0xC4
62 #define M_SOI 0xD8 /* Start Of Image (beginning of datastream) */
63 #define M_EOI 0xD9 /* End Of Image (end of datastream) */
64 #define M_SOS 0xDA /* Start Of Scan (begins compressed data) */
65 #define M_DQT 0xDB
66 #define M_APP0 0xE0
67 #define M_APP1 0xE1
68
69 #define QUICKTIME_MJPG_TAG 0x6d6a7067 /* 'mjpg' */
70
71 #ifdef HAVE_LIBDV
72 static int check_DV2_input(lav_file_t *lav_fd);
73 #endif
74
75 #define TMP_EXTENSION ".tmp"
76
77 #ifdef HAVE_LIBQUICKTIME
78 /*
79 put_int4:
80 Put a 4 byte integer value into a character array as big endian number
81 */
82
put_int4(unsigned char * buf,int val)83 static void put_int4(unsigned char *buf, int val)
84 {
85 buf[0] = (val >> 24);
86 buf[1] = (val >> 16);
87 buf[2] = (val >> 8 );
88 buf[3] = (val );
89 }
90 #endif
91
92 /*
93 get_int2:
94 get a 2 byte integer value from a character array as big endian number
95 */
96
get_int2(unsigned char * buff)97 static int get_int2(unsigned char *buff)
98 {
99 return (buff[0]*256 + buff[1]);
100 }
101
102 /*
103 scan_jpeg:
104 Scan jpeg data for markers, needed for Quicktime MJPA format
105 and partly for AVI files.
106 Taken mostly from Adam Williams' quicktime library
107 */
108
scan_jpeg(unsigned char * jpegdata,long jpeglen,int header_only)109 static int scan_jpeg(unsigned char * jpegdata, long jpeglen, int header_only)
110 {
111 int marker, length;
112 long p;
113
114 jpeg_field_size = 0;
115 jpeg_quant_offset = 0;
116 jpeg_huffman_offset = 0;
117 jpeg_image_offset = 0;
118 jpeg_scan_offset = 0;
119 jpeg_data_offset = 0;
120 jpeg_padded_len = 0;
121 jpeg_app0_offset = 0;
122 jpeg_app1_offset = 0;
123
124 /* The initial marker must be SOI */
125
126 if (jpegdata[0] != 0xFF || jpegdata[1] != M_SOI) return -1;
127
128 /* p is the pointer within the jpeg data */
129
130 p = 2;
131
132 /* scan through the jpeg data */
133
134 while(p<jpeglen)
135 {
136 /* get next marker */
137
138 /* Find 0xFF byte; skip any non-FFs */
139 while(jpegdata[p] != 0xFF)
140 {
141 p++;
142 if(p>=jpeglen) return -1;
143 }
144
145 /* Get marker code byte, swallowing any duplicate FF bytes */
146 while(jpegdata[p] == 0xFF)
147 {
148 p++;
149 if(p>=jpeglen) return -1;
150 }
151
152 marker = jpegdata[p++];
153
154 if(p<=jpeglen-2)
155 length = get_int2(jpegdata+p);
156 else
157 length = 0;
158
159 /* We found a marker - check it */
160
161 if(marker == M_EOI) { jpeg_field_size = p; break; }
162
163 switch(marker)
164 {
165 case M_SOF0:
166 case M_SOF1:
167 jpeg_image_offset = p-2;
168 break;
169 case M_DQT:
170 if(jpeg_quant_offset==0) jpeg_quant_offset = p-2;
171 break;
172 case M_DHT:
173 if(jpeg_huffman_offset==0) jpeg_huffman_offset = p-2;
174 break;
175 case M_SOS:
176 jpeg_scan_offset = p-2;
177 jpeg_data_offset = p+length;
178 if(header_only) return 0; /* we are done with the headers */
179 break;
180 case M_APP0:
181 if(jpeg_app0_offset==0) jpeg_app0_offset = p-2;
182 break;
183 case M_APP1:
184 if(jpeg_app1_offset==0) jpeg_app1_offset = p-2;
185 break;
186 }
187
188 /* The pseudo marker as well as the markers M_TEM (0x01)
189 and M_RST0 ... M_RST7 (0xd0 ... 0xd7) have no paramters.
190 M_SOI and M_EOI also have no parameters, but we should
191 never come here in that case */
192
193 if(marker == 0 || marker == 1 || (marker >= 0xd0 && marker <= 0xd7))
194 continue;
195
196 /* skip length bytes */
197
198 if(p+length<=jpeglen)
199 p += length;
200 else
201 return -1;
202 }
203
204 /* We are through parsing the jpeg data, we should have seen M_EOI */
205
206 if(!jpeg_field_size) return -1;
207
208 /* Check for trailing garbage until jpeglen is reached or a new
209 M_SOI is seen */
210
211 while(p<jpeglen)
212 {
213 if(p<jpeglen-1 && jpegdata[p]==0xFF && jpegdata[p+1]==M_SOI) break;
214 p++;
215 }
216
217 jpeg_padded_len = p;
218 return 0;
219 }
220
221 /* The query routines about the format */
222
lav_query_APP_marker(char format)223 int lav_query_APP_marker(char format)
224 {
225 /* AVI needs the APP0 marker, Quicktime APP1 */
226
227 switch(format)
228 {
229 case 'a': return 0;
230 case 'A': return 0;
231 case 'j': return 0;
232 case 'q': return 1;
233 default: return 0;
234 }
235 }
236
lav_query_APP_length(char format)237 int lav_query_APP_length(char format)
238 {
239 /* AVI: APP0 14 bytes, Quicktime APP1: 40 */
240
241 switch(format)
242 {
243 case 'a': return 14;
244 case 'A': return 14;
245 case 'j': return 14;
246 case 'q': return 40;
247 default: return 0;
248 }
249 }
250
lav_query_polarity(char format)251 int lav_query_polarity(char format)
252 {
253 /* Quicktime needs TOP_FIRST, for AVI we have the choice */
254
255 switch(format)
256 {
257 case 'a': return Y4M_ILACE_TOP_FIRST;
258 case 'A': return Y4M_ILACE_BOTTOM_FIRST;
259 case 'j': return Y4M_ILACE_TOP_FIRST;
260 case 'q': return Y4M_ILACE_TOP_FIRST;
261 default: return Y4M_ILACE_TOP_FIRST;
262 }
263 }
264
lav_open_output_file(char * filename,char format,int width,int height,int interlaced,double fps,int asize,int achans,long arate)265 lav_file_t *lav_open_output_file(char *filename, char format,
266 int width, int height, int interlaced, double fps,
267 int asize, int achans, long arate)
268 {
269 lav_file_t *lav_fd = (lav_file_t*) malloc(sizeof(lav_file_t));
270 char *tempfile;
271
272 if (lav_fd == 0) { internal_error=ERROR_MALLOC; return 0; }
273
274 lav_fd->avi_fd = 0;
275 lav_fd->qt_fd = 0;
276 lav_fd->format = format;
277 lav_fd->interlacing = interlaced ? lav_query_polarity(format) :
278 Y4M_ILACE_NONE;
279 lav_fd->has_audio = (asize>0 && achans>0);
280 lav_fd->bps = (asize*achans+7)/8;
281 lav_fd->chroma = Y4M_UNKNOWN;
282
283 switch(format)
284 {
285 case 'a':
286 case 'A':
287 /* Open AVI output file */
288
289 lav_fd->avi_fd = AVI_open_output_file(filename);
290 if(!lav_fd->avi_fd) { free(lav_fd); return 0; }
291 AVI_set_video(lav_fd->avi_fd, width, height, fps, "MJPG");
292 if (asize) AVI_set_audio(lav_fd->avi_fd, achans, arate, asize, WAVE_FORMAT_PCM, 0);
293 return lav_fd;
294
295 case 'j':
296 /* Open JPEG output file */
297 tempfile = (char *)malloc(strlen(filename) + strlen(TMP_EXTENSION) + 1);
298 if (tempfile == NULL)
299 {
300 internal_error=ERROR_MALLOC;
301 return(0);
302 }
303 strcpy(tempfile, filename);
304 strcat(tempfile, TMP_EXTENSION);
305 lav_fd->jpeg_filename = strdup(filename);
306 lav_fd->jpeg_fd = open(tempfile, O_CREAT | O_TRUNC | O_WRONLY, 0644);
307 free(tempfile);
308 return lav_fd;
309
310 case 'q':
311 #ifdef HAVE_LIBQUICKTIME
312 /* open quicktime output file */
313
314 /* since the documentation says that the file should be empty,
315 we try to remove it first */
316
317 remove(filename);
318
319 lav_fd->qt_fd = quicktime_open(filename, 0, 1);
320 if(!lav_fd->qt_fd) { free(lav_fd); return 0; }
321 quicktime_set_video(lav_fd->qt_fd, 1, width, height, fps,
322 (interlaced ? QUICKTIME_MJPA : QUICKTIME_JPEG));
323 if (interlaced)
324 {
325 if (lav_fd->interlacing == Y4M_ILACE_TOP_FIRST)
326 lqt_set_fiel(lav_fd->qt_fd, 0, 2, 9);
327 else if (lav_fd->interlacing == Y4M_ILACE_BOTTOM_FIRST)
328 lqt_set_fiel(lav_fd->qt_fd, 0, 2, 14);
329 }
330 if (asize)
331 quicktime_set_audio(lav_fd->qt_fd, achans, arate, asize, QUICKTIME_TWOS);
332 return lav_fd;
333 #else
334 internal_error = ERROR_FORMAT;
335 return 0;
336 #endif
337 default:
338 return 0;
339 }
340 }
341
lav_close(lav_file_t * lav_file)342 int lav_close(lav_file_t *lav_file)
343 {
344 int res;
345 char *tempfile;
346
347 video_format = lav_file->format; internal_error = 0; /* for error messages */
348
349 switch (lav_file->format)
350 {
351 case 'a':
352 case 'A':
353 res = AVI_close( lav_file->avi_fd );
354 break;
355 case 'j':
356 tempfile = (char *)malloc(strlen(lav_file->jpeg_filename) +
357 strlen(TMP_EXTENSION) + 1);
358 if (tempfile == NULL)
359 {
360 res = -1;
361 break;
362 }
363 strcpy(tempfile, lav_file->jpeg_filename);
364 strcat(tempfile, TMP_EXTENSION);
365 res = close(lav_file->jpeg_fd);
366 rename(tempfile, lav_file->jpeg_filename);
367 free(tempfile);
368 free(lav_file->jpeg_filename);
369 break;
370 #ifdef HAVE_LIBQUICKTIME
371 case 'q':
372 res = quicktime_close( lav_file->qt_fd );
373 break;
374 #endif
375 default:
376 res = -1;
377 }
378 free(lav_file);
379 return res;
380 }
381
lav_write_frame(lav_file_t * lav_file,uint8_t * buff,long size,long count)382 int lav_write_frame(lav_file_t *lav_file, uint8_t *buff, long size, long count)
383 {
384 int res, n;
385 uint8_t *jpgdata = NULL;
386 long jpglen = 0;
387
388 video_format = lav_file->format; internal_error = 0; /* for error messages */
389
390 /* For interlaced video insert the apropriate APPn markers */
391
392 if(lav_file->interlacing != Y4M_ILACE_NONE)
393 {
394 switch(lav_file->format)
395 {
396 case 'a':
397 case 'A':
398
399 jpgdata = buff;
400 jpglen = size;
401
402 /* Loop over both fields */
403
404 for(n=0;n<2;n++)
405 {
406 /* For first field scan entire field, for second field
407 scan the JPEG header, put in AVI1 + polarity.
408 Be generous on errors */
409
410 res = scan_jpeg(jpgdata, size, n);
411 if (res) { internal_error=ERROR_JPEG; return -1; }
412
413 if(!jpeg_app0_offset) continue;
414
415 /* APP0 marker should be at least 14+2 bytes */
416 if(get_int2(jpgdata+jpeg_app0_offset+2) < 16 ) continue;
417
418 jpgdata[jpeg_app0_offset+4] = 'A';
419 jpgdata[jpeg_app0_offset+5] = 'V';
420 jpgdata[jpeg_app0_offset+6] = 'I';
421 jpgdata[jpeg_app0_offset+7] = '1';
422 jpgdata[jpeg_app0_offset+8] = lav_file->format=='a' ? n+1 : 2-n;
423
424 /* Update pointer and len for second field */
425 jpgdata += jpeg_padded_len;
426 jpglen -= jpeg_padded_len;
427 }
428 break;
429
430 case 'j':
431
432 jpgdata = buff;
433 jpglen = size;
434 break;
435
436 #ifdef HAVE_LIBQUICKTIME
437 case 'q':
438
439 jpgdata = buff;
440 jpglen = size;
441
442 /* Loop over both fields */
443
444 for(n=0;n<2;n++)
445 {
446 /* Scan the entire JPEG field data - APP1 marker MUST be present */
447 res = scan_jpeg(jpgdata,jpglen,0);
448 if(res || !jpeg_app1_offset) { internal_error=ERROR_JPEG; return -1; }
449
450 /* Length of APP1 marker must be at least 40 + 2 bytes */
451 if ( get_int2(jpgdata+jpeg_app1_offset+2) < 42)
452 { internal_error=ERROR_JPEG; return -1; }
453
454 /* Fill in data */
455 put_int4(jpgdata+jpeg_app1_offset+ 4,0);
456 put_int4(jpgdata+jpeg_app1_offset+ 8,QUICKTIME_MJPG_TAG);
457 put_int4(jpgdata+jpeg_app1_offset+12,jpeg_field_size);
458 put_int4(jpgdata+jpeg_app1_offset+16,jpeg_padded_len);
459 put_int4(jpgdata+jpeg_app1_offset+20,n==0?jpeg_padded_len:0);
460 put_int4(jpgdata+jpeg_app1_offset+24,jpeg_quant_offset);
461 put_int4(jpgdata+jpeg_app1_offset+28,jpeg_huffman_offset);
462 put_int4(jpgdata+jpeg_app1_offset+32,jpeg_image_offset);
463 put_int4(jpgdata+jpeg_app1_offset+36,jpeg_scan_offset);
464 put_int4(jpgdata+jpeg_app1_offset+40,jpeg_data_offset);
465
466 /* Update pointer and len for second field */
467 jpgdata += jpeg_padded_len;
468 jpglen -= jpeg_padded_len;
469 }
470 break;
471 #endif
472 }
473 }
474
475 res = 0; /* Silence gcc */
476 for(n=0;n<count;n++)
477 {
478 switch(lav_file->format)
479 {
480 case 'a':
481 case 'A':
482 if(n==0)
483 res = AVI_write_frame( lav_file->avi_fd, buff, size, 0);
484 else
485 res = AVI_dup_frame( lav_file->avi_fd );
486 break;
487 case 'j':
488 if (n==0)
489 write(lav_file->jpeg_fd, buff, size);
490 break;
491 #ifdef HAVE_LIBQUICKTIME
492 case 'q':
493 res = quicktime_write_frame( lav_file->qt_fd, buff, size, 0 );
494 break;
495 #endif
496 default:
497 res = -1;
498 }
499 if (res) break;
500 }
501 return res;
502 }
503
lav_write_audio(lav_file_t * lav_file,uint8_t * buff,long samps)504 int lav_write_audio(lav_file_t *lav_file, uint8_t *buff, long samps)
505 {
506 int res = -1;
507 #ifdef HAVE_LIBQUICKTIME
508 int i, j;
509 int16_t *buff16 = (int16_t *)buff, **qt_audion;
510 int channels = lav_audio_channels(lav_file);
511 int bits = lav_audio_bits(lav_file);
512 #endif
513
514 video_format = lav_file->format; internal_error = 0; /* for error messages */
515
516 switch(lav_file->format)
517 {
518 case 'a':
519 case 'A':
520 res = AVI_write_audio( lav_file->avi_fd, buff, samps*lav_file->bps);
521 break;
522 #ifdef HAVE_LIBQUICKTIME
523 case 'q':
524 if (bits != 16 || channels > 1)
525 {
526 /* Deinterleave the audio into the two channels and/or convert
527 * bits per sample to the required format.
528 */
529 qt_audion = malloc(channels * sizeof(*qt_audion));
530 for (i = 0; i < channels; i++)
531 qt_audion[i] = malloc(samps * sizeof(**qt_audion));
532
533 if (bits == 16)
534 for (i = 0; i < samps; i++)
535 for (j = 0; j < channels; j++)
536 qt_audion[j][i] = buff16[channels * i + j];
537 else if (bits == 8)
538 for (i = 0; i < samps; i++)
539 for (j = 0; j < channels; j++)
540 qt_audion[j][i] = ((int16_t)(buff[channels * i + j]) << 8) ^ 0x8000;
541
542 if (bits == 8 || bits == 16)
543 res = lqt_encode_audio_track(lav_file->qt_fd, qt_audion, NULL, samps, 0);
544
545 for (i = 0; i < channels; i++)
546 free(qt_audion[i]);
547 free(qt_audion);
548 }
549 else
550 {
551 qt_audion = &buff16;
552 res = lqt_encode_audio_track(lav_file->qt_fd, qt_audion, NULL, samps, 0);
553 }
554 break;
555 #endif
556 default:
557 break;
558 }
559
560 return res;
561 }
562
lav_video_frames(lav_file_t * lav_file)563 long lav_video_frames(lav_file_t *lav_file)
564 {
565 video_format = lav_file->format; internal_error = 0; /* for error messages */
566 switch(lav_file->format)
567 {
568 case 'a':
569 case 'A':
570 return AVI_video_frames(lav_file->avi_fd);
571 #ifdef HAVE_LIBQUICKTIME
572 case 'q':
573 return quicktime_video_length(lav_file->qt_fd,0);
574 #endif
575 }
576 return -1;
577 }
578
lav_video_width(lav_file_t * lav_file)579 int lav_video_width(lav_file_t *lav_file)
580 {
581 video_format = lav_file->format; internal_error = 0; /* for error messages */
582 switch(lav_file->format)
583 {
584 case 'a':
585 case 'A':
586 return AVI_video_width(lav_file->avi_fd);
587 #ifdef HAVE_LIBQUICKTIME
588 case 'q':
589 return quicktime_video_width(lav_file->qt_fd,0);
590 #endif
591 }
592 return -1;
593 }
594
lav_video_height(lav_file_t * lav_file)595 int lav_video_height(lav_file_t *lav_file)
596 {
597 video_format = lav_file->format; internal_error = 0; /* for error messages */
598 switch(lav_file->format)
599 {
600 case 'a':
601 case 'A':
602 return AVI_video_height(lav_file->avi_fd);
603 #ifdef HAVE_LIBQUICKTIME
604 case 'q':
605 return quicktime_video_height(lav_file->qt_fd,0);
606 #endif
607 }
608 return -1;
609 }
610
lav_frame_rate(lav_file_t * lav_file)611 double lav_frame_rate(lav_file_t *lav_file)
612 {
613 video_format = lav_file->format; internal_error = 0; /* for error messages */
614 switch(lav_file->format)
615 {
616 case 'a':
617 case 'A':
618 return AVI_frame_rate(lav_file->avi_fd);
619 #ifdef HAVE_LIBQUICKTIME
620 case 'q':
621 return quicktime_frame_rate(lav_file->qt_fd,0);
622 #endif
623 }
624 return -1;
625 }
626
lav_video_interlacing(lav_file_t * lav_file)627 int lav_video_interlacing(lav_file_t *lav_file)
628 {
629 return lav_file->interlacing;
630 }
631
lav_video_sampleaspect(lav_file_t * lav_file,int * sar_w,int * sar_h)632 void lav_video_sampleaspect(lav_file_t *lav_file, int *sar_w, int *sar_h)
633 {
634 *sar_w = lav_file->sar_w;
635 *sar_h = lav_file->sar_h;
636 return;
637 }
638
lav_video_chroma(lav_file_t * lav_file)639 int lav_video_chroma(lav_file_t *lav_file)
640 {
641 return lav_file->chroma;
642 }
643
lav_video_compressor(lav_file_t * lav_file)644 const char *lav_video_compressor(lav_file_t *lav_file)
645 {
646 video_format = lav_file->format; internal_error = 0; /* for error messages */
647 switch(lav_file->format)
648 {
649 case 'a':
650 case 'A':
651 return AVI_video_compressor(lav_file->avi_fd);
652 #ifdef HAVE_LIBQUICKTIME
653 case 'q':
654 return quicktime_video_compressor(lav_file->qt_fd,0);
655 #endif
656 }
657 return "N/A";
658 }
659
lav_audio_channels(lav_file_t * lav_file)660 int lav_audio_channels(lav_file_t *lav_file)
661 {
662 if(!lav_file->has_audio) return 0;
663 video_format = lav_file->format; internal_error = 0; /* for error messages */
664 switch(lav_file->format)
665 {
666 case 'a':
667 case 'A':
668 return AVI_audio_channels(lav_file->avi_fd);
669 #ifdef HAVE_LIBQUICKTIME
670 case 'q':
671 return quicktime_track_channels(lav_file->qt_fd,0);
672 #endif
673 }
674 return -1;
675 }
676
lav_audio_bits(lav_file_t * lav_file)677 int lav_audio_bits(lav_file_t *lav_file)
678 {
679 if(!lav_file->has_audio) return 0;
680 video_format = lav_file->format; internal_error = 0; /* for error messages */
681 switch(lav_file->format)
682 {
683 case 'a':
684 case 'A':
685 return AVI_audio_bits(lav_file->avi_fd);
686 #ifdef HAVE_LIBQUICKTIME
687 case 'q':
688 return quicktime_audio_bits(lav_file->qt_fd,0);
689 #endif
690 }
691 return -1;
692 }
693
lav_audio_rate(lav_file_t * lav_file)694 long lav_audio_rate(lav_file_t *lav_file)
695 {
696 if(!lav_file->has_audio) return 0;
697 video_format = lav_file->format; internal_error = 0; /* for error messages */
698 switch(lav_file->format)
699 {
700 case 'a':
701 case 'A':
702 return AVI_audio_rate(lav_file->avi_fd);
703 #ifdef HAVE_LIBQUICKTIME
704 case 'q':
705 return quicktime_sample_rate(lav_file->qt_fd,0);
706 #endif
707 }
708 return -1;
709 }
710
lav_audio_samples(lav_file_t * lav_file)711 long lav_audio_samples(lav_file_t *lav_file)
712 {
713 if(!lav_file->has_audio) return 0;
714 video_format = lav_file->format; internal_error = 0; /* for error messages */
715 switch(lav_file->format)
716 {
717 case 'a':
718 case 'A':
719 return AVI_audio_bytes(lav_file->avi_fd)/lav_file->bps;
720 #ifdef HAVE_LIBQUICKTIME
721 case 'q':
722 return quicktime_audio_length(lav_file->qt_fd,0);
723 #endif
724 }
725 return -1;
726 }
727
lav_frame_size(lav_file_t * lav_file,long frame)728 long lav_frame_size(lav_file_t *lav_file, long frame)
729 {
730 video_format = lav_file->format; internal_error = 0; /* for error messages */
731 switch(lav_file->format)
732 {
733 case 'a':
734 case 'A':
735 return AVI_frame_size(lav_file->avi_fd,frame);
736 #ifdef HAVE_LIBQUICKTIME
737 case 'q':
738 return quicktime_frame_size(lav_file->qt_fd,frame,0);
739 #endif
740 }
741 return -1;
742 }
743
lav_seek_start(lav_file_t * lav_file)744 int lav_seek_start(lav_file_t *lav_file)
745 {
746 video_format = lav_file->format; internal_error = 0; /* for error messages */
747 switch(lav_file->format)
748 {
749 case 'a':
750 case 'A':
751 return AVI_seek_start(lav_file->avi_fd);
752 #ifdef HAVE_LIBQUICKTIME
753 case 'q':
754 return quicktime_seek_start(lav_file->qt_fd);
755 #endif
756 }
757 return -1;
758 }
759
lav_set_video_position(lav_file_t * lav_file,long frame)760 int lav_set_video_position(lav_file_t *lav_file, long frame)
761 {
762 video_format = lav_file->format; internal_error = 0; /* for error messages */
763 switch(lav_file->format)
764 {
765 case 'a':
766 case 'A':
767 return AVI_set_video_position(lav_file->avi_fd,frame);
768 #ifdef HAVE_LIBQUICKTIME
769 case 'q':
770 return quicktime_set_video_position(lav_file->qt_fd,frame,0);
771 #endif
772 }
773 return -1;
774 }
775
lav_read_frame(lav_file_t * lav_file,uint8_t * vidbuf)776 int lav_read_frame(lav_file_t *lav_file, uint8_t *vidbuf)
777 {
778 int keyframe;
779
780 video_format = lav_file->format; internal_error = 0; /* for error messages */
781 switch(lav_file->format)
782 {
783 case 'a':
784 case 'A':
785 return AVI_read_frame(lav_file->avi_fd,vidbuf, &keyframe);
786 #ifdef HAVE_LIBQUICKTIME
787 case 'q':
788 return quicktime_read_frame(lav_file->qt_fd,vidbuf,0);
789 #endif
790 }
791 return -1;
792 }
793
lav_set_audio_position(lav_file_t * lav_file,long sample)794 int lav_set_audio_position(lav_file_t *lav_file, long sample)
795 {
796 if(!lav_file->has_audio) return 0;
797 video_format = lav_file->format; internal_error = 0; /* for error messages */
798 switch(lav_file->format)
799 {
800 case 'a':
801 case 'A':
802 return AVI_set_audio_position(lav_file->avi_fd,sample*lav_file->bps);
803 #ifdef HAVE_LIBQUICKTIME
804 case 'q':
805 quicktime_set_audio_position(lav_file->qt_fd,sample,0);
806 #endif
807 }
808 return -1;
809 }
810
lav_read_audio(lav_file_t * lav_file,uint8_t * audbuf,long samps)811 long lav_read_audio(lav_file_t *lav_file, uint8_t *audbuf, long samps)
812 {
813 #ifdef HAVE_LIBQUICKTIME
814 int64_t last_pos, start_pos;
815 int res, i, j;
816 int16_t *qt_audio = (int16_t *)audbuf, **qt_audion;
817 int channels = lav_audio_channels(lav_file);
818 uint8_t b0, b1;
819 #endif
820
821 if(!lav_file->has_audio)
822 {
823 internal_error = ERROR_NOAUDIO;
824 return(-1);
825 }
826 video_format = lav_file->format; internal_error = 0; /* for error messages */
827 switch(lav_file->format)
828 {
829 case 'a':
830 case 'A':
831 return AVI_read_audio(lav_file->avi_fd,audbuf,samps*lav_file->bps)/lav_file->bps;
832 #ifdef HAVE_LIBQUICKTIME
833 case 'q':
834 qt_audion = malloc(channels * sizeof (int16_t **));
835 for (i = 0; i < channels; i++)
836 qt_audion[i] = (int16_t *)malloc(samps * lav_file->bps);
837
838 start_pos = quicktime_audio_position(lav_file->qt_fd, 0);
839 lqt_decode_audio_track(lav_file->qt_fd, qt_audion, NULL, samps, 0);
840 last_pos = lqt_last_audio_position(lav_file->qt_fd, 0);
841 res = last_pos - start_pos;
842 if (res <= 0)
843 goto out;
844 /* Interleave the channels of audio into the one buffer provided */
845 for (i =0; i < res; i++)
846 {
847 for (j = 0; j < channels; j++)
848 qt_audio[(channels*i) + j] = qt_audion[j][i];
849 }
850
851 if (lav_detect_endian())
852 {
853 i= 0;
854 while (i < (2*res) )
855 {
856 b0 = 0;
857 b1 = 0;
858 b0 = (qt_audio[i] & 0x00FF);
859 b1 = (qt_audio[i] & 0xFF00) >> 8;
860 qt_audio[i] = (b0 <<8) + b1;
861 i = i +1;
862 }
863 }
864 out:
865 for (j = 0; j < channels; j++)
866 free(qt_audion[j]);
867 free(qt_audion);
868 return(res);
869 #endif
870 }
871 return -1;
872 }
873
lav_open_input_file(char * filename)874 lav_file_t *lav_open_input_file(char *filename)
875 {
876 int n;
877 const char *video_comp = NULL;
878 #ifdef HAVE_LIBQUICKTIME
879 char *audio_comp;
880 #endif
881 unsigned char *frame = NULL; /* Make sure un-init segfaults! */
882 long len;
883 int jpg_height, jpg_width, ncomps, hf[3], vf[3];
884 int ierr;
885
886 lav_file_t *lav_fd = (lav_file_t*) malloc(sizeof(lav_file_t));
887
888 if(lav_fd==0) { internal_error=ERROR_MALLOC; return 0; }
889
890 /* Set lav_fd */
891
892 lav_fd->avi_fd = 0;
893 lav_fd->qt_fd = 0;
894 lav_fd->format = 0;
895 lav_fd->interlacing = Y4M_UNKNOWN;
896 lav_fd->sar_w = 1; /* unknown - assume square pixels */
897 lav_fd->sar_h = 1;
898 lav_fd->has_audio = 0;
899 lav_fd->bps = 0;
900 lav_fd->chroma = Y4M_UNKNOWN;
901
902 /* open video file, try AVI first */
903
904 lav_fd->avi_fd = AVI_open_input_file(filename,1);
905 video_format = 'a'; /* for error messages */
906
907 if(lav_fd->avi_fd)
908 {
909 /* It is an AVI file */
910 lav_fd->qt_fd = 0;
911 lav_fd->format = 'a';
912 lav_fd->has_audio = (AVI_audio_bits(lav_fd->avi_fd)>0 &&
913 AVI_audio_format(lav_fd->avi_fd)==WAVE_FORMAT_PCM);
914 video_comp = AVI_video_compressor(lav_fd->avi_fd);
915 }
916 else if( AVI_errno==AVI_ERR_NO_AVI )
917 {
918 #ifdef HAVE_LIBQUICKTIME
919 if(!quicktime_check_sig(filename))
920 #endif
921 {
922 /* None of the known formats */
923 char errmsg[1024];
924 sprintf(errmsg, "Unable to identify file (not a supported format - avi");
925 #ifdef HAVE_LIBQUICKTIME
926 strcat(errmsg, ", quicktime");
927 #endif
928 strcat(errmsg, ").\n");
929 fprintf(stderr, errmsg);
930 free(lav_fd);
931 internal_error = ERROR_FORMAT; /* Format not recognized */
932 return 0;
933 }
934 #ifdef HAVE_LIBQUICKTIME
935 else
936 {
937 quicktime_pasp_t pasp;
938 int nfields, detail;
939
940 /* It is a quicktime file */
941 lav_fd->qt_fd = quicktime_open(filename,1,0);
942 video_format = 'q'; /* for error messages */
943 if (!lav_fd->qt_fd)
944 {
945 free(lav_fd);
946 return 0;
947 }
948 lav_fd->avi_fd = NULL;
949 lav_fd->format = 'q';
950 video_comp = quicktime_video_compressor(lav_fd->qt_fd,0);
951 /* We want at least one video track */
952 if (quicktime_video_tracks(lav_fd->qt_fd) < 1)
953 {
954 lav_close(lav_fd);
955 internal_error = ERROR_FORMAT;
956 return 0;
957 }
958 /*
959 * If the quicktime file has the sample aspect atom then use it to set
960 * the sar values in the lav_fd structure. Hardwired (like everywhere else)
961 * to only look at track 0.
962 */
963 if (lqt_get_pasp(lav_fd->qt_fd, 0, &pasp) != 0)
964 {
965 lav_fd->sar_w = pasp.hSpacing;
966 lav_fd->sar_h = pasp.vSpacing;
967 }
968 /*
969 * If a 'fiel' atom is present (not guaranteed) then use it to set the
970 * interlacing type.
971 */
972 if (lqt_get_fiel(lav_fd->qt_fd, 0, &nfields, &detail) != 0)
973 {
974 if (nfields == 2)
975 {
976 if (detail == 14 || detail == 6)
977 lav_fd->interlacing = Y4M_ILACE_BOTTOM_FIRST;
978 else if (detail == 9 || detail == 1)
979 lav_fd->interlacing = Y4M_ILACE_TOP_FIRST;
980 else
981 mjpeg_warn("unknown 'detail' in 'fiel' atom: %d", detail);
982 }
983 else
984 lav_fd->interlacing = Y4M_ILACE_NONE;
985 }
986 /* Check for audio tracks */
987 lav_fd->has_audio = 0;
988 if (quicktime_audio_tracks(lav_fd->qt_fd))
989 {
990 audio_comp = quicktime_audio_compressor(lav_fd->qt_fd,0);
991 if (strncasecmp(audio_comp, QUICKTIME_TWOS,4)==0)
992 lav_fd->has_audio = 1;
993 }
994 }
995 #endif
996 }
997 else
998 {
999 /* There should be an error from avilib, just return */
1000 free(lav_fd);
1001 return 0;
1002 }
1003
1004 /* set audio bytes per sample */
1005
1006 lav_fd->bps = (lav_audio_channels(lav_fd)*lav_audio_bits(lav_fd)+7)/8;
1007 if(lav_fd->bps==0) lav_fd->bps=1; /* make it save since we will divide by that value */
1008
1009 /*
1010 * Check compressor. The YUV checks are not right (the support code appears
1011 * incorrect and/or incomplete). In particular yuv2 is a packed format not
1012 * planar and YV12 has the U and V planes reversed from IYUV (which is what
1013 * the support code appears to think is YUV420). My hunch is that folks are
1014 * only using DV and MJPG so the YUV bugs aren't being triggered.
1015 *
1016 * But at least now the checks are consolidated in 1 place instead of being
1017 * duplicated in two places (editlist.c and this file).
1018 */
1019
1020 ierr = 0;
1021
1022 if (strncasecmp(video_comp, "yv12", 3) == 0)
1023 {
1024 lav_fd->dataformat = DATAFORMAT_YUV420;
1025 /*
1026 * This is probably not correct. But since 'yv12' isn't really supported it
1027 * doesn't matter ;)
1028 */
1029 lav_fd->chroma = Y4M_CHROMA_420JPEG;
1030 }
1031 else if (strncasecmp(video_comp, "yuv2", 4) == 0)
1032 {
1033 lav_fd->dataformat = DATAFORMAT_YUV422;
1034 lav_fd->chroma = Y4M_CHROMA_422;
1035 }
1036 else if (strncasecmp(video_comp, "dv", 2) == 0)
1037 {
1038 lav_fd->dataformat = DATAFORMAT_DV2;
1039 lav_fd->interlacing = Y4M_ILACE_BOTTOM_FIRST;
1040 }
1041 else if (strncasecmp(video_comp, "mjp", 3) == 0 ||
1042 strncasecmp(video_comp, "jpeg", 4) == 0)
1043 lav_fd->dataformat = DATAFORMAT_MJPG;
1044 else
1045 {
1046 internal_error = ERROR_FORMAT;
1047 goto ERREXIT;
1048 }
1049
1050 #ifdef HAVE_LIBDV
1051 if (lav_fd->dataformat == DATAFORMAT_DV2)
1052 {
1053 ierr = check_DV2_input(lav_fd);
1054 if (ierr)
1055 goto ERREXIT;
1056 }
1057 #endif /* HAVE_LIBDV */
1058
1059 if (lav_fd->dataformat != DATAFORMAT_MJPG)
1060 return lav_fd;
1061
1062 /*
1063 * From here on down is MJPG only code - the yuv and dv cases have been handled
1064 * above. Make some checks on the video source. Read the first frame for this.
1065 */
1066
1067 frame = NULL;
1068 if (lav_set_video_position(lav_fd,0))
1069 goto ERREXIT;
1070 if ((len = lav_frame_size(lav_fd,0)) <=0)
1071 goto ERREXIT;
1072 if ((frame = (unsigned char*) malloc(len)) == 0)
1073 {
1074 ierr=ERROR_MALLOC;
1075 goto ERREXIT;
1076 }
1077 if (lav_read_frame(lav_fd,frame) <= 0)
1078 goto ERREXIT;
1079 /* reset video position to 0 */
1080 if (lav_set_video_position(lav_fd,0))
1081 goto ERREXIT;
1082 if (scan_jpeg(frame, len, 1))
1083 {
1084 ierr=ERROR_JPEG;
1085 goto ERREXIT;
1086 }
1087
1088 /* We have to look to the JPEG SOF marker for further information
1089 The SOF marker has the following format:
1090
1091 FF
1092 C0
1093 len_hi
1094 len_lo
1095 data_precision
1096 height_hi
1097 height_lo
1098 width_hi
1099 width_lo
1100 num_components
1101
1102 And then 3 bytes for each component:
1103
1104 Component id
1105 H, V sampling factors (as nibbles)
1106 Quantization table number
1107 */
1108
1109 /* Check if the JPEG has the special 4:2:2 format needed for
1110 some HW JPEG decompressors (the Iomega Buz, for example) */
1111
1112 ncomps = frame[jpeg_image_offset + 9];
1113 if(ncomps==3)
1114 {
1115 for(n=0;n<3;n++)
1116 {
1117 hf[n] = frame[jpeg_image_offset + 10 + 3*n + 1]>>4;
1118 vf[n] = frame[jpeg_image_offset + 10 + 3*n + 1]&0xf;
1119 }
1120
1121 /* Identify chroma sub-sampling format only 420 and 422 supported
1122 at present...*/
1123 if( hf[0] == 2*hf[1] && hf[0] == 2*hf[2] )
1124 {
1125 if( vf[0] == vf[1] && vf[0] == vf[2] )
1126 {
1127 lav_fd->chroma = Y4M_CHROMA_422;
1128 }
1129 else if( vf[0] == 2*vf[1] && vf[0] == 2*vf[2] )
1130 lav_fd->chroma = Y4M_CHROMA_420JPEG;
1131 else
1132 lav_fd->chroma = Y4M_UNKNOWN;
1133 }
1134 else
1135 lav_fd->chroma = Y4M_UNKNOWN;
1136 }
1137
1138 /* Check if video is interlaced */
1139 /* height and width are encoded in the JPEG SOF marker at offsets 5 and 7 */
1140
1141 jpg_height = get_int2(frame + jpeg_image_offset + 5);
1142 jpg_width = get_int2(frame + jpeg_image_offset + 7);
1143
1144 /* check height */
1145
1146 if( jpg_height == lav_video_height(lav_fd))
1147 lav_fd->interlacing = Y4M_ILACE_NONE;
1148 else if ( jpg_height == lav_video_height(lav_fd)/2 )
1149 {
1150 /* Video is interlaced */
1151
1152 switch(lav_fd->format)
1153 {
1154 case 'a':
1155 /* Check the APP0 Marker, if present */
1156
1157 if(jpeg_app0_offset &&
1158 get_int2(frame + jpeg_app0_offset + 2) >= 5 &&
1159 strncasecmp((char*)(frame + jpeg_app0_offset + 4),"AVI1",4)==0 )
1160 {
1161 if (frame[jpeg_app0_offset+8]==1)
1162 lav_fd->interlacing = Y4M_ILACE_TOP_FIRST;
1163 else
1164 lav_fd->interlacing = Y4M_ILACE_BOTTOM_FIRST;
1165 }
1166 else
1167 {
1168 /* There is no default, it really depends on the
1169 application which produced the AVI */
1170 lav_fd->interlacing = Y4M_ILACE_TOP_FIRST;
1171 }
1172 lav_fd->format = lav_fd->interlacing == Y4M_ILACE_BOTTOM_FIRST ? 'A' : 'a';
1173 break;
1174 case 'q':
1175 lav_fd->interlacing = Y4M_ILACE_TOP_FIRST;
1176 break;
1177 }
1178 }
1179 else
1180 {
1181 ierr=ERROR_JPEG;
1182 goto ERREXIT;
1183 }
1184
1185 free(frame);
1186 return lav_fd;
1187
1188 ERREXIT:
1189 lav_close(lav_fd);
1190 if(frame) free(frame);
1191 internal_error = ierr;
1192 return 0;
1193 }
1194
1195 /* Get size of first field of for a data array containing
1196 (possibly) two jpeg fields */
1197
lav_get_field_size(uint8_t * jpegdata,long jpeglen)1198 int lav_get_field_size(uint8_t * jpegdata, long jpeglen)
1199 {
1200 int res;
1201
1202 res = scan_jpeg(jpegdata,jpeglen,0);
1203 if(res<0) return jpeglen; /* Better than nothing */
1204
1205 /* we return jpeg_padded len since this routine is used
1206 for field exchange where alignment might be important */
1207
1208 return jpeg_padded_len;
1209 }
1210
1211 static char error_string[4096];
1212
lav_strerror(void)1213 const char *lav_strerror(void)
1214 {
1215
1216 switch(internal_error)
1217 {
1218 case ERROR_JPEG:
1219 sprintf(error_string,"Internal: broken JPEG format");
1220 internal_error = 0;
1221 return error_string;
1222 case ERROR_MALLOC:
1223 sprintf(error_string,"Internal: Out of memory");
1224 internal_error = 0;
1225 return error_string;
1226 case ERROR_FORMAT:
1227 sprintf(error_string,"Input file format not recognized");
1228 internal_error = 0;
1229 return error_string;
1230 case ERROR_NOAUDIO:
1231 sprintf(error_string,"Trying to read audio from a video only file");
1232 internal_error = 0;
1233 return error_string;
1234 }
1235
1236 switch(video_format)
1237 {
1238 case 'a':
1239 case 'A':
1240 return AVI_strerror();
1241 #ifdef HAVE_LIBQUICKTIME
1242 case 'q':
1243 /* The quicktime documentation doesn't say much about error codes,
1244 we hope that strerror may give some info */
1245 sprintf(error_string,"Quicktime error, possible(!) reason: %s",strerror(errno));
1246 return error_string;
1247 #endif
1248 default:
1249 /* No or unknown video format */
1250 if(errno) strerror(errno);
1251 else sprintf(error_string,"No or unknown video format");
1252 return error_string;
1253 }
1254 }
1255
1256 #ifdef HAVE_LIBDV
check_DV2_input(lav_file_t * lav_fd)1257 static int check_DV2_input(lav_file_t *lav_fd)
1258 {
1259 int ierr = 0;
1260 double len = 0;
1261 unsigned char *frame = NULL;
1262 dv_decoder_t *decoder = NULL;
1263 uint8_t *output[3] = { NULL, NULL, NULL };
1264 uint16_t pitches[3];
1265
1266 /* Make some checks on the video source, we read the first frame for that */
1267
1268 if (lav_set_video_position(lav_fd,0))
1269 goto ERREXIT;
1270 if ((len = lav_frame_size(lav_fd,0)) <=0)
1271 goto ERREXIT;
1272 if ((frame = (unsigned char*) malloc(len)) == 0)
1273 {
1274 ierr=ERROR_MALLOC;
1275 goto ERREXIT;
1276 }
1277
1278 if (lav_read_frame(lav_fd, frame) <= 0)
1279 goto ERREXIT;
1280
1281 decoder = dv_decoder_new(0,0,0);
1282 dv_parse_header(decoder, frame);
1283
1284 switch (decoder->system)
1285 {
1286 case e_dv_system_525_60:
1287 if (dv_format_wide(decoder))
1288 {
1289 lav_fd->sar_w = 40;
1290 lav_fd->sar_h = 33;
1291 }
1292 else {
1293 lav_fd->sar_w = 10;
1294 lav_fd->sar_h = 11;
1295 }
1296 break;
1297 case e_dv_system_625_50:
1298 if (dv_format_wide(decoder))
1299 {
1300 lav_fd->sar_w = 118;
1301 lav_fd->sar_h = 81;
1302 } else {
1303 lav_fd->sar_w = 59;
1304 lav_fd->sar_h = 54;
1305 }
1306 break;
1307 default:
1308 ierr = ERROR_FORMAT; /* Neither 525 or 625 system */
1309 goto ERREXIT; /* declare a format error! */
1310 }
1311
1312 switch(decoder->sampling) {
1313 case e_dv_sample_420:
1314 /* libdv decodes PAL DV directly as planar YUV 420
1315 * (YV12 or 4CC 0x32315659) if configured with the flag
1316 * --with-pal-yuv=YV12 which is not (!) the default
1317 */
1318 if (libdv_pal_yv12 < 0 ) {
1319 /* PAL YV12 not already detected.
1320 * Setting decoding options for 4:2:2, because libdv pitches
1321 * is *int* for YUY2 and *uint16_t* for YV12 (no comment...)
1322 * In YV12 mode, even if pitches is 0, the first line is filled.
1323 */
1324
1325 pitches[0] = decoder->width * 2;
1326 pitches[1] = 0;
1327 pitches[2] = 0;
1328
1329 /* Hacking libdv: set invalid values for chroma. If libdv
1330 * was compiled with PAL YV12, the values are overwritten,
1331 * otherwise (4:2:2 output) only dv_frame[0] is filled and
1332 * chroma planes remain untouched.
1333 */
1334 output[0] = (uint8_t *)malloc(decoder->width * decoder->height * 2);
1335 output[1] = (uint8_t *)malloc(decoder->width * decoder->height / 2);
1336 if (!output[0] || !output[1])
1337 {
1338 ierr=ERROR_MALLOC;
1339 goto ERREXIT;
1340 } else {
1341 output[2] = output[1] + (decoder->width * decoder->height / 4 );
1342 output[1][0] = 0;
1343 output[1][1] = 255;
1344 output[2][0] = 255;
1345 output[2][1] = 0;
1346 dv_decode_full_frame(decoder, frame, e_dv_color_yuv,
1347 output, (int *)pitches);
1348 if (output[1][0]==0 && output[1][1]==255 &&
1349 output[2][0]==255 && output[2][1]==0) {
1350 libdv_pal_yv12 = 0;
1351 lav_fd->chroma = Y4M_CHROMA_422;
1352 mjpeg_info("Detected libdv PAL output YUY2 (4:2:2)");
1353 } else {
1354 libdv_pal_yv12 = 1;
1355 lav_fd->chroma = Y4M_CHROMA_420PALDV;
1356 mjpeg_info("Detected libdv PAL output YV12 (4:2:0)");
1357 }
1358 free(output[0]);
1359 free(output[1]);
1360 }
1361 } else {
1362 if(libdv_pal_yv12 == 0)
1363 lav_fd->chroma = Y4M_CHROMA_422;
1364 else
1365 lav_fd->chroma = Y4M_CHROMA_420PALDV;
1366 }
1367 break;
1368 case e_dv_sample_411:
1369 /* libdv decodes NTSC DV (native 411) as packed YUV 422 (YUY2 or 4CC 0x32595559)
1370 * where the U and V information is repeated.
1371 */
1372 lav_fd->chroma = Y4M_CHROMA_411;
1373 break;
1374 case e_dv_sample_422:
1375 /* libdv decodes PAL DV (native 420) as packed YUV 422 (YUY2 or 4CC 0x32595559)
1376 * where the U and V information is repeated. This can be
1377 * transformed to planar 420 (YV12 or 4CC 0x32315659).
1378 */
1379 lav_fd->chroma = Y4M_CHROMA_422;
1380 break;
1381 case e_dv_sample_none:
1382 /* FIXME */
1383 break;
1384 }
1385 free(frame);
1386 dv_decoder_free(decoder);
1387 lav_set_video_position(lav_fd,0);
1388 return 0;
1389
1390 ERREXIT:
1391 lav_close(lav_fd);
1392 if (frame)
1393 free(frame);
1394 if (decoder)
1395 dv_decoder_free(decoder);
1396 if (output[0])
1397 free(output[0]);
1398 if (output[1])
1399 free(output[1]);
1400 if (ierr)
1401 internal_error = ierr;
1402 return 1;
1403 }
1404 #endif
1405
lav_fileno(lav_file_t * lav_file)1406 int lav_fileno(lav_file_t *lav_file)
1407 {
1408 int res;
1409
1410 video_format = lav_file->format;
1411
1412 switch(lav_file->format)
1413 {
1414 case 'a':
1415 case 'A':
1416 res = AVI_fileno(lav_file->avi_fd);
1417 break;
1418 #ifdef HAVE_LIBQUICKTIME
1419 case 'q':
1420 res = lqt_fileno((quicktime_t *)lav_file->qt_fd);
1421 break;
1422 #endif
1423 default:
1424 res = -1;
1425 }
1426 return res;
1427 }
1428
1429 /* We need this to reorder the 32 bit values for big endian systems */
reorder_32(uint32_t todo,int big_endian)1430 uint32_t reorder_32(uint32_t todo, int big_endian)
1431 {
1432 unsigned char b0, b1, b2, b3;
1433 unsigned long reversed;
1434
1435 if( big_endian )
1436 {
1437 b0 = (todo & 0x000000FF);
1438 b1 = (todo & 0x0000FF00) >> 8;
1439 b2 = (todo & 0x00FF0000) >> 16;
1440 b3 = (todo & 0xFF000000) >> 24;
1441
1442 reversed = (b0 << 24) + (b1 << 16) + (b2 << 8) +b3;
1443 return reversed;
1444 }
1445 return todo;
1446 }
1447
lav_detect_endian(void)1448 int lav_detect_endian (void)
1449 {
1450 unsigned int fred;
1451 char *pfred;
1452
1453 fred = 2 | (1 << (sizeof(int)*8-8));
1454 pfred = (char *)&fred;
1455
1456 if (*pfred == 1)
1457 return 1;
1458 else if(*pfred == 2)
1459 return 0;
1460 else
1461 return -1;
1462 }
1463