summaryrefslogtreecommitdiffstats
path: root/libk3b/videodvd/k3bvideodvd.cpp
blob: f4353932385690600eb470d45011bddf4404cdaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* 
 *
 * $Id: sourceheader 511311 2006-02-19 14:51:05Z trueg $
 * Copyright (C) 2006 Sebastian Trueg <trueg@k3b.org>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See the file "COPYING" for the exact licensing terms.
 */


#ifndef __STDC_LIMIT_MACROS
#define __STDC_LIMIT_MACROS // needed for *_MAX macros in dvdread headers
#endif

#include "k3bvideodvd.h"

#include <k3bdevice.h>

#include <tqfile.h>

#include <klocale.h>

#include <inttypes.h> // needed by dvdreads headers
#include <dvdread/dvd_reader.h>
#include <dvdread/ifo_types.h>
#include <dvdread/ifo_read.h>


// I don't get this stuff, I should read something about VideoDVD some day...
#define CONVERT_TIME(x) (((x & 0xf0) >> 3) * 5 + (x & 0x0f))
#define CONVERT_FRAME(x) (((x & 0x30) >> 3) * 5 + (x & 0x0f))


K3bVideoDVD::VideoDVD::VideoDVD()
{
}


K3bVideoDVD::VideoDVD::~VideoDVD()
{
}


bool K3bVideoDVD::VideoDVD::valid() const
{
  return ( m_device != 0 );
}


bool K3bVideoDVD::VideoDVD::open( K3bDevice::Device* dev )
{
  m_device = 0;
  m_titles.clear();

  //
  // Initialize libdvdread
  //
  dvd_reader_t* dvdReaderT = DVDOpen( TQFile::encodeName(dev->blockDeviceName()) );
  if( !dvdReaderT ) {
    kdDebug() << "(K3bVideoDVD) Could not open device " << dev->blockDeviceName() << endl;
    return false;
  }

  //
  // Read volume id
  //
  char v[33];
  if( DVDUDFVolumeInfo( dvdReaderT, v, 33, 0, 0 ) != 0 &&
      DVDISOVolumeInfo( dvdReaderT, v, 33, 0, 0 ) != 0 ) {
    kdDebug() << "(K3bVideoDVD) Could not read volume info." << endl;
    DVDClose( dvdReaderT );
    return false;
  }
  m_volumeIdentifier = TQString::fromLatin1( v, 32 );

  //
  // Open the VMG info
  //
  ifo_handle_t* vmg = ifoOpen( dvdReaderT, 0 );
  if( !vmg ) {
    kdDebug() << "(K3bVideoDVD) Can't open VMG info." << endl;
    DVDClose( dvdReaderT );
    return false;
  }

  //
  // parse titles
  //
  m_titles.resize( vmg->tt_srpt->nr_of_srpts );
  for( unsigned int i = 0; i < vmg->tt_srpt->nr_of_srpts; ++i ) {
    title_info_t& title = vmg->tt_srpt->title[i];

    //    m_titles[i].m_videoDVD = this;

    //
    // general title info
    //
    m_titles[i].m_titleNum   = i+1;
    m_titles[i].m_numPTTs    = title.nr_of_ptts;
    m_titles[i].m_numAngles  = title.nr_of_angles;
    m_titles[i].m_titleSet   = title.title_set_nr;
    m_titles[i].m_ttn        = title.vts_ttn;

    //
    // Open the title set the current title is a part of
    //
    ifo_handle_t* titleIfo = ifoOpen( dvdReaderT, vmg->tt_srpt->title[i].title_set_nr );
    if( !titleIfo ) {
      kdDebug() << "(K3bVideoDVD) Can't open Title ifo." << endl;
      ifoClose( vmg );
      DVDClose( dvdReaderT );
      return false;
    }

    //
    // Length of this title
    //
    // the program chain number of the first partoftitle of the current title (FIXME: but a title may contain multiple pgcs)
    int pgc_id = titleIfo->vts_ptt_srpt->title[ m_titles[i].ttn() - 1 ].ptt[0].pgcn;
    // (first?) program chain of the first partoftitle of the current title
    pgc_t* cur_pgc = titleIfo->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;

    m_titles[i].m_playbackTime = Time( CONVERT_TIME(cur_pgc->playback_time.hour),
				       CONVERT_TIME(cur_pgc->playback_time.minute),
				       CONVERT_TIME(cur_pgc->playback_time.second),
				       CONVERT_FRAME(cur_pgc->playback_time.frame_u) );

    //
    // Video stream information
    //
    m_titles[i].m_videoStream.m_permittedDf = titleIfo->vtsi_mat->vts_video_attr.permitted_df;
    m_titles[i].m_videoStream.m_displayAspectRatio = titleIfo->vtsi_mat->vts_video_attr.display_aspect_ratio;
    m_titles[i].m_videoStream.m_videoFormat = titleIfo->vtsi_mat->vts_video_attr.video_format;
    m_titles[i].m_videoStream.m_mpegVersion = titleIfo->vtsi_mat->vts_video_attr.mpeg_version;
    m_titles[i].m_videoStream.m_filmMode = titleIfo->vtsi_mat->vts_video_attr.film_mode;
    m_titles[i].m_videoStream.m_letterboxed = titleIfo->vtsi_mat->vts_video_attr.letterboxed;
    m_titles[i].m_videoStream.m_pictureSize = titleIfo->vtsi_mat->vts_video_attr.picture_size;
    m_titles[i].m_videoStream.m_bitRate = titleIfo->vtsi_mat->vts_video_attr.bit_rate;

    //
    // Audio stream information
    //
    m_titles[i].m_audioStreams.resize( titleIfo->vtsi_mat->nr_of_vts_audio_streams );
    for( unsigned int j = 0; j < titleIfo->vtsi_mat->nr_of_vts_audio_streams; ++j ) {
      m_titles[i].m_audioStreams[j].m_format = titleIfo->vtsi_mat->vts_audio_attr[j].audio_format;
      m_titles[i].m_audioStreams[j].m_applicationMode = titleIfo->vtsi_mat->vts_audio_attr[j].application_mode;
      m_titles[i].m_audioStreams[j].m_quantization = titleIfo->vtsi_mat->vts_audio_attr[j].quantization;
      m_titles[i].m_audioStreams[j].m_sampleFrequency = titleIfo->vtsi_mat->vts_audio_attr[j].sample_frequency;
      m_titles[i].m_audioStreams[j].m_codeExtension = titleIfo->vtsi_mat->vts_audio_attr[j].code_extension;
      m_titles[i].m_audioStreams[j].m_multiChannelExt = titleIfo->vtsi_mat->vts_audio_attr[j].multichannel_extension;
      m_titles[i].m_audioStreams[j].m_channels = titleIfo->vtsi_mat->vts_audio_attr[j].channels+1;
      if( titleIfo->vtsi_mat->vts_audio_attr[j].lang_type == 1 )
	m_titles[i].m_audioStreams[j].m_langCode.sprintf( "%c%c", 
							  titleIfo->vtsi_mat->vts_audio_attr[j].lang_code>>8, 
							  titleIfo->vtsi_mat->vts_audio_attr[j].lang_code & 0xff );
      else
	m_titles[i].m_audioStreams[j].m_langCode = TQString();
    }

    //
    // SubPicture stream information
    //
    m_titles[i].m_subPictureStreams.resize( titleIfo->vtsi_mat->nr_of_vts_subp_streams );
    for( unsigned int j = 0; j < titleIfo->vtsi_mat->nr_of_vts_subp_streams; ++j ) {
      m_titles[i].m_subPictureStreams[j].m_codeMode = titleIfo->vtsi_mat->vts_subp_attr[j].code_mode;
      m_titles[i].m_subPictureStreams[j].m_codeExtension = titleIfo->vtsi_mat->vts_subp_attr[j].code_extension;
      if( titleIfo->vtsi_mat->vts_subp_attr[j].type == 1 )
	m_titles[i].m_subPictureStreams[j].m_langCode.sprintf( "%c%c", 
							       titleIfo->vtsi_mat->vts_subp_attr[j].lang_code>>8, 
							       titleIfo->vtsi_mat->vts_subp_attr[j].lang_code & 0xff );
      else
	m_titles[i].m_subPictureStreams[j].m_langCode = TQString();
    }

    //
    // add chapter info
    //
    m_titles[i].m_ptts.resize( m_titles[i].numPTTs() );
    for( unsigned int j = 0; j < m_titles[i].numPTTs(); ++j ) {
      m_titles[i].m_ptts[j].m_pttNum = j+1;
      m_titles[i].m_ptts[j].m_playbackTime = Time( CONVERT_TIME(cur_pgc->cell_playback[j].playback_time.hour),
						   CONVERT_TIME(cur_pgc->cell_playback[j].playback_time.minute),
						   CONVERT_TIME(cur_pgc->cell_playback[j].playback_time.second),
						   CONVERT_FRAME(cur_pgc->cell_playback[j].playback_time.frame_u) );
      m_titles[i].m_ptts[j].m_firstSector = cur_pgc->cell_playback[j].first_sector;
      m_titles[i].m_ptts[j].m_lastSector = cur_pgc->cell_playback[j].last_sector;
    }

    ifoClose( titleIfo );
  }

  ifoClose( vmg );
  DVDClose( dvdReaderT );

  //
  // Setting the device makes this a valid instance
  //
  m_device = dev;

  return true;
}


const K3bVideoDVD::Title& K3bVideoDVD::VideoDVD::title( unsigned int num ) const
{
  return m_titles[num];
}


const K3bVideoDVD::Title& K3bVideoDVD::VideoDVD::operator[]( unsigned int num ) const
{
  return title( num );
}


void K3bVideoDVD::VideoDVD::debug() const
{
  kdDebug() << "VideoDVD information:" << endl
	    << "=====================" << endl
	    << "Volume ID: " << volumeIdentifier() << endl << endl;

  for( unsigned int i = 0; i < numTitles(); ++i ) {
    kdDebug() << "Title " << title(i).titleNumber() << " (" << title(i).playbackTime().toString() << ")" << endl
	      << "   Chapters: " << title(i).numPTTs() << endl
	      << "   Angles:   " << title(i).numAngles() << endl
	      << "   VTS,TTN:  " << title(i).titleSet() << "," << title(i).ttn() << endl
	      << "   Audio Streams:" << endl;
    for( unsigned int j = 0; j < title(i).numAudioStreams(); ++j )
      kdDebug() << "      " << title(i).audioStream(j).langCode() << ": " 
		<< audioFormatString( title(i).audioStream(j).format() ) << ", "
		<< audioCodeExtensionString( title(i).audioStream(j).codeExtension() ) << endl;
    kdDebug() << "   SubPicture Streams:" << endl;
    for( unsigned int j = 0; j < title(i).numSubPictureStreams(); ++j )
      kdDebug() << "      " << title(i).subPictureStream(j).langCode() << ": " 
		<< subPictureCodeModeString( title(i).subPictureStream(j).codeMode() ) << ", "
		<< subPictureCodeExtensionString( title(i).subPictureStream(j).codeExtension() ) << endl;
  }
}


TQString K3bVideoDVD::audioFormatString( int format )
{
  switch( format ) {
  case AUDIO_FORMAT_AC3:
    return i18n("AC3");
  case AUDIO_FORMAT_MPEG1:
    return i18n("MPEG1");
  case AUDIO_FORMAT_MPEG2EXT:
    return i18n("MPEG2 Extended");
  case AUDIO_FORMAT_LPCM:
    return i18n("LPCM");
  case AUDIO_FORMAT_DTS:
    return i18n("DTS");
  default:
    return i18n("unknown audio format");
  }
}


TQString K3bVideoDVD::audioCodeExtensionString( int ext )
{
  switch( ext ) {
  case AUDIO_CODE_EXT_UNSPECIFIED:
    return i18n("Unspecified");
  case AUDIO_CODE_EXT_NORMAL:
    return i18n("Normal");
  case AUDIO_CODE_EXT_VISUALLY_IMPAIRED:
    return i18n("For the visually impaired");
  case AUDIO_CODE_EXT_DIR_COMMENTS_1:
    return i18n("Director's comments 1");
  case AUDIO_CODE_EXT_DIR_COMMENTS_2:
    return i18n("Director's comments 2");
  default:
    return i18n("unknown audio code extension");
  }
}


TQString K3bVideoDVD::subPictureCodeModeString( int mode )
{
  switch( mode ) {
  case SUBPIC_CODE_MODE_RLE:
    return i18n("RLE");
  case SUBPIC_CODE_MODE_EXT:
    return i18n("Extended");
  default:
    return i18n("unknown coding mode");
  }
}


TQString K3bVideoDVD::subPictureCodeExtensionString( int ext )
{
  switch( ext ) {
  case SUBPIC_CODE_EXT_UNSPECIFIED:
    return i18n("Unspecified");
  case SUBPIC_CODE_EXT_CAPTION_NORMAL_SIZE:
    return i18n("Caption with normal size character");
  case SUBPIC_CODE_EXT_CAPTION_BIGGER_SIZE:
    return i18n("Caption with bigger size character");
  case SUBPIC_CODE_EXT_CAPTION_FOR_CHILDREN:
    return i18n("Caption for children");
  case SUBPIC_CODE_EXT_CLOSED_CAPTION_NORMAL_SIZE:
    return i18n("Closed caption with normal size character");
  case SUBPIC_CODE_EXT_CLOSED_CAPTION_BIGGER_SIZE:
    return i18n("Closed caption with bigger size character");
  case SUBPIC_CODE_EXT_CLOSED_CAPTION_FOR_CHILDREN:
    return i18n("Closed caption for children");
  case SUBPIC_CODE_EXT_FORCED_CAPTION:
    return i18n("Forced caption");
  case SUBPIC_CODE_EXT_DIR_COMMENTS_NORMAL_SIZE:
    return i18n("Director's comments with normal size characters");
  case SUBPIC_CODE_EXT_DIR_COMMENTS_BIGGER_SIZE:
    return i18n("Director's comments with bigger size characters");
  case SUBPIC_CODE_EXT_DIR_COMMENTS_FOR_CHILDREN:
    return i18n("Director's comments for children");
  default:
    return i18n("unknown code extension");
  }
}