Libav 0.7.1
|
00001 /* 00002 * SAUCE header parser 00003 * Copyright (c) 2010 Peter Ross <pross@xvid.org> 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * Libav is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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); /* flags */ 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 }