Browse code

Add ff_sauce_read()

Originally committed as revision 24300 to svn://svn.ffmpeg.org/ffmpeg/trunk

Peter Ross authored on 2010/07/18 17:05:04
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,99 @@
0
+/*
1
+ * @file
2
+ * SAUCE header parser
3
+ * Copyright (c) 2010 Peter Ross <pross@xvid.org>
4
+ *
5
+ * This file is part of FFmpeg.
6
+ *
7
+ * FFmpeg is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * FFmpeg is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with FFmpeg; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+#include "libavutil/intreadwrite.h"
23
+#include "avformat.h"
24
+#include "sauce.h"
25
+
26
+int ff_sauce_read(AVFormatContext *avctx, uint64_t *fsize, int *got_width, int get_height)
27
+{
28
+    ByteIOContext *pb = avctx->pb;
29
+    char buf[36];
30
+    int datatype, filetype, t1, t2, nb_comments, flags;
31
+    uint64_t start_pos = url_fsize(pb) - 128;
32
+
33
+    url_fseek(pb, start_pos, SEEK_SET);
34
+    if (get_buffer(pb, buf, 7) != 7)
35
+        return -1;
36
+    if (memcmp(buf, "SAUCE00", 7))
37
+        return -1;
38
+
39
+#define GET_SAUCE_META(name,size) \
40
+    if (get_buffer(pb, buf, size) == size && buf[0]) { \
41
+        buf[size] = 0; \
42
+        av_metadata_set2(&avctx->metadata, name, buf, 0); \
43
+    }
44
+
45
+    GET_SAUCE_META("title",     35)
46
+    GET_SAUCE_META("artist",    20)
47
+    GET_SAUCE_META("publisher", 20)
48
+    GET_SAUCE_META("date",      8)
49
+    url_fskip(pb, 4);
50
+    datatype    = get_byte(pb);
51
+    filetype    = get_byte(pb);
52
+    t1          = get_le16(pb);
53
+    t2          = get_le16(pb);
54
+    nb_comments = get_byte(pb);
55
+    flags       = get_byte(pb);
56
+    url_fskip(pb, 4);
57
+    GET_SAUCE_META("encoder",   22);
58
+
59
+    if (got_width && datatype && filetype) {
60
+        if ((datatype == 1 && filetype <=2) || (datatype == 5 && filetype == 255) || datatype == 6) {
61
+            if (t1) {
62
+                avctx->streams[0]->codec->width = t1<<3;
63
+                *got_width = 1;
64
+            }
65
+            if (get_height && t2)
66
+                avctx->streams[0]->codec->height = t2<<4;
67
+        } else if (datatype == 5) {
68
+            if (filetype > 1) {
69
+                avctx->streams[0]->codec->width = (filetype == 1 ? t1 : filetype) << 4;
70
+                *got_width = 1;
71
+            }
72
+            if (get_height && t2)
73
+                avctx->streams[0]->codec->height = t2<<4;
74
+        }
75
+    }
76
+
77
+    *fsize -= 128;
78
+
79
+    if (nb_comments > 0) {
80
+        url_fseek(pb, start_pos - 64*nb_comments - 5, SEEK_SET);
81
+        if (get_buffer(pb, buf, 5) == 5 && !memcmp(buf, "COMNT", 5)) {
82
+            int i;
83
+            char *str = av_malloc(65*nb_comments + 1);
84
+            *fsize -= 64*nb_comments + 5;
85
+            if (!str)
86
+                return 0;
87
+            for (i = 0; i < nb_comments; i++) {
88
+                if (get_buffer(pb, str + 65*i, 64) != 64)
89
+                    break;
90
+                str[65*i + 64] = '\n';
91
+            }
92
+            str[65*i] = 0;
93
+            av_metadata_set2(&avctx->metadata, "comment", str, AV_METADATA_DONT_STRDUP_VAL);
94
+        }
95
+    }
96
+
97
+    return 0;
98
+}
0 99
new file mode 100644
... ...
@@ -0,0 +1,37 @@
0
+/*
1
+ * @file
2
+ * SAUCE header parser
3
+ * Copyright (c) 2010 Peter Ross <pross@xvid.org>
4
+ *
5
+ * This file is part of FFmpeg.
6
+ *
7
+ * FFmpeg is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * FFmpeg is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with FFmpeg; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+#ifndef AVFORMAT_SAUCE_H
23
+#define AVFORMAT_SAUCE_H
24
+
25
+#include "avformat.h"
26
+
27
+/**
28
+ * @param avctx AVFormatContext
29
+ * @param fsize[out] return length of file, less SAUCE header
30
+ * @param got_width[out] set to non-zero if SAUCE header reported height
31
+ * @param get_height Tell SAUCE header to parse height
32
+ */
33
+int ff_sauce_read(AVFormatContext *avctx, uint64_t *fsize, int *got_width, int get_height);
34
+
35
+#endif /* AVFORMAT_SAUCE_H */
36
+