• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavformat/assdec.c

Go to the documentation of this file.
00001 /*
00002  * SSA/ASS demuxer
00003  * Copyright (c) 2008 Michael Niedermayer
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "avformat.h"
00023 
00024 #define MAX_LINESIZE 2000
00025 
00026 typedef struct ASSContext{
00027     uint8_t *event_buffer;
00028     uint8_t **event;
00029     unsigned int event_count;
00030     unsigned int event_index;
00031 }ASSContext;
00032 
00033 static void get_line(ByteIOContext *s, char *buf, int maxlen)
00034 {
00035     int i = 0;
00036     char c;
00037 
00038     do{
00039         c = get_byte(s);
00040         if (i < maxlen-1)
00041             buf[i++] = c;
00042     }while(c != '\n' && c);
00043 
00044     buf[i] = 0;
00045 }
00046 
00047 static int probe(AVProbeData *p)
00048 {
00049     const char *header= "[Script Info]";
00050 
00051     if(   !memcmp(p->buf  , header, strlen(header))
00052        || !memcmp(p->buf+3, header, strlen(header)))
00053         return AVPROBE_SCORE_MAX;
00054 
00055     return 0;
00056 }
00057 
00058 static int read_close(AVFormatContext *s)
00059 {
00060     ASSContext *ass = s->priv_data;
00061 
00062     av_freep(&ass->event_buffer);
00063     av_freep(&ass->event);
00064 
00065     return 0;
00066 }
00067 
00068 static int64_t get_pts(const uint8_t *p)
00069 {
00070     int hour, min, sec, hsec;
00071 
00072     if(sscanf(p, "%*[^,],%d:%d:%d%*c%d", &hour, &min, &sec, &hsec) != 4)
00073         return AV_NOPTS_VALUE;
00074 
00075 //    av_log(NULL, AV_LOG_ERROR, "%d %d %d %d %d [%s]\n", i, hour, min, sec, hsec, p);
00076 
00077     min+= 60*hour;
00078     sec+= 60*min;
00079 
00080     return sec*100+hsec;
00081 }
00082 
00083 static int event_cmp(uint8_t **a, uint8_t **b)
00084 {
00085     return get_pts(*a) - get_pts(*b);
00086 }
00087 
00088 static int read_header(AVFormatContext *s, AVFormatParameters *ap)
00089 {
00090     int i, header_remaining;
00091     ASSContext *ass = s->priv_data;
00092     ByteIOContext *pb = s->pb;
00093     AVStream *st;
00094     int allocated[2]={0};
00095     uint8_t *p, **dst[2]={0};
00096     int pos[2]={0};
00097 
00098     st = av_new_stream(s, 0);
00099     if (!st)
00100         return -1;
00101     av_set_pts_info(st, 64, 1, 100);
00102     st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
00103     st->codec->codec_id= CODEC_ID_SSA;
00104 
00105     header_remaining= INT_MAX;
00106     dst[0] = &st->codec->extradata;
00107     dst[1] = &ass->event_buffer;
00108     while(!url_feof(pb)){
00109         uint8_t line[MAX_LINESIZE];
00110 
00111         get_line(pb, line, sizeof(line));
00112 
00113         if(!memcmp(line, "[Events]", 8))
00114             header_remaining= 2;
00115         else if(line[0]=='[')
00116             header_remaining= INT_MAX;
00117 
00118         i= header_remaining==0;
00119 
00120         if(i && get_pts(line) == AV_NOPTS_VALUE)
00121             continue;
00122 
00123         p = av_fast_realloc(*(dst[i]), &allocated[i], pos[i]+MAX_LINESIZE);
00124         if(!p)
00125             goto fail;
00126         *(dst[i])= p;
00127         memcpy(p + pos[i], line, strlen(line)+1);
00128         pos[i] += strlen(line);
00129         if(i) ass->event_count++;
00130         else  header_remaining--;
00131     }
00132     st->codec->extradata_size= pos[0];
00133 
00134     if(ass->event_count >= UINT_MAX / sizeof(*ass->event))
00135         goto fail;
00136 
00137     ass->event= av_malloc(ass->event_count * sizeof(*ass->event));
00138     p= ass->event_buffer;
00139     for(i=0; i<ass->event_count; i++){
00140         ass->event[i]= p;
00141         while(*p && *p != '\n')
00142             p++;
00143         p++;
00144     }
00145 
00146     qsort(ass->event, ass->event_count, sizeof(*ass->event), (void*)event_cmp);
00147 
00148     return 0;
00149 
00150 fail:
00151     read_close(s);
00152 
00153     return -1;
00154 }
00155 
00156 static int read_packet(AVFormatContext *s, AVPacket *pkt)
00157 {
00158     ASSContext *ass = s->priv_data;
00159     uint8_t *p, *end;
00160 
00161     if(ass->event_index >= ass->event_count)
00162         return AVERROR(EIO);
00163 
00164     p= ass->event[ ass->event_index ];
00165 
00166     end= strchr(p, '\n');
00167     av_new_packet(pkt, end ? end-p+1 : strlen(p));
00168     pkt->flags |= AV_PKT_FLAG_KEY;
00169     pkt->pos= p - ass->event_buffer + s->streams[0]->codec->extradata_size;
00170     pkt->pts= pkt->dts= get_pts(p);
00171     memcpy(pkt->data, p, pkt->size);
00172 
00173     ass->event_index++;
00174 
00175     return 0;
00176 }
00177 
00178 AVInputFormat ass_demuxer = {
00179     "ass",
00180     NULL_IF_CONFIG_SMALL("SSA/ASS format"),
00181     sizeof(ASSContext),
00182     probe,
00183     read_header,
00184     read_packet,
00185     read_close,
00186 //    read_seek,
00187 };

Generated on Fri Sep 16 2011 17:17:47 for FFmpeg by  doxygen 1.7.1