source: trunk/src/testing/bin/fileServer/misc/mmpython/disc/ifomodule.c @ 4

Revision 4, 6.2 KB checked in by ajaworski, 13 years ago (diff)

Added modified SAGE sources

RevLine 
[4]1//based on http://arnfast.net/projects/ifoinfo.php by Jens Arnfast
2
3#include <Python.h>
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <ctype.h>
8#include <string.h>
9#include <unistd.h>
10#include <assert.h>
11
12#include <dvdread/dvd_reader.h>
13#include <dvdread/ifo_types.h>
14#include <dvdread/ifo_read.h>
15
16static PyObject *ifoinfo_open(PyObject *self, PyObject *args);
17static PyObject *ifoinfo_close(PyObject *self, PyObject *args);
18
19static PyObject *ifoinfo_read_title(PyObject *self, PyObject *args);
20static PyObject *ifoinfo_get_audio_tracks(PyObject *self, PyObject *args);
21static PyObject *ifoinfo_get_subtitle_tracks(PyObject *self, PyObject *args);
22
23static PyMethodDef IfoMethods[] = {
24  {"open",  ifoinfo_open, METH_VARARGS},
25  {"close",  ifoinfo_close, METH_VARARGS},
26  {"title", ifoinfo_read_title, METH_VARARGS},
27  {"audio", ifoinfo_get_audio_tracks, METH_VARARGS},
28  {"subtitle", ifoinfo_get_subtitle_tracks, METH_VARARGS},
29  {NULL, NULL}
30};
31
32
33void initifoparser() {
34   (void) Py_InitModule("ifoparser", IfoMethods);
35}
36
37
38dvd_reader_t *dvd;
39ifo_handle_t *ifofile;
40
41static PyObject *ifoinfo_open(PyObject *self, PyObject *args) {
42  tt_srpt_t *tt_srpt;
43  int i, ch, gotopt = -1, dochapters = -1;
44  char *dvddevice;
45
46  if (!PyArg_ParseTuple(args, "s", &dvddevice))
47    return Py_BuildValue("i", 0);
48
49  dvd = DVDOpen(dvddevice);
50 
51  if (!dvd)
52    return Py_BuildValue("i", 0);
53 
54  ifofile = ifoOpen(dvd, 0);
55  if (!ifofile) {
56    DVDClose(dvd);
57    return Py_BuildValue("i", 0);
58  }
59
60  tt_srpt = ifofile->tt_srpt;
61  return Py_BuildValue("i", tt_srpt->nr_of_srpts);
62}
63
64
65static PyObject *ifoinfo_close(PyObject *self, PyObject *args) {
66  ifoClose(ifofile);
67  DVDClose(dvd);
68  return Py_BuildValue("i", 0);
69}
70
71
72static PyObject *ifoinfo_read_title(PyObject *self, PyObject *args) {
73  int i;
74
75  tt_srpt_t *tt_srpt;
76  ifo_handle_t *vtsfile;
77  int vtsnum, ttnnum, j;
78  long playtime;
79
80  if (!PyArg_ParseTuple(args, "i", &i))
81    return Py_BuildValue("(iiiii)", 0, 0, 0, 0, 0);
82
83  i--;
84 
85  tt_srpt = ifofile->tt_srpt;
86  vtsnum  = tt_srpt->title[i].title_set_nr;
87  ttnnum  = tt_srpt->title[i].vts_ttn;
88     
89  vtsfile = ifoOpen(dvd, vtsnum);
90   
91  if (!vtsfile)
92    return Py_BuildValue("(iiiii)", 0, 0, 0, 0, 0);
93
94  playtime = 0;
95 
96  if (vtsfile->vts_pgcit) {
97    dvd_time_t *ttime;
98    ttime = &vtsfile->vts_pgcit->pgci_srp[0].pgc->playback_time;
99    playtime = ((ttime->hour * 60) + ttime->minute) * 60 + ttime->second;
100  }
101
102  // Number of Chapters, Number of Angles, Playback time, Num Audio tracks,
103  // Num subtitles
104  return Py_BuildValue("(iiiii)", tt_srpt->title[i].nr_of_ptts,
105                       tt_srpt->title[i].nr_of_angles, playtime,                       
106                       vtsfile->vtsi_mat->nr_of_vts_audio_streams,
107                       vtsfile->vtsi_mat->nr_of_vts_subp_streams);
108}
109
110
111static PyObject * ifoinfo_get_subtitle_tracks(PyObject *self, PyObject *args) {
112  char language[5];
113  int trackno;
114  tt_srpt_t *tt_srpt;
115  int vtsnum, ttnnum;
116  ifo_handle_t *vtsfile;
117  int i;
118  subp_attr_t *attr;
119 
120  if (!PyArg_ParseTuple(args, "ii", &i, &trackno))
121    return Py_BuildValue("(s)", "N/A");
122
123  i--;
124  trackno--;
125 
126  tt_srpt = ifofile->tt_srpt;
127  vtsnum = tt_srpt->title[i].title_set_nr;
128  ttnnum = tt_srpt->title[i].vts_ttn;
129 
130  vtsfile = ifoOpen(dvd, vtsnum);
131 
132  if (vtsfile->vts_pgcit) {
133    attr = &vtsfile->vtsi_mat->vts_subp_attr[trackno];
134
135    if ( attr->type == 0
136         && attr->lang_code == 0
137         && attr->zero1 == 0
138         && attr->zero2 == 0
139         && attr->lang_extension == 0 ) {
140      return Py_BuildValue("(s)", "N/A");
141    }
142
143    /* language code */
144    if (isalpha((int)(attr->lang_code >> 8)) && isalpha((int)(attr->lang_code & 0xff))) {
145      snprintf(language, 5, "%c%c", attr->lang_code >> 8, attr->lang_code & 0xff);
146    } else {
147      snprintf(language, 5, "%02x%02x", 0xff & (unsigned)(attr->lang_code >> 8),
148               0xff & (unsigned)(attr->lang_code & 0xff));
149    }
150
151    return Py_BuildValue("(s)", language);
152  }
153}
154
155
156static PyObject * ifoinfo_get_audio_tracks(PyObject *self, PyObject *args) {
157  char audioformat[10];
158  char audiolang[5];
159  int audiochannels;
160  int audioid;
161  int audiofreq;
162  audio_attr_t *attr;
163  int i;
164  int trackno;
165  tt_srpt_t *tt_srpt;
166  int vtsnum, ttnnum;
167  ifo_handle_t *vtsfile;
168 
169  if (!PyArg_ParseTuple(args, "ii", &i, &trackno))
170    return Py_BuildValue("i", 0);
171
172  i--;
173  trackno--;
174 
175  tt_srpt = ifofile->tt_srpt;
176  vtsnum = tt_srpt->title[i].title_set_nr;
177  ttnnum = tt_srpt->title[i].vts_ttn;
178 
179  vtsfile = ifoOpen(dvd, vtsnum);
180 
181  if (vtsfile->vts_pgcit && vtsfile->vtsi_mat) {
182    attr = &vtsfile->vtsi_mat->vts_audio_attr[trackno];
183
184    audioid = trackno + 128;
185   
186    if ( attr->audio_format == 0
187         && attr->multichannel_extension == 0
188         && attr->lang_type == 0
189         && attr->application_mode == 0
190         && attr->quantization == 0
191         && attr->sample_frequency == 0
192         && attr->channels == 0
193         && attr->lang_extension == 0
194         && attr->unknown1 == 0
195         && attr->unknown1 == 0) {
196      snprintf(audioformat, 10, "Unknown");
197      return Py_BuildValue("i", 0);
198    }
199   
200    /* audio format */
201    switch (attr->audio_format) {
202    case 0:
203      snprintf(audioformat, 10, "ac3");
204      break;
205    case 1:
206      snprintf(audioformat, 10, "N/A");
207      break;
208    case 2:
209      snprintf(audioformat, 10, "mpeg1");
210      break;
211    case 3:
212      snprintf(audioformat, 10, "mpeg2ext");
213    break;
214    case 4:
215      snprintf(audioformat, 10, "lpcm");
216      break;
217    case 5:
218      snprintf(audioformat, 10, "N/A");
219      break;
220    case 6:
221      snprintf(audioformat, 10, "dts");
222      break;
223    default:
224      snprintf(audioformat, 10, "N/A");
225    }
226   
227    switch (attr->lang_type) {
228    case 0:
229      assert(attr->lang_code == 0 || attr->lang_code == 0xffff);
230      snprintf(audiolang, 5, "N/A");
231      break;
232    case 1:
233      snprintf(audiolang, 5, "%c%c", attr->lang_code>>8, attr->lang_code & 0xff);
234      break;
235    default:
236      snprintf(audiolang, 5, "N/A");
237    }
238   
239    switch(attr->sample_frequency) {
240    case 0:
241      audiofreq = 48;
242      break;
243    case 1:
244      audiofreq = -1;
245      break;
246    default:
247      audiofreq = -1;
248    }
249   
250    audiochannels = attr->channels + 1;
251   
252    //AUDIOTRACK: ID=%i; LANG=%s; FORMAT=%s; CHANNELS=%i; FREQ=%ikHz
253    return Py_BuildValue("(issii)", audioid, audiolang, audioformat, audiochannels,
254                         audiofreq);
255  }
256
257  return NULL;
258}
259
Note: See TracBrowser for help on using the repository browser.