00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "libavutil/intreadwrite.h"
00028 #include "libavutil/dict.h"
00029 #include "avformat.h"
00030 #include "sauce.h"
00031
00032 int ff_sauce_read(AVFormatContext *avctx, uint64_t *fsize, int *got_width, int get_height)
00033 {
00034 AVIOContext *pb = avctx->pb;
00035 char buf[36];
00036 int datatype, filetype, t1, t2, nb_comments;
00037 uint64_t start_pos = avio_size(pb) - 128;
00038
00039 avio_seek(pb, start_pos, SEEK_SET);
00040 if (avio_read(pb, buf, 7) != 7)
00041 return -1;
00042 if (memcmp(buf, "SAUCE00", 7))
00043 return -1;
00044
00045 #define GET_SAUCE_META(name,size) \
00046 if (avio_read(pb, buf, size) == size && buf[0]) { \
00047 buf[size] = 0; \
00048 av_dict_set(&avctx->metadata, name, buf, 0); \
00049 }
00050
00051 GET_SAUCE_META("title", 35)
00052 GET_SAUCE_META("artist", 20)
00053 GET_SAUCE_META("publisher", 20)
00054 GET_SAUCE_META("date", 8)
00055 avio_skip(pb, 4);
00056 datatype = avio_r8(pb);
00057 filetype = avio_r8(pb);
00058 t1 = avio_rl16(pb);
00059 t2 = avio_rl16(pb);
00060 nb_comments = avio_r8(pb);
00061 avio_skip(pb, 1);
00062 avio_skip(pb, 4);
00063 GET_SAUCE_META("encoder", 22);
00064
00065 if (got_width && datatype && filetype) {
00066 if ((datatype == 1 && filetype <=2) || (datatype == 5 && filetype == 255) || datatype == 6) {
00067 if (t1) {
00068 avctx->streams[0]->codec->width = t1<<3;
00069 *got_width = 1;
00070 }
00071 if (get_height && t2)
00072 avctx->streams[0]->codec->height = t2<<4;
00073 } else if (datatype == 5) {
00074 if (filetype > 1) {
00075 avctx->streams[0]->codec->width = (filetype == 1 ? t1 : filetype) << 4;
00076 *got_width = 1;
00077 }
00078 if (get_height && t2)
00079 avctx->streams[0]->codec->height = t2<<4;
00080 }
00081 }
00082
00083 *fsize -= 128;
00084
00085 if (nb_comments > 0) {
00086 avio_seek(pb, start_pos - 64*nb_comments - 5, SEEK_SET);
00087 if (avio_read(pb, buf, 5) == 5 && !memcmp(buf, "COMNT", 5)) {
00088 int i;
00089 char *str = av_malloc(65*nb_comments + 1);
00090 *fsize -= 64*nb_comments + 5;
00091 if (!str)
00092 return 0;
00093 for (i = 0; i < nb_comments; i++) {
00094 if (avio_read(pb, str + 65*i, 64) != 64)
00095 break;
00096 str[65*i + 64] = '\n';
00097 }
00098 str[65*i] = 0;
00099 av_dict_set(&avctx->metadata, "comment", str, AV_DICT_DONT_STRDUP_VAL);
00100 }
00101 }
00102
00103 return 0;
00104 }