sauce.c 3.24 KB
Newer Older
Peter Ross's avatar
Peter Ross committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * SAUCE header parser
 * Copyright (c) 2010 Peter Ross <pross@xvid.org>
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

22 23 24 25 26
/**
 * @file
 * SAUCE header parser
 */

Peter Ross's avatar
Peter Ross committed
27
#include "libavutil/intreadwrite.h"
28
#include "libavutil/dict.h"
Peter Ross's avatar
Peter Ross committed
29 30 31 32 33
#include "avformat.h"
#include "sauce.h"

int ff_sauce_read(AVFormatContext *avctx, uint64_t *fsize, int *got_width, int get_height)
{
34
    AVIOContext *pb = avctx->pb;
Peter Ross's avatar
Peter Ross committed
35
    char buf[36];
Mans Rullgard's avatar
Mans Rullgard committed
36
    int datatype, filetype, t1, t2, nb_comments;
37
    uint64_t start_pos = avio_size(pb) - 128;
Peter Ross's avatar
Peter Ross committed
38

39
    avio_seek(pb, start_pos, SEEK_SET);
40
    if (avio_read(pb, buf, 7) != 7)
Peter Ross's avatar
Peter Ross committed
41 42 43 44 45
        return -1;
    if (memcmp(buf, "SAUCE00", 7))
        return -1;

#define GET_SAUCE_META(name,size) \
46
    if (avio_read(pb, buf, size) == size && buf[0]) { \
Peter Ross's avatar
Peter Ross committed
47
        buf[size] = 0; \
48
        av_dict_set(&avctx->metadata, name, buf, 0); \
Peter Ross's avatar
Peter Ross committed
49 50 51 52 53 54
    }

    GET_SAUCE_META("title",     35)
    GET_SAUCE_META("artist",    20)
    GET_SAUCE_META("publisher", 20)
    GET_SAUCE_META("date",      8)
55
    avio_skip(pb, 4);
56 57 58 59 60
    datatype    = avio_r8(pb);
    filetype    = avio_r8(pb);
    t1          = avio_rl16(pb);
    t2          = avio_rl16(pb);
    nb_comments = avio_r8(pb);
Mans Rullgard's avatar
Mans Rullgard committed
61
    avio_skip(pb, 1); /* flags */
62
    avio_skip(pb, 4);
Peter Ross's avatar
Peter Ross committed
63 64 65 66 67 68 69 70 71 72 73
    GET_SAUCE_META("encoder",   22);

    if (got_width && datatype && filetype) {
        if ((datatype == 1 && filetype <=2) || (datatype == 5 && filetype == 255) || datatype == 6) {
            if (t1) {
                avctx->streams[0]->codec->width = t1<<3;
                *got_width = 1;
            }
            if (get_height && t2)
                avctx->streams[0]->codec->height = t2<<4;
        } else if (datatype == 5) {
74
            if (filetype) {
Peter Ross's avatar
Peter Ross committed
75 76 77 78 79 80 81 82 83 84 85
                avctx->streams[0]->codec->width = (filetype == 1 ? t1 : filetype) << 4;
                *got_width = 1;
            }
            if (get_height && t2)
                avctx->streams[0]->codec->height = t2<<4;
        }
    }

    *fsize -= 128;

    if (nb_comments > 0) {
86
        avio_seek(pb, start_pos - 64*nb_comments - 5, SEEK_SET);
87
        if (avio_read(pb, buf, 5) == 5 && !memcmp(buf, "COMNT", 5)) {
Peter Ross's avatar
Peter Ross committed
88 89 90 91 92 93
            int i;
            char *str = av_malloc(65*nb_comments + 1);
            *fsize -= 64*nb_comments + 5;
            if (!str)
                return 0;
            for (i = 0; i < nb_comments; i++) {
94
                if (avio_read(pb, str + 65*i, 64) != 64)
Peter Ross's avatar
Peter Ross committed
95 96 97 98
                    break;
                str[65*i + 64] = '\n';
            }
            str[65*i] = 0;
99
            av_dict_set(&avctx->metadata, "comment", str, AV_DICT_DONT_STRDUP_VAL);
Peter Ross's avatar
Peter Ross committed
100 101 102 103 104
        }
    }

    return 0;
}