2010-07-18 08:05:04 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2010-07-19 11:50:07 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* SAUCE header parser
|
|
|
|
*/
|
|
|
|
|
2010-07-18 08:05:04 +00:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2011-05-22 10:46:29 +00:00
|
|
|
#include "libavutil/dict.h"
|
2010-07-18 08:05:04 +00:00
|
|
|
#include "avformat.h"
|
|
|
|
#include "sauce.h"
|
|
|
|
|
|
|
|
int ff_sauce_read(AVFormatContext *avctx, uint64_t *fsize, int *got_width, int get_height)
|
|
|
|
{
|
2011-02-20 10:04:12 +00:00
|
|
|
AVIOContext *pb = avctx->pb;
|
2010-07-18 08:05:04 +00:00
|
|
|
char buf[36];
|
2011-06-01 16:26:27 +00:00
|
|
|
int datatype, filetype, t1, t2, nb_comments;
|
2011-03-04 18:57:36 +00:00
|
|
|
uint64_t start_pos = avio_size(pb) - 128;
|
2010-07-18 08:05:04 +00:00
|
|
|
|
2011-02-28 13:57:54 +00:00
|
|
|
avio_seek(pb, start_pos, SEEK_SET);
|
2011-02-21 15:43:01 +00:00
|
|
|
if (avio_read(pb, buf, 7) != 7)
|
2010-07-18 08:05:04 +00:00
|
|
|
return -1;
|
|
|
|
if (memcmp(buf, "SAUCE00", 7))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
#define GET_SAUCE_META(name,size) \
|
2011-02-21 15:43:01 +00:00
|
|
|
if (avio_read(pb, buf, size) == size && buf[0]) { \
|
2010-07-18 08:05:04 +00:00
|
|
|
buf[size] = 0; \
|
2011-05-22 10:46:29 +00:00
|
|
|
av_dict_set(&avctx->metadata, name, buf, 0); \
|
2010-07-18 08:05:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GET_SAUCE_META("title", 35)
|
|
|
|
GET_SAUCE_META("artist", 20)
|
|
|
|
GET_SAUCE_META("publisher", 20)
|
|
|
|
GET_SAUCE_META("date", 8)
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, 4);
|
2011-02-21 15:43:01 +00:00
|
|
|
datatype = avio_r8(pb);
|
|
|
|
filetype = avio_r8(pb);
|
|
|
|
t1 = avio_rl16(pb);
|
|
|
|
t2 = avio_rl16(pb);
|
|
|
|
nb_comments = avio_r8(pb);
|
2011-06-01 16:26:27 +00:00
|
|
|
avio_skip(pb, 1); /* flags */
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, 4);
|
2010-07-18 08:05:04 +00:00
|
|
|
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) {
|
2012-12-13 23:06:15 +00:00
|
|
|
if (filetype) {
|
2010-07-18 08:05:04 +00:00
|
|
|
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) {
|
2011-02-28 13:57:54 +00:00
|
|
|
avio_seek(pb, start_pos - 64*nb_comments - 5, SEEK_SET);
|
2011-02-21 15:43:01 +00:00
|
|
|
if (avio_read(pb, buf, 5) == 5 && !memcmp(buf, "COMNT", 5)) {
|
2010-07-18 08:05:04 +00:00
|
|
|
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++) {
|
2011-02-21 15:43:01 +00:00
|
|
|
if (avio_read(pb, str + 65*i, 64) != 64)
|
2010-07-18 08:05:04 +00:00
|
|
|
break;
|
|
|
|
str[65*i + 64] = '\n';
|
|
|
|
}
|
|
|
|
str[65*i] = 0;
|
2011-05-22 10:46:29 +00:00
|
|
|
av_dict_set(&avctx->metadata, "comment", str, AV_DICT_DONT_STRDUP_VAL);
|
2010-07-18 08:05:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|