4
* This file contains all data types and functions related to video and audio
5
* codecs and file formats.
8
* Copyright (C) 2004-07 Karl, Frankfurt
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.
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.
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.
25
#ifndef DOXYGEN_SHOULD_SKIP_THIS
26
#define DEBUGFILE "codecs.c"
31
#endif // DOXYGEN_SHOULD_SKIP_THIS
46
#include <ffmpeg/avcodec.h>
47
#include <ffmpeg/avformat.h>
52
#include "xvidcap-intl.h"
55
* fps and fps_rage arrays for use in the codecs array
57
static const XVC_FpsRange one_to_hundred_range[] = {
64
#define len_one_to_hundred_range (sizeof(one_to_hundred_range) / \
67
static const XVC_FpsRange mpeg4_range[] = {
74
#define len_mpeg4_range (sizeof(mpeg4_range) / sizeof(XVC_FpsRange))
76
static const XVC_Fps mpeg1_fps[] = {
87
#define len_mpeg1_fps (sizeof(mpeg1_fps) / sizeof(XVC_Fps))
89
static const XVC_Fps dv_fps[] = {
94
#define len_dv_fps (sizeof(dv_fps) / sizeof(XVC_Fps))
96
/** \brief global array storing all available codecs */
97
const XVC_Codec xvc_codecs[NUMCODECS] = {
110
N_("Portable Graymap"),
113
one_to_hundred_range,
114
len_one_to_hundred_range,
119
N_("Portable Pixmap"),
122
one_to_hundred_range,
123
len_one_to_hundred_range,
128
N_("Portable Network Graphics"),
131
one_to_hundred_range,
132
len_one_to_hundred_range,
137
N_("Joint Picture Expert Group"),
140
one_to_hundred_range,
141
len_one_to_hundred_range,
158
mpeg4_range, /* this is actually MPEG4 ... dunno if
159
* this is the same here */
174
N_("Microsoft DIVX 2"),
177
mpeg4_range, /* this is actually MPEG4 ... dunno if
178
* this is the same here */
184
N_("Microsoft DIVX 3"),
187
mpeg4_range, /* this is actually MPEG4 ... dunno if
188
* this is the same here */
197
mpeg4_range, /* this is actually MPEG4 ... dunno if
198
* this is the same here */
220
#ifdef HAVE_LIBTHEORA
226
mpeg4_range, /* this is actually MPEG4 ... dunno if
227
* this is the same here */
231
#endif // HAVE_LIBTHEORA
234
N_("Soerensen VQ 1"),
237
mpeg4_range, /* this is actually MPEG4 ... dunno if
238
* this is the same here */
246
* \brief global array storing all available audio codecs
248
const XVC_AuCodec xvc_audio_codecs[NUMAUCODECS] = {
254
#ifdef HAVE_FFMPEG_AUDIO
259
#ifdef HAVE_LIBMP3LAME
264
#endif // HAVE_LIBMP3LAME
273
#endif // HAVE_FFMPEG_AUDIO
278
* arrays with extensions, allowed video and audio codecs for use in
279
* the global xvc_formats array
281
static const char *extension_xwd[] = { "xwd" };
283
#define len_extension_xwd (sizeof(extension_xwd) / sizeof(char*))
286
static const char *extension_pgm[] = { "pgm" };
288
#define len_extension_pgm (sizeof(extension_pgm) / sizeof(char*))
290
static const char *extension_ppm[] = { "ppm" };
292
#define len_extension_ppm (sizeof(extension_ppm) / sizeof(char*))
294
static const char *extension_png[] = { "png" };
296
#define len_extension_png (sizeof(extension_png) / sizeof(char*))
298
static const char *extension_jpg[] = { "jpg", "jpeg" };
300
#define len_extension_jpg (sizeof(extension_jpg) / sizeof(char*))
302
static const char *extension_avi[] = { "avi" };
304
#define len_extension_avi (sizeof(extension_avi) / sizeof(char*))
306
static const char *extension_mpg[] = { "mpeg", "mpg" };
308
#define len_extension_mpg (sizeof(extension_mpg) / sizeof(char*))
310
static const char *extension_asf[] = { "asf" };
312
#define len_extension_asf (sizeof(extension_asf) / sizeof(char*))
314
static const char *extension_flv[] = { "flv", "flv1" };
316
#define len_extension_flv (sizeof(extension_flv) / sizeof(char*))
318
static const char *extension_swf[] = { "swf" };
320
#define len_extension_swf (sizeof(extension_swf) / sizeof(char*))
322
static const char *extension_dv[] = { "dv" };
324
#define len_extension_dv (sizeof(extension_dv) / sizeof(char*))
326
static const char *extension_m1v[] = { "m1v", "vcd" };
328
#define len_extension_m1v (sizeof(extension_m1v) / sizeof(char*))
330
static const char *extension_m2v[] = { "m2v", "svcd" };
332
#define len_extension_m2v (sizeof(extension_m2v) / sizeof(char*))
334
static const char *extension_mov[] = { "mov", "qt" };
336
#define len_extension_mov (sizeof(extension_mov) / sizeof(char*))
338
static const XVC_CodecID allowed_vid_codecs_pgm[] = { CODEC_PGM };
340
#define len_allowed_vid_codecs_pgm (sizeof(allowed_vid_codecs_pgm) / \
343
static const XVC_CodecID allowed_vid_codecs_ppm[] = { CODEC_PPM };
345
#define len_allowed_vid_codecs_ppm (sizeof(allowed_vid_codecs_ppm) / \
348
static const XVC_CodecID allowed_vid_codecs_png[] = { CODEC_PNG };
350
#define len_allowed_vid_codecs_png (sizeof(allowed_vid_codecs_png) / \
353
static const XVC_CodecID allowed_vid_codecs_jpg[] = { CODEC_JPEG };
355
#define len_allowed_vid_codecs_jpg (sizeof(allowed_vid_codecs_jpg) / \
358
static const XVC_CodecID allowed_vid_codecs_avi[] = {
364
#ifdef HAVE_LIBTHEORA
366
#endif // HAVE_LIBTHEORA
370
#define len_allowed_vid_codecs_avi (sizeof(allowed_vid_codecs_avi) / \
373
static const XVC_CodecID allowed_vid_codecs_asf[] = { CODEC_MSDIV3 };
375
#define len_allowed_vid_codecs_asf (sizeof(allowed_vid_codecs_asf) / \
378
static const XVC_CodecID allowed_vid_codecs_flv[] = { CODEC_FLV };
380
#define len_allowed_vid_codecs_flv (sizeof(allowed_vid_codecs_flv) / \
383
static const XVC_CodecID allowed_vid_codecs_swf[] = { CODEC_FLV, CODEC_MJPEG };
385
#define len_allowed_vid_codecs_swf (sizeof(allowed_vid_codecs_swf) / \
388
static const XVC_CodecID allowed_vid_codecs_dv[] = { CODEC_DV, CODEC_MJPEG };
390
#define len_allowed_vid_codecs_dv (sizeof(allowed_vid_codecs_dv) / \
393
static const XVC_CodecID allowed_vid_codecs_mpeg1[] = { CODEC_MPEG1 };
395
#define len_allowed_vid_codecs_mpeg1 (sizeof(allowed_vid_codecs_mpeg1) / \
398
static const XVC_CodecID allowed_vid_codecs_mpeg2[] = { CODEC_MPEG2 };
400
#define len_allowed_vid_codecs_mpeg2 (sizeof(allowed_vid_codecs_mpeg2) / \
403
static const XVC_CodecID allowed_vid_codecs_mov[] = {
409
#define len_allowed_vid_codecs_mov (sizeof(allowed_vid_codecs_mov) / \
412
#ifdef HAVE_FFMPEG_AUDIO
413
static const XVC_AuCodecID allowed_au_codecs_avi[] = {
415
#ifdef HAVE_LIBMP3LAME
417
#endif // HAVE_LIBMP3LAME
422
#define len_allowed_au_codecs_avi (sizeof(allowed_au_codecs_avi) / \
423
sizeof(XVC_AuCodecID))
425
static const XVC_AuCodecID au_codecs_mp2_and_mp3[] = {
426
#ifdef HAVE_LIBMP3LAME
428
#endif // HAVE_LIBMP3LAME
432
#define len_au_codecs_mp2_and_mp3 (sizeof(au_codecs_mp2_and_mp3) / \
433
sizeof(XVC_AuCodecID))
435
#ifdef HAVE_LIBMP3LAME
436
static const XVC_AuCodecID au_codecs_mp3[] = { AU_CODEC_MP3 };
438
#define len_au_codecs_mp3 (sizeof(au_codecs_mp3) / \
439
sizeof(XVC_AuCodecID))
440
#endif // HAVE_LIBMP3LAME
442
static const XVC_AuCodecID au_codecs_mp2_and_pcm[] = {
447
#define len_au_codecs_mp2_and_pcm (sizeof(au_codecs_mp2_and_pcm) / \
448
sizeof(XVC_AuCodecID))
450
static const XVC_AuCodecID au_codecs_mp2[] = { AU_CODEC_MP2 };
452
#define len_au_codecs_mp2 (sizeof(au_codecs_mp2) / \
453
sizeof(XVC_AuCodecID))
454
#endif // HAVE_FFMPEG_AUDIO
458
* \brief global array storing all available file formats
460
const XVC_FFormat xvc_formats[NUMCAPS] = {
475
N_("X11 Window Dump"),
488
N_("Portable Graymap File"),
491
allowed_vid_codecs_pgm,
492
len_allowed_vid_codecs_pgm,
500
N_("Portable Anymap File"),
503
allowed_vid_codecs_ppm,
504
len_allowed_vid_codecs_ppm,
512
N_("Portable Network Graphics File"),
515
allowed_vid_codecs_png,
516
len_allowed_vid_codecs_png,
524
N_("Joint Picture Expert Group"),
527
allowed_vid_codecs_jpg,
528
len_allowed_vid_codecs_jpg,
536
N_("Microsoft Audio Video Interleaved File"),
539
allowed_vid_codecs_avi,
540
len_allowed_vid_codecs_avi,
541
#ifdef HAVE_FFMPEG_AUDIO
542
#ifdef HAVE_LIBMP3LAME
544
#else // HAVE_LIBMP3LAME
546
#endif // HAVE_LIBMP3LAME
547
allowed_au_codecs_avi,
548
len_allowed_au_codecs_avi,
549
#else // HAVE_FFMPEG_AUDIO
553
#endif // HAVE_FFMPEG_AUDIO
558
N_("General AVI file (DIVX default)"),
561
allowed_vid_codecs_avi,
562
len_allowed_vid_codecs_avi,
563
#ifdef HAVE_FFMPEG_AUDIO
564
#ifdef HAVE_LIBMP3LAME
566
#else // HAVE_LIBMP3LAME
568
#endif // HAVE_LIBMP3LAME
569
allowed_au_codecs_avi,
570
len_allowed_au_codecs_avi,
571
#else // HAVE_FFMPEG_AUDIO
575
#endif // HAVE_FFMPEG_AUDIO
580
N_("Microsoft Advanced Streaming Format"),
583
allowed_vid_codecs_asf,
584
len_allowed_vid_codecs_asf,
586
#ifdef HAVE_FFMPEG_AUDIO
587
au_codecs_mp2_and_mp3,
588
len_au_codecs_mp2_and_mp3,
589
#else // HAVE_FFMPEG_AUDIO
592
#endif // HAVE_FFMPEG_AUDIO
597
N_("Macromedia Flash Video Stream"),
600
allowed_vid_codecs_flv,
601
len_allowed_vid_codecs_flv,
602
#ifdef HAVE_FFMPEG_AUDIO
603
#ifdef HAVE_LIBMP3LAME
607
#else // HAVE_LIBMP3LAME
611
#endif // HAVE_LIBMP3LAME
612
#else // HAVE_FFMPEG_AUDIO
616
#endif // HAVE_FFMPEG_AUDIO
621
N_("Macromedia Shockwave Flash File"),
624
allowed_vid_codecs_swf,
625
len_allowed_vid_codecs_swf,
626
#ifdef HAVE_FFMPEG_AUDIO
627
#ifdef HAVE_LIBMP3LAME
631
#else // HAVE_LIBMP3LAME
635
#endif // HAVE_LIBMP3LAME
636
#else // HAVE_FFMPEG_AUDIO
640
#endif // HAVE_FFMPEG_AUDIO
645
N_("DV Video Format"),
648
allowed_vid_codecs_dv,
649
len_allowed_vid_codecs_dv,
650
#ifdef HAVE_FFMPEG_AUDIO
652
au_codecs_mp2_and_pcm,
653
len_au_codecs_mp2_and_pcm,
654
#else // HAVE_FFMPEG_AUDIO
658
#endif // HAVE_FFMPEG_AUDIO
663
N_("MPEG1 System Format"),
666
allowed_vid_codecs_mpeg1,
667
len_allowed_vid_codecs_mpeg1,
668
#ifdef HAVE_FFMPEG_AUDIO
676
#endif // HAVE_FFMPEG_AUDIO
681
N_("MPEG2 PS Format"),
684
allowed_vid_codecs_mpeg2,
685
len_allowed_vid_codecs_mpeg2,
686
#ifdef HAVE_FFMPEG_AUDIO
694
#endif // HAVE_FFMPEG_AUDIO
699
N_("Quicktime Format"),
702
allowed_vid_codecs_mov,
703
len_allowed_vid_codecs_mov,
704
#ifdef HAVE_FFMPEG_AUDIO
706
allowed_au_codecs_avi,
707
len_allowed_au_codecs_avi,
708
#else // HAVE_FFMPEG_AUDIO
712
#endif // HAVE_FFMPEG_AUDIO
719
* \brief finds libavcodec's codec id from xvidcap's
721
* @param xv_codec xvidcap's codec id
722
* @return an integer codec id as understood by libavcodec. Since 0 is a valid
723
* codec id we return -1 on failure
726
xvc_trans_codec (XVC_CodecID xv_codec)
730
if (xv_codec > 0 && xv_codec < NUMCODECS)
731
ret = xvc_codecs[xv_codec].ffmpeg_id;
737
* \brief finds out if a codec is in the an array of valid video codec ids
740
* @param format the id of the format to check
741
* @param codec the codec id to check for
742
* @return 0 for not found, otherwise the position in the array where codec
743
* was found started at 1 (i. e. normal index + 1)
746
xvc_is_valid_video_codec (XVC_FFormatID format, XVC_CodecID codec)
748
int i = 0, found = 0;
750
if (format < 0 || format >= NUMCAPS ||
751
xvc_formats[format].num_allowed_vid_codecs == 0)
753
for (i = 0; i < xvc_formats[format].num_allowed_vid_codecs; i++) {
754
if (xvc_formats[format].allowed_vid_codecs[i] == codec) {
763
* \brief finds out if an audio codec is in the an array of valid audio codec
764
* ids for a given format
766
* @param format the id of the format to check
767
* @param codec the audio codec id to check for
768
* @return 0 for not found, otherwise the position in the array where codec
769
* was found started at 1 (i. e. normal index + 1)
772
xvc_is_valid_audio_codec (XVC_FFormatID format, XVC_AuCodecID codec)
777
if (format < 0 || format >= NUMCAPS ||
778
xvc_formats[format].num_allowed_au_codecs == 0)
781
for (i = 0; i < xvc_formats[format].num_allowed_au_codecs; i++) {
782
if (xvc_formats[format].allowed_au_codecs[i] == codec) {
791
* \brief find target file format based on filename, i. e. the extension
793
* @param file a filename to test
794
* @return the index number pointing to the array element for the format found
795
* in the global file xvc_formats array
798
xvc_codec_get_target_from_filename (const char *file)
803
ext = rindex (file, '.');
809
for (i = 0; i < NUMCAPS; i++) {
810
if (xvc_formats[i].extensions) {
811
for (i2 = 0; i2 < xvc_formats[i].num_extensions; i2++) {
812
if (strcasecmp (ext, xvc_formats[i].extensions[i2]) == 0) {
813
// then we have found the right extension in target n
825
* \brief checks if fps rate is valid for given codec
827
* @param fps the fps rate to test
828
* @param codec the codec to test the fps rate against specified as an index
829
* number pointing to a codec in the global codecs array
830
* @param exact if 1 the function does an exact check, if 0 it will accept
831
* an fps value as valid, if there is a valid fps value that is
832
* not more than 0.01 larger or smaller than the fps given
833
* @return 1 for fps is valid for given codec or 0 for not valid
836
xvc_codec_is_valid_fps (XVC_Fps fps, XVC_CodecID codec, int exact)
838
#define DEBUGFUNCTION "xvc_codec_is_valid_fps()"
841
if (xvc_codecs[codec].num_allowed_fps == 0 &&
842
xvc_codecs[codec].num_allowed_fps_ranges == 0)
845
if (xvc_get_index_of_fps_array_element (xvc_codecs[codec].num_allowed_fps,
846
xvc_codecs[codec].allowed_fps,
852
if (xvc_codecs[codec].num_allowed_fps_ranges != 0) {
855
for (i = 0; i < xvc_codecs[codec].num_allowed_fps_ranges; i++) {
856
XVC_FpsRange curr = xvc_codecs[codec].allowed_fps_ranges[i];
858
if (XVC_FPS_GTE (fps, curr.start) && XVC_FPS_LTE (fps, curr.end)) {
870
* \brief gets the index number of a given fps value from an array of fps
873
* @param size the size of the array of fps values
874
* @param haystack array of XVC_Fps values
875
* @param needle the fps to search for
876
* @param exact if 1 the function looks for an exact match, if 0 it will accept
877
* an fps value as matching, if there is a valid fps value that is
878
* not more than 0.01 larger or smaller than the fps given
879
* @return the index number of the fps value in the array or -1 if not found
882
xvc_get_index_of_fps_array_element (int size,
883
const XVC_Fps * haystack,
884
XVC_Fps needle, int exact)
888
for (i = 0; i < size; i++) {
889
if (XVC_FPS_EQUAL (haystack[i], needle)) {
896
for (i = 0; i < size; i++) {
899
h = (double) haystack[i].num / (double) haystack[i].den;
900
n = (double) needle.num / (double) needle.den;
901
if (fabs (n - h) < 0.01) {
911
* \brief reads a string and returns an fps value
913
* This accepts two forms of specifying an fps value: Either as a floating
914
* point number with locale specific decimal point, or as a fraction like
917
* @param fps_string a string representation of an fps value
918
* @return the fps value read from the string or { 0, 1}
921
xvc_read_fps_from_string (char *fps_string)
923
struct lconv *loc = localeconv ();
924
XVC_Fps fps = { 0, 1 };
926
if ((index (fps_string, '/') &&
927
!strstr (fps_string, loc->decimal_point)) ||
928
(index (fps_string, '/') && strstr (fps_string, loc->decimal_point) &&
929
index (fps_string, '/') < strstr (fps_string, loc->decimal_point))
931
char *slash, *endptr;
933
fps.num = strtol (fps_string, &endptr, 10);
934
slash = index (fps_string, '/');
936
fps.den = strtol (slash, &endptr, 10);
940
fps.num = (int) (strtod (fps_string, &endptr) * pow (10,
941
xvc_get_number_of_fraction_digits_from_float_string
947
xvc_get_number_of_fraction_digits_from_float_string
948
(fps_string)) + 0.5);