FFmpeg
sauce.c
Go to the documentation of this file.
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/dict.h"
28 #include "libavutil/mem.h"
29 #include "avformat.h"
30 #include "sauce.h"
31 
32 int ff_sauce_read(AVFormatContext *avctx, uint64_t *fsize, int *got_width, int get_height)
33 {
34  AVIOContext *pb = avctx->pb;
35  char buf[36];
36  int datatype, filetype, t1, t2, nb_comments;
37  int64_t start_pos = avio_size(pb);
38 
39  if (start_pos < 128)
40  return AVERROR_INVALIDDATA;
41 
42  start_pos -= 128;
43 
44  avio_seek(pb, start_pos, SEEK_SET);
45  if (avio_read(pb, buf, 7) != 7)
46  return -1;
47  if (memcmp(buf, "SAUCE00", 7))
48  return -1;
49 
50 #define GET_SAUCE_META(name,size) \
51  if (avio_read(pb, buf, size) == size && buf[0]) { \
52  buf[size] = 0; \
53  av_dict_set(&avctx->metadata, name, buf, 0); \
54  }
55 
56  GET_SAUCE_META("title", 35)
57  GET_SAUCE_META("artist", 20)
58  GET_SAUCE_META("publisher", 20)
59  GET_SAUCE_META("date", 8)
60  avio_skip(pb, 4);
61  datatype = avio_r8(pb);
62  filetype = avio_r8(pb);
63  t1 = avio_rl16(pb);
64  t2 = avio_rl16(pb);
65  nb_comments = avio_r8(pb);
66  avio_skip(pb, 1); /* flags */
67  avio_skip(pb, 4);
68  GET_SAUCE_META("encoder", 22);
69 
70  if (got_width && datatype && filetype) {
71  if ((datatype == 1 && filetype <=2) || (datatype == 5 && filetype == 255) || datatype == 6) {
72  if (t1) {
73  avctx->streams[0]->codecpar->width = t1<<3;
74  *got_width = 1;
75  }
76  if (get_height && t2)
77  avctx->streams[0]->codecpar->height = t2<<4;
78  } else if (datatype == 5) {
79  if (filetype) {
80  avctx->streams[0]->codecpar->width = (filetype == 1 ? t1 : filetype) << 4;
81  *got_width = 1;
82  }
83  if (get_height && t2)
84  avctx->streams[0]->codecpar->height = t2<<4;
85  }
86  }
87 
88  *fsize -= 128;
89 
90  if (nb_comments > 0) {
91  avio_seek(pb, start_pos - 64*nb_comments - 5, SEEK_SET);
92  if (avio_read(pb, buf, 5) == 5 && !memcmp(buf, "COMNT", 5)) {
93  int i;
94  char *str = av_malloc(65*nb_comments + 1);
95  *fsize -= 64*nb_comments + 5;
96  if (!str)
97  return 0;
98  for (i = 0; i < nb_comments; i++) {
99  if (avio_read(pb, str + 65*i, 64) != 64)
100  break;
101  str[65*i + 64] = '\n';
102  }
103  str[65*i] = 0;
104  av_dict_set(&avctx->metadata, "comment", str, AV_DICT_DONT_STRDUP_VAL);
105  }
106  }
107 
108  return 0;
109 }
int64_t
long long int64_t
Definition: coverity.c:34
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1368
avio_size
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:323
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:714
AV_DICT_DONT_STRDUP_VAL
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:79
AVFormatContext::metadata
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1535
get_height
static int get_height(const FieldMatchContext *fm, const AVFrame *f, int plane, int input)
Definition: vf_fieldmatch.c:163
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:134
fsize
static int64_t fsize(FILE *f)
Definition: audiomatch.c:29
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1342
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
GET_SAUCE_META
#define GET_SAUCE_META(name, size)
ff_sauce_read
int ff_sauce_read(AVFormatContext *avctx, uint64_t *fsize, int *got_width, int get_height)
Definition: sauce.c:32
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:603
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVCodecParameters::height
int height
Definition: codec_par.h:135
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:231
avformat.h
dict.h
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:612
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:318
mem.h
av_dict_set
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:88
sauce.h
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61