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