Statistics
| Branch: | Revision:

ffmpeg / libavformat / sauce.c @ b7effd4e

History | View | Annotate | Download (3.24 KB)

1
/*
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
/**
23
 * @file
24
 * SAUCE header parser
25
 */
26

    
27
#include "libavutil/intreadwrite.h"
28
#include "avformat.h"
29
#include "sauce.h"
30

    
31
int ff_sauce_read(AVFormatContext *avctx, uint64_t *fsize, int *got_width, int get_height)
32
{
33
    AVIOContext *pb = avctx->pb;
34
    char buf[36];
35
    int datatype, filetype, t1, t2, nb_comments, flags;
36
    uint64_t start_pos = url_fsize(pb) - 128;
37

    
38
    url_fseek(pb, start_pos, SEEK_SET);
39
    if (avio_read(pb, buf, 7) != 7)
40
        return -1;
41
    if (memcmp(buf, "SAUCE00", 7))
42
        return -1;
43

    
44
#define GET_SAUCE_META(name,size) \
45
    if (avio_read(pb, buf, size) == size && buf[0]) { \
46
        buf[size] = 0; \
47
        av_metadata_set2(&avctx->metadata, name, buf, 0); \
48
    }
49

    
50
    GET_SAUCE_META("title",     35)
51
    GET_SAUCE_META("artist",    20)
52
    GET_SAUCE_META("publisher", 20)
53
    GET_SAUCE_META("date",      8)
54
    url_fskip(pb, 4);
55
    datatype    = avio_r8(pb);
56
    filetype    = avio_r8(pb);
57
    t1          = avio_rl16(pb);
58
    t2          = avio_rl16(pb);
59
    nb_comments = avio_r8(pb);
60
    flags       = avio_r8(pb);
61
    url_fskip(pb, 4);
62
    GET_SAUCE_META("encoder",   22);
63

    
64
    if (got_width && datatype && filetype) {
65
        if ((datatype == 1 && filetype <=2) || (datatype == 5 && filetype == 255) || datatype == 6) {
66
            if (t1) {
67
                avctx->streams[0]->codec->width = t1<<3;
68
                *got_width = 1;
69
            }
70
            if (get_height && t2)
71
                avctx->streams[0]->codec->height = t2<<4;
72
        } else if (datatype == 5) {
73
            if (filetype > 1) {
74
                avctx->streams[0]->codec->width = (filetype == 1 ? t1 : filetype) << 4;
75
                *got_width = 1;
76
            }
77
            if (get_height && t2)
78
                avctx->streams[0]->codec->height = t2<<4;
79
        }
80
    }
81

    
82
    *fsize -= 128;
83

    
84
    if (nb_comments > 0) {
85
        url_fseek(pb, start_pos - 64*nb_comments - 5, SEEK_SET);
86
        if (avio_read(pb, buf, 5) == 5 && !memcmp(buf, "COMNT", 5)) {
87
            int i;
88
            char *str = av_malloc(65*nb_comments + 1);
89
            *fsize -= 64*nb_comments + 5;
90
            if (!str)
91
                return 0;
92
            for (i = 0; i < nb_comments; i++) {
93
                if (avio_read(pb, str + 65*i, 64) != 64)
94
                    break;
95
                str[65*i + 64] = '\n';
96
            }
97
            str[65*i] = 0;
98
            av_metadata_set2(&avctx->metadata, "comment", str, AV_METADATA_DONT_STRDUP_VAL);
99
        }
100
    }
101

    
102
    return 0;
103
}