~ubuntu-branches/ubuntu/feisty/avidemux/feisty

« back to all changes in this revision

Viewing changes to avidemux/ADM_video/ADM_vobsubinfo.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christian Marillat
  • Date: 2005-05-25 13:02:29 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20050525130229-jw94cav0yhmg7vjw
Tags: 1:2.0.40-0.0
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// C++ Implementation: vobsub parser
 
3
//
 
4
// Description:
 
5
//
 
6
//
 
7
// Author: Mean, 2005 GPL
 
8
//
 
9
// Copyright: See COPYING file that comes with this distribution
 
10
//
 
11
//
 
12
/***************************************************************************
 
13
 *                                                                         *
 
14
 *   This program is free software; you can redistribute it and/or modify  *
 
15
 *   it under the terms of the GNU General Public License as published by  *
 
16
 *   the Free Software Foundation; either version 2 of the License, or     *
 
17
 *   (at your option) any later version.                                   *
 
18
 *                                                                         *
 
19
 ***************************************************************************/
 
20
#include "config.h"
 
21
#include <stdio.h>
 
22
#include <stdlib.h>
 
23
#include <string.h>
 
24
#include <ADM_assert.h>
 
25
#include "ADM_library/default.h"
 
26
#include "ADM_vobsubinfo.h"
 
27
 
 
28
static uint8_t fillLine(char *str,VobSubInfo *sub,uint32_t line);
 
29
static uint32_t countLine(FILE *f,int index);
 
30
uint8_t fillPalette(char *str,VobSubInfo *sub);
 
31
 
 
32
uint8_t destroySubInfo(VobSubInfo *sub)
 
33
{
 
34
        if(!sub) return 1;      // ?
 
35
        if(sub->lines) delete [] sub->lines;
 
36
        delete sub;
 
37
        return 1;
 
38
 
 
39
 
 
40
}
 
41
//****************** 
 
42
// Extract Line info
 
43
//******************
 
44
uint8_t fillLine(char *str,VobSubInfo *sub,uint32_t line)
 
45
{
 
46
int hh,mm,ss,ms,o;
 
47
uint64_t ti;
 
48
uint32_t pos;
 
49
 
 
50
        ADM_assert(line<sub->nbLines);
 
51
 
 
52
        o=sscanf(str,"timestamp: %d:%d:%d:%d, filepos: %x\n",&hh,&mm,&ss,&ms,&pos);
 
53
        ADM_assert(o==5);
 
54
        
 
55
        ti=hh*3600+mm*60+ss;
 
56
        ti=ti*1000+ms;
 
57
        
 
58
        sub->lines[line].startTime      =ti;
 
59
        sub->lines[line].stopTime       =ti+1000;
 
60
        sub->lines[line].fileOffset     =pos;
 
61
        
 
62
        return 1;
 
63
}
 
64
//******************
 
65
// Extract palette
 
66
//******************
 
67
uint8_t fillPalette(char *str,VobSubInfo *sub)
 
68
{
 
69
int p[16],o;
 
70
 
 
71
        o=sscanf(str,"palette: %x, %x, %x, %x, %x, %x, %x, %x,"
 
72
                                " %x, %x, %x, %x, %x, %x, %x, %x",
 
73
                                &p[0],&p[1],&p[2],&p[3],&p[4],&p[5],&p[6],&p[7],
 
74
                                &p[8],&p[9],&p[10],&p[11],&p[12],&p[13],&p[14],&p[15]);
 
75
        ADM_assert(o==16);
 
76
        for(int i=0;i<16;i++)
 
77
                sub->Palette[i]=p[i];
 
78
                                       
 
79
        return 1;
 
80
}
 
81
//******************
 
82
//******************
 
83
uint8_t vobSubRead(char *filename,int index,VobSubInfo **info)
 
84
{
 
85
FILE            *file=NULL;
 
86
uint32_t        nb_lines;
 
87
VobSubInfo      *sub=NULL;
 
88
uint8_t         success=0;
 
89
uint32_t        line=0,l;
 
90
char            str[1024];
 
91
char            *dup;
 
92
int             language=0;
 
93
 
 
94
        if(!filename)
 
95
        {
 
96
                printf("Null file ?\n");
 
97
                return 0;
 
98
        }
 
99
        *info=NULL;
 
100
        file=fopen(filename,"rt");
 
101
        if(!file) 
 
102
        {
 
103
                printf("Could not open %s file\n",filename);
 
104
                return 0;
 
105
        }
 
106
        nb_lines=countLine(file,index);
 
107
        if(!nb_lines)
 
108
        {
 
109
                printf("Empty file\n");
 
110
                 goto subAbort;
 
111
        }
 
112
        // Try to read the file
 
113
        sub=new VobSubInfo;
 
114
        memset(sub,0,sizeof(VobSubInfo));
 
115
        //
 
116
        sub->nbLines=nb_lines;
 
117
        sub->lines=new vobSubLine[nb_lines];
 
118
        memset(sub->lines,0,sizeof(vobSubLine)*nb_lines);
 
119
        printf("Rebuilding %d lines of subs\n",nb_lines);
 
120
        
 
121
        while(line<nb_lines)
 
122
        {
 
123
                fgets(str,1023,file); 
 
124
                if(!strncmp(str,"palette:",7))                 fillPalette(str,sub);
 
125
                else 
 
126
                {
 
127
                        if(!strncmp(str,"timestamp: ",10) && language)        
 
128
                        {
 
129
                                fillLine(str,sub,line);
 
130
                                line++;
 
131
                        }
 
132
                        else
 
133
                        {
 
134
                                if(!strncmp(str,"id:",3))       // Catch language/index
 
135
                                {
 
136
                                  int  l;
 
137
                                  char s[50];
 
138
                                  sscanf(str,"id: %s index: %d",s,&l);
 
139
                                  printf("Found lang : %s index %d while searching %d\n",s,l,index);
 
140
                                  if(l==index) language=1;
 
141
                                  else language=0;                                                                              
 
142
                                
 
143
                                }
 
144
                                else
 
145
                                {
 
146
                                        if(!strncmp(str,"size:",5))       // Catch original screen dimension
 
147
                                        {
 
148
                                                sscanf(str,"size:%lux%lu",&(sub->width),&(sub->height));
 
149
                                        }
 
150
                                
 
151
                                }
 
152
                                
 
153
                        }
 
154
                }
 
155
        }
 
156
subSuccess:        
 
157
        success=1;
 
158
subAbort:        
 
159
        if(success)
 
160
        {
 
161
                *info=sub;
 
162
        }
 
163
        else
 
164
        {
 
165
                destroySubInfo( sub);        
 
166
        }
 
167
        fclose(file);
 
168
        return success;
 
169
 
 
170
}
 
171
//******************
 
172
// count #nb beginning by timestamp in file
 
173
//******************
 
174
uint32_t countLine(FILE *f,int index)
 
175
{
 
176
char str[1024],s[1024];
 
177
uint32_t nb=0;
 
178
uint32_t match=0;
 
179
int lang;
 
180
 
 
181
        fseek(f,0,SEEK_SET);
 
182
        while(!feof(f))
 
183
        {
 
184
                fgets(str,1023,f);
 
185
                if(!strncmp(str,"id:",3))
 
186
                {
 
187
                  sscanf(str,"id: %s index: %d",s,&lang);
 
188
                  if(lang==index) match=1;
 
189
                  else match=0;
 
190
                }
 
191
                else
 
192
                  if(match)
 
193
                        if(!strncmp(str,"timestamp: ",10)) nb++;
 
194
        
 
195
        }
 
196
        fseek(f,0,SEEK_SET);
 
197
        return nb;
 
198
}
 
199
//*****************************************
 
200
vobSubLanguage *vobSubAllocateLanguage(void)
 
201
{
 
202
  vobSubLanguage *l;
 
203
  l=new vobSubLanguage;
 
204
  memset(l,0,sizeof(vobSubLanguage));
 
205
  return l;  
 
206
}
 
207
//*****************************************
 
208
uint8_t vobSubDestroyLanguage(vobSubLanguage *lingua)
 
209
{
 
210
  for(uint32_t i=0;i<lingua->nbLanguage;i++)  
 
211
    delete [] lingua->language[i].name;
 
212
  delete lingua; 
 
213
  return 1;
 
214
}
 
215
//*****************************************
 
216
uint8_t vobSubGetLanguage(char *filename,vobSubLanguage *lingua)
 
217
{
 
218
  char str[1024];
 
219
  char s[16];
 
220
  uint32_t nb=0,index;
 
221
  FILE *fd=NULL;
 
222
  
 
223
  fd=fopen(filename,"rb");
 
224
  if(!fd) return 0;
 
225
  
 
226
  
 
227
 
 
228
  
 
229
  while(!feof(fd))
 
230
  {
 
231
    fgets(str,1023,fd);
 
232
    if(!strncmp(str,"id: ",3))
 
233
    {
 
234
      sscanf(str,"id: %s index: %d",s,&index);
 
235
      lingua->language[nb].name=new char[3];
 
236
      lingua->language[nb].name[0]=str[4];
 
237
      lingua->language[nb].name[1]=str[5];
 
238
      lingua->language[nb].name[2]=0;
 
239
      //
 
240
      lingua->language[nb].index=index;
 
241
      nb++;
 
242
    }        
 
243
  }
 
244
  lingua->nbLanguage=nb;
 
245
  return 1;
 
246
}
 
247
 
 
248
//EOF