annotate sauce.c @ 6491:b7f807b4cd88 libavformat tip

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