annotate sauce.c @ 6270:b6f0ef0c9d3a libavformat

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