summaryrefslogtreecommitdiffstats
path: root/HBCommon.cpp
blob: 9a88cb52988e123de11a93373cc436ed964405ef (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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/* $Id: HBCommon.cpp,v 1.6 2003/08/24 21:56:03 titer Exp $ */

#include "HBCommon.h"
#include "HBFifo.h"
#include "HBMpegDemux.h"
#include "HBPictureWin.h"
#include "HBWindow.h"

#include <Application.h>

#include <dvdread/ifo_types.h>
#include <dvdplay/dvdplay.h>
#include <dvdplay/info.h>
#include <dvdplay/state.h>
#include <dvdplay/nav.h>

extern "C" {
#include <mpeg2dec/mpeg2.h>
}

void Log( char * log, ... )
{
    char * string = (char*) malloc( 1024 );
    
    /* Show the time */
    time_t _now = time( NULL );
    struct tm * now = localtime( &_now );
    sprintf( string, "[%02d:%02d:%02d] ",
             now->tm_hour, now->tm_min, now->tm_sec );

    /* Convert the message to a string */
    va_list args;
    va_start( args, log );
    int ret = vsnprintf( string + 11, 1011, log, args );
    va_end( args );
    
    /* Add the end of line */
    string[ret+11] = '\n';
    string[ret+12] = '\0';
    
    /* Send this to the be_app */
    /* We do this so we are sure that only one message is printed
       at a time */
    BMessage * message = new BMessage( PRINT_MESSAGE );
    message->AddPointer( "string", string );
    be_app->PostMessage( message );
    delete message;
}

void Status( char * text, float pos, int mode )
{
    char * textCopy = strdup( text );
    BMessage * message = new BMessage( CHANGE_STATUS );
    message->AddPointer( "text", textCopy );
    message->AddFloat( "pos", pos );
    message->AddInt32( "mode", mode );
    be_app->PostMessage( message );
    delete message;
}

/* Get a readable language description from the code */
iso639_lang_t languages[] =
{ { "Afar", "", "aa" },
  { "Abkhazian", "", "ab" },
  { "Afrikaans", "", "af" },
  { "Albanian", "", "sq" },
  { "Amharic", "", "am" },
  { "Arabic", "", "ar" },
  { "Armenian", "", "hy" },
  { "Assamese", "", "as" },
  { "Avestan", "", "ae" },
  { "Aymara", "", "ay" },
  { "Azerbaijani", "", "az" },
  { "Bashkir", "", "ba" },
  { "Basque", "", "eu" },
  { "Belarusian", "", "be" },
  { "Bengali", "", "bn" },
  { "Bihari", "", "bh" },
  { "Bislama", "", "bi" },
  { "Bosnian", "", "bs" },
  { "Breton", "", "br" },
  { "Bulgarian", "", "bg" },
  { "Burmese", "", "my" },
  { "Catalan", "", "ca" },
  { "Chamorro", "", "ch" },
  { "Chechen", "", "ce" },
  { "Chinese", "", "zh" },
  { "Church Slavic", "", "cu" },
  { "Chuvash", "", "cv" },
  { "Cornish", "", "kw" },
  { "Corsican", "", "co" },
  { "Czech", "", "cs" },
  { "Danish", "Dansk", "da" },
  { "Dutch", "Nederlands", "nl" },
  { "Dzongkha", "", "dz" },
  { "English", "English", "en" },
  { "Esperanto", "", "eo" },
  { "Estonian", "", "et" },
  { "Faroese", "", "fo" },
  { "Fijian", "", "fj" },
  { "Finnish", "Suomi", "fi" },
  { "French", "Francais", "fr" },
  { "Frisian", "", "fy" },
  { "Georgian", "", "ka" },
  { "German", "Deutsch", "de" },
  { "Gaelic (Scots)", "", "gd" },
  { "Irish", "", "ga" },
  { "Gallegan", "", "gl" },
  { "Manx", "", "gv" },
  { "Greek, Modern ()", "", "el" },
  { "Guarani", "", "gn" },
  { "Gujarati", "", "gu" },
  { "Hebrew", "", "he" },
  { "Herero", "", "hz" },
  { "Hindi", "", "hi" },
  { "Hiri Motu", "", "ho" },
  { "Hungarian", "Magyar", "hu" },
  { "Icelandic", "Islenska", "is" },
  { "Inuktitut", "", "iu" },
  { "Interlingue", "", "ie" },
  { "Interlingua", "", "ia" },
  { "Indonesian", "", "id" },
  { "Inupiaq", "", "ik" },
  { "Italian", "Italiano", "it" },
  { "Javanese", "", "jv" },
  { "Japanese", "", "ja" },
  { "Kalaallisut (Greenlandic)", "", "kl" },
  { "Kannada", "", "kn" },
  { "Kashmiri", "", "ks" },
  { "Kazakh", "", "kk" },
  { "Khmer", "", "km" },
  { "Kikuyu", "", "ki" },
  { "Kinyarwanda", "", "rw" },
  { "Kirghiz", "", "ky" },
  { "Komi", "", "kv" },
  { "Korean", "", "ko" },
  { "Kuanyama", "", "kj" },
  { "Kurdish", "", "ku" },
  { "Lao", "", "lo" },
  { "Latin", "", "la" },
  { "Latvian", "", "lv" },
  { "Lingala", "", "ln" },
  { "Lithuanian", "", "lt" },
  { "Letzeburgesch", "", "lb" },
  { "Macedonian", "", "mk" },
  { "Marshall", "", "mh" },
  { "Malayalam", "", "ml" },
  { "Maori", "", "mi" },
  { "Marathi", "", "mr" },
  { "Malay", "", "ms" },
  { "Malagasy", "", "mg" },
  { "Maltese", "", "mt" },
  { "Moldavian", "", "mo" },
  { "Mongolian", "", "mn" },
  { "Nauru", "", "na" },
  { "Navajo", "", "nv" },
  { "Ndebele, South", "", "nr" },
  { "Ndebele, North", "", "nd" },
  { "Ndonga", "", "ng" },
  { "Nepali", "", "ne" },
  { "Norwegian", "Norsk", "no" },
  { "Norwegian Nynorsk", "", "nn" },
  { "Norwegian Bokmål", "", "nb" },
  { "Chichewa; Nyanja", "", "ny" },
  { "Occitan (post 1500); Provençal", "", "oc" },
  { "Oriya", "", "or" },
  { "Oromo", "", "om" },
  { "Ossetian; Ossetic", "", "os" },
  { "Panjabi", "", "pa" },
  { "Persian", "", "fa" },
  { "Pali", "", "pi" },
  { "Polish", "", "pl" },
  { "Portuguese", "Portugues", "pt" },
  { "Pushto", "", "ps" },
  { "Quechua", "", "qu" },
  { "Raeto-Romance", "", "rm" },
  { "Romanian", "", "ro" },
  { "Rundi", "", "rn" },
  { "Russian", "", "ru" },
  { "Sango", "", "sg" },
  { "Sanskrit", "", "sa" },
  { "Serbian", "", "sr" },
  { "Croatian", "Hrvatski", "hr" },
  { "Sinhalese", "", "si" },
  { "Slovak", "", "sk" },
  { "Slovenian", "", "sl" },
  { "Northern Sami", "", "se" },
  { "Samoan", "", "sm" },
  { "Shona", "", "sn" },
  { "Sindhi", "", "sd" },
  { "Somali", "", "so" },
  { "Sotho, Southern", "", "st" },
  { "Spanish", "Espanol", "es" },
  { "Sardinian", "", "sc" },
  { "Swati", "", "ss" },
  { "Sundanese", "", "su" },
  { "Swahili", "", "sw" },
  { "Swedish", "Svenska", "sv" },
  { "Tahitian", "", "ty" },
  { "Tamil", "", "ta" },
  { "Tatar", "", "tt" },
  { "Telugu", "", "te" },
  { "Tajik", "", "tg" },
  { "Tagalog", "", "tl" },
  { "Thai", "", "th" },
  { "Tibetan", "", "bo" },
  { "Tigrinya", "", "ti" },
  { "Tonga (Tonga Islands)", "", "to" },
  { "Tswana", "", "tn" },
  { "Tsonga", "", "ts" },
  { "Turkish", "", "tr" },
  { "Turkmen", "", "tk" },
  { "Twi", "", "tw" },
  { "Uighur", "", "ug" },
  { "Ukrainian", "", "uk" },
  { "Urdu", "", "ur" },
  { "Uzbek", "", "uz" },
  { "Vietnamese", "", "vi" },
  { "Volapük", "", "vo" },
  { "Welsh", "", "cy" },
  { "Wolof", "", "wo" },
  { "Xhosa", "", "xh" },
  { "Yiddish", "", "yi" },
  { "Yoruba", "", "yo" },
  { "Zhuang", "", "za" },
  { "Zulu", "", "zu" },
  { NULL, NULL, NULL } };

char * LanguageForCode( int code )
{
    char codeString[2];
    codeString[0] = ( code >> 8 ) & 0xFF;
    codeString[1] = code & 0xFF;
    
    iso639_lang_t * lang;
    for( lang = languages; lang->engName; lang++ )
    {
        if( !strncmp( lang->iso639_1, codeString, 2 ) )
        {
            if( *lang->nativeName )
                return lang->nativeName;

            return lang->engName;
        }
    }
    
    return "Unknown";
}

HBVolumeInfo::HBVolumeInfo( char * name, char * device )
    : BMenuItem( "", new BMessage( VOLUME_SELECTED ) )
{
    fInitOK    = false;
    fName      = strdup( name );
    fDevice    = strdup( device );
    fTitleList = new BList();

    SetLabel( fName );
    
    /* Open the device */
    dvdplay_ptr vmg;
    vmg = dvdplay_open( device, NULL, NULL );
    if( !vmg )
    {
        Log( "VolumeInfo::DetectTitles: dvdplay_open() failed (%s)",
             device );
        return;
    }
    
    /* Detect titles */
    HBTitleInfo * titleInfo;
    for( int i = 0; i < dvdplay_title_nr( vmg ); i++ )
    {
        Log( "HBVolumeInfo : new title (%d)", i + 1 );
        
        char statusText[128]; memset( statusText, 0, 128 );
        snprintf( statusText, 128,
                  "Checking DVD volumes (%s, title %d)...",
                  fName, i + 1 );
        Status( statusText, 0.0, ENABLE_DETECTING );

        titleInfo = new HBTitleInfo( vmg, i + 1, device );
        
        if( !titleInfo->InitCheck() )
        {
            delete titleInfo;
            continue;
        }
        
        fTitleList->AddItem( titleInfo );
    }
    
    dvdplay_close( vmg );
    
    if( fTitleList->CountItems() > 0 )
    {
        fInitOK = true;
    }

}

HBVolumeInfo::~HBVolumeInfo()
{
    free( fName );
    free( fDevice );
    
    HBTitleInfo * titleInfo;
    while( ( titleInfo = (HBTitleInfo*) fTitleList->ItemAt( 0 ) ) )
    {
        fTitleList->RemoveItem( titleInfo );
        delete titleInfo;
    }
    delete fTitleList;
}

bool HBVolumeInfo::InitCheck()
{
    return fInitOK;
}

HBTitleInfo::HBTitleInfo( dvdplay_ptr vmg, int index, char * device )
    : BMenuItem( "", new BMessage( TITLE_SELECTED ) )
{
    fInitOK         = false;
    fDevice         = strdup( device );
    fIndex          = index;

    fAudioInfoList1 = new BList();
    fAudioInfoList2 = new BList();
    fPSFifo         = NULL;
    fMpeg2Fifo      = NULL;
    fRawFifo        = NULL;
    fMpeg4Fifo      = NULL;

    dvdplay_start( vmg, fIndex );
    
    /* Length */
    fLength = dvdplay_title_time( vmg );
    
    /* Discard titles under 60 seconds */
    if( fLength < 60 )
    {
        Log( "HBTitleInfo : skipping title %d (length = %lld sec)",
             index, fLength );
        return;
    }

    char label[1024]; memset( label, 0, 1024 );
    sprintf( label, "Title %d (%02lld:%02lld:%02lld)",
             index, fLength / 3600, ( fLength % 3600 ) / 60,
             fLength % 60 );
    SetLabel( label );
    
    /* Detect languages */
    int audio_nr, audio;
    dvdplay_audio_info( vmg, &audio_nr, &audio );
    
    audio_attr_t * attr;
    HBAudioInfo * audioInfo;
    for( int i = 0; i < audio_nr; i++ )
    {
        int id = dvdplay_audio_id( vmg, i );
        if( id > 0 )
        {
            Log( "HBTitleInfo : new language (%x)", id );
            attr = dvdplay_audio_attr( vmg, i );
            audioInfo = new HBAudioInfo( id, LanguageForCode( attr->lang_code ) );
            fAudioInfoList1->AddItem( audioInfo );
        }
    }
    
    /* Discard titles with no audio tracks */
    if( !fAudioInfoList1->CountItems() )
    {
        Log( "HBTitleInfo : skipping title %d (no audio track)", index );
        return;
    }
    
    /* Add a dummy 'None' language */
    audioInfo = new HBAudioInfo( 0, "None" );
    fAudioInfoList1->AddItem( audioInfo );
    
    /* Duplicate the audio list */
    for( int i = 0; i < fAudioInfoList1->CountItems(); i++ )
    {
        audioInfo = (HBAudioInfo*) fAudioInfoList1->ItemAt( i );
        fAudioInfoList2->AddItem( new HBAudioInfo( audioInfo ) );
    }
    
    /* Decode a few pictures so the user can crop/resize it */
    int titleFirst = dvdplay_title_first ( vmg );
    int titleEnd   = dvdplay_title_end( vmg );

    /* Kludge : libdvdplay wants we to read a first block before seeking */
    uint8_t dummyBuf[2048];
    dvdplay_read( vmg, dummyBuf, 1 );

    for( int i = 0; i < 10; i++ )
    {
        dvdplay_seek( vmg, ( i + 1 ) * ( titleEnd - titleFirst ) / 11 ) ;
        if( !DecodeFrame( vmg, i ) )
        {
            Log( "HBTitleInfo::HBTitleInfo : could not decode frame %d", i );
            return;
        }
    }
    
    fPictureWin = new HBPictureWin( this );

    fInitOK = true;
}

HBTitleInfo::~HBTitleInfo()
{
    HBAudioInfo * audioInfo;
    
    while( ( audioInfo = (HBAudioInfo*) fAudioInfoList1->ItemAt( 0 ) ) )
    {
        fAudioInfoList1->RemoveItem( audioInfo );
        delete audioInfo;
    }
    delete fAudioInfoList1;
    
    while( ( audioInfo = (HBAudioInfo*) fAudioInfoList2->ItemAt( 0 ) ) )
    {
        fAudioInfoList2->RemoveItem( audioInfo );
        delete audioInfo;
    }
    delete fAudioInfoList2;
}

bool HBTitleInfo::DecodeFrame( dvdplay_ptr vmg, int i )
{
    /* Init libmpeg2 */
    mpeg2dec_t         * handle = mpeg2_init();
    const mpeg2_info_t * info   = mpeg2_info( handle );
    mpeg2_state_t        state;
    
    BList    * esBufferList = NULL;
    HBBuffer * psBuffer     = NULL;
    HBBuffer * esBuffer     = NULL;
    
    for( ;; )
    {
        state = mpeg2_parse( handle );
        
        if( state == STATE_BUFFER )
        {
            /* Free the previous buffer */
            if( esBuffer )
            {
                delete esBuffer;
                esBuffer = NULL;
            }
            
            /* Get a new one */
            while( !esBuffer )
            {
                while( !esBufferList )
                {
                    psBuffer = new HBBuffer( DVD_VIDEO_LB_LEN );
                    if( dvdplay_read( vmg, psBuffer->fData, 1 ) != 1 )
                    {
                        Log( "dvdplay_read failed" );
                    }
                    esBufferList = PStoES( psBuffer );
                }

                esBuffer = (HBBuffer*) esBufferList->ItemAt( 0 );
                esBufferList->RemoveItem( esBuffer );
                if( !esBufferList->CountItems() )
                {
                    delete esBufferList;
                    esBufferList = NULL;
                }
                
                if( esBuffer->fStreamId != 0xE0 )
                {
                    delete esBuffer;
                    esBuffer = NULL;
                }
            }
            
            /* Feed libmpeg2 */
            mpeg2_buffer( handle, esBuffer->fData,
                          esBuffer->fData + esBuffer->fSize );
        }
        else if( state == STATE_SEQUENCE )
        {
            /* Get size & framerate info */
            fInWidth     = info->sequence->width;
            fInHeight    = info->sequence->height;
            fPixelWidth  = info->sequence->pixel_width;
            fPixelHeight = info->sequence->pixel_height;
            fPictures[i] = (uint8_t*) malloc( 3 * fInWidth * fInHeight / 2 );
            fRate        = 27000000;
            fScale       = info->sequence->frame_period;
        }
        else if( ( state == STATE_SLICE || state == STATE_END ) &&
                 ( info->display_fbuf ) &&
                 ( info->display_picture->flags & PIC_MASK_CODING_TYPE )
                     == PIC_FLAG_CODING_TYPE_I )
        {
            /* Copy it */
            /* TODO : make libmpeg2 write directly in our buffer */
            memcpy( fPictures[i],
                    info->display_fbuf->buf[0],
                    fInWidth * fInHeight );
            memcpy( fPictures[i] + fInWidth * fInHeight,
                    info->display_fbuf->buf[1],
                    fInWidth * fInHeight / 4 );
            memcpy( fPictures[i] + 5 * fInWidth * fInHeight / 4,
                    info->display_fbuf->buf[2],
                    fInWidth * fInHeight / 4 );
            break;
        }
        else if( state == STATE_INVALID )
        {
            /* Reset libmpeg2 */
            mpeg2_close( handle );
            handle = mpeg2_init();
        }
    }
    
    mpeg2_close( handle );
    
    return true;
}

bool HBTitleInfo::InitCheck()
{
    return fInitOK;
}

/* Audio track */
HBAudioInfo::HBAudioInfo( int id, char * description )
    : BMenuItem( "", new BMessage( LANGUAGE_SELECTED ) )
{
    fId            = id;
    fOutSampleRate = 44100;
    
    fAc3Fifo = NULL;
    fRawFifo = NULL;
    fMp3Fifo = NULL;
    
    SetLabel( description );
}

HBAudioInfo::HBAudioInfo( HBAudioInfo * audioInfo )
    : BMenuItem( "", new BMessage( LANGUAGE_SELECTED ) )
{
    fId            = audioInfo->fId;
    fInSampleRate  = audioInfo->fInSampleRate;
    fOutSampleRate = audioInfo->fOutSampleRate;
    fInBitrate     = audioInfo->fInBitrate;
    fOutBitrate    = audioInfo->fOutBitrate;
    
    fAc3Fifo = NULL;
    fRawFifo = NULL;
    fMp3Fifo = NULL;
    
    SetLabel( audioInfo->Label() );
}

HBAudioInfo::~HBAudioInfo()
{
}