File: FFmpegReader.m

package info (click to toggle)
lynkeos.app 3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,740 kB
  • sloc: objc: 40,528; ansic: 811; cpp: 150; sh: 68; makefile: 27
file content (690 lines) | stat: -rw-r--r-- 19,650 bytes parent folder | download
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
//
//  Lynkeos
//  $Id$
//
//  Based on ffmpeg_access.c by Christophe JALADY.
//  Created by Jean-Etienne LAMIAUD on Mon Jun 27 2005.
//
//  Copyright (c) 2004-2005. Christophe JALADY
//  Copyright (c) 2005-2023. Jean-Etienne LAMIAUD
//
// 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.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// 

#if !GNUSTEP
#include <AVFoundation/AVFoundation.h>
#endif
#import <AppKit/NSGraphics.h>

#include <libavutil/imgutils.h>

#include <LynkeosCore/LynkeosProcessing.h>
#include "MyCachePrefs.h"

#include "FFmpegReader.h"

#ifdef DOUBLE_PIXELS
   #error This reader is not able to run with double precision reals
#endif

#define K_TIME_PAGE_SIZE 256

/*!
 * @abstract ObjC container for a FFMpeg AVFrame
 */
@interface MyAVFrameContainer : NSObject
{
@public
   AVFrame *_frame; //!< The AVFrame we wrap around
}
//! Dedicated initializer
- (id) initWithAVFrame:(AVFrame*)frame ;
@end

@implementation MyAVFrameContainer
- (id) initWithAVFrame:(AVFrame*)frame
{
   if ( (self = [self init]) != nil )
      _frame = frame;
   return( self );
}

- (void) dealloc
{
   av_frame_free(&_frame);
   [super dealloc];
}
@end

/*!
 * @category FFmpegReader(Private)
 * @abstract Internals of the FFmpeg reader
 * @discussion This code is supposed to seek to the nearest previous key frame
 *   if needed, then to step through the frames up to the required frame.
 *   Due to some unpredictable behaviour in the key frames reporting and their
 *   time tagging : when a problem is detected, the key frame control structure
 *   is filled with only the first sequence frame. It is much slower, but (knock
 *   on wood) works.
 * @ingroup FileAccess
 */
@interface FFmpegReader(Private)

/*!
 * @method nextFrame
 * @abstract Access the next frame in the movie.
 * @result Wether a frame was succesfully read
 */
- (BOOL) nextFrame ;

/*!
 * @method getFrame:
 * @abstract Get the needed frame
 * @param index The index of the frame to get
 */
- (AVFrame*) getFrame :(u_long) index ;

@end

@implementation FFmpegReader(Private)

- (BOOL) nextFrame
{
   int ret;
   BOOL frameFinished = NO;

   // Decode packets until we have decoded a complete frame
   while ( !frameFinished )
   {
      if (_decoderState == DataNeeded)
      {
         // Read the next packet, skipping all packets that aren't for this stream
         do
         {
            // Free old packet content
            av_packet_unref( _packet );

            // Read new packet
            ret = av_read_frame(_pFormatCtx, _packet);

         } while( ret >= 0 && (_packet->stream_index != _videoStream ) );

         if ( ret < 0 )
         {
            // Obviously, the stream must be at end
            _decoderState = EndOfFile;
         }
      }

      // Decode the next chunk of data
      if (_decoderState != Flushing)
      {
         AVPacket *pk = NULL;

         switch (_decoderState )
         {
            case DataNeeded:
            case DataRepeat:
               pk = _packet;
               break;
            case EndOfFile:
               // We will send a *last* NULL packet
               _decoderState = Flushing;
               break;
            default:
               break;
         }
         ret = avcodec_send_packet(_pCodecCtx, pk);

         if ( ret < 0 )
         {
            if (errno == EAGAIN)
            {
               _decoderState = DataRepeat;
               ret = 0;
            }
            else
            {
               NSLog( @"Error while decoding frame" );
            }
         }
         else
         {
            if (_decoderState == DataRepeat)
               _decoderState = DataNeeded;
         }
      }

      av_frame_unref(_pCurrentFrame);
      ret = avcodec_receive_frame(_pCodecCtx, _pCurrentFrame);

      if (ret == 0)
         frameFinished = YES;
      else
      {
         if (ret == AVERROR_EOF)
         {
//            NSLog(@"Finished to decode stream");
            break;
         }
//       else
//          NSLog(@"FFMpeg error : %s", av_err2str(ret));
      }
   }

   if ( frameFinished )
      _nextIndex ++;

   return( frameFinished );
}

- (AVFrame*) getFrame :(u_long) index
{
   NSString *key = [NSString stringWithFormat:@"%s&%06ld", _pFormatCtx->url, index];
   LynkeosObjectCache *movieCache = [LynkeosObjectCache movieCache];
   MyAVFrameContainer *pix;

   if ( movieCache != nil &&
        (pix = (MyAVFrameContainer*)[movieCache getObjectForKey:key]) != nil )
      return( pix->_frame );

   int ret;
   BOOL success = NO;

   // Do not move if the frame already read is asked
   if ( index == (_nextIndex - 1) )
      return( _pCurrentFrame );

//   for( ;; )
//   {
      // Go to the previous key frame if needed
      if ( index < _nextIndex
          || _times[index].keyFrame != _times[_nextIndex].keyFrame )
      {
         // Reset the decoder
         if ( _packet->data != NULL )
            av_packet_unref( _packet );
         _packet->size = 0;
         avcodec_flush_buffers(_pCodecCtx);
         _decoderState = DataNeeded;

//         NSLog(@"Seeking FFMpeg stream to frame %lu", _times[index].keyFrame);
         ret = av_seek_frame( _pFormatCtx, _videoStream,
                             _times[index].timestamp,
                             AVSEEK_FLAG_BACKWARD );

         if ( ret == 0 )
            _nextIndex = _times[index].keyFrame;
         else
            _nextIndex = _numberOfFrames + 1;
      }
      else
         ret = 0;

      if ( ret == 0 )
      {
         success = YES;
         while ( _nextIndex <= index && success )
         {
            success = [self nextFrame];

            // Keep the last frames in cache for list processing
            if ( success && ( _nextIndex == index+1
                              || (movieCache != nil && _nextIndex+numberOfCpus > index) ) )
            {
               if ( movieCache != nil )
               {
                  AVFrame *frameCopy = av_frame_alloc();
                  av_frame_ref(frameCopy, _pCurrentFrame);
                  [movieCache setObject:
                     [[[MyAVFrameContainer alloc] initWithAVFrame: frameCopy] autorelease]
                                 forKey:
                     [NSString stringWithFormat:@"%s&%06ld", _pFormatCtx->url,_nextIndex-1]];
               }
            }
         }
      }
      else
         NSLog( @"Seek to frame failed" );

//      if ( (! success || ret <= 0)
//          && (_times[index].keyFrame != 0 || _times[index].timestamp != 0) )
//      { // Hack to try to read buggy sequences (or which makes FFmpeg bug ;o)
//         unsigned int i;
//         NSLog( @"Trying to revert to sequential read" );
//         for( i = 1; i < _numberOfFrames; i++ )
//         {
//            _times[i].keyFrame = 0;
//            _times[i].timestamp = 0;
//         }
//         _nextIndex = _numberOfFrames;
//      }
//      else
//         // Succeeded or hopeless
//         break;
//   }

   return( _pCurrentFrame );
}

@end

@implementation FFmpegReader

+ (void) load  // Force the runtime to load the class
{
}

+ (void) lynkeosFileTypes:(NSArray**)fileTypes
{
   const AVInputFormat *fmt;
   void *opaque = NULL;

   // Start the list with obvious file types, in case the introspection would fail
#if !GNUSTEP
   *fileTypes = [NSMutableArray arrayWithObjects: @"avi",@"mpeg",@"mpg",@"mp4",@"wmv",@"mov",
                 AVFileTypeQuickTimeMovie, AVFileTypeMPEG4, AVFileTypeAppleM4V, nil];
#else
   *fileTypes = [NSMutableArray arrayWithObjects:
                                  @"avi",@"mpeg",@"mpg",@"mp4",@"wmv",@"mov",nil];
#endif
   while ((fmt = av_demuxer_iterate(&opaque)))
   {
      if (fmt->extensions != NULL)
      {
         // Filtering only the formats with video capacity did not work... get them all
         NSString *extensions = [NSString stringWithUTF8String:fmt->extensions];
         NSArray
#if !GNUSTEP
<NSString *>
#endif
           *extList = [extensions componentsSeparatedByString:@","];
         NSEnumerator *extEnum = [extList objectEnumerator];
         NSString *ext;
         while( (ext = [extEnum nextObject]) != nil )
         {
            if (![*fileTypes containsObject:ext])
               [(NSMutableArray*)*fileTypes addObject:ext];
         }
      }
   }
}

- (id) init
{
   self = [super init];
   if ( self != nil )
   {
      _width = 0;
      _height = 0;
      _numberOfPlanes = 0;
      _pFormatCtx = NULL;
      _pCodecCtx = NULL;
      _pCurrentFrame = NULL;
      _displayConverter = NULL;
      _procConverter = NULL;
      _convBuffer = NULL;
      _bufLineLength = 0;
      _videoStream = -1;
      _packet = av_packet_alloc();
      _decoderState = DataNeeded;
      _numberOfFrames = 0;
      _nextIndex = 0;
      _mutex = [[NSLock alloc] init];
      _times = NULL;
   }
   return( self );
}

- (id) initWithURL:(NSURL*)url
{
   unsigned int       i;
   int                ret;
   const AVCodec           *pCodec;
   AVCodecParameters *codecParams;
   u_long             arraySize;
   BOOL               validFrame;
   int64_t         /* startTime, timestamp,*/ keyTimestamp;
   u_long             keyIndex;

   self = [self init];

   if ( self != nil )
   {
      // Open video file
      ret = avformat_open_input( &_pFormatCtx,
                                [[url path] fileSystemRepresentation],
//                                [[url absoluteString] UTF8String],
                                NULL, NULL );
      if ( ret < 0 )
      {
         NSLog( @"Could not open file %@\n%s", [url absoluteString], av_err2str(ret) );
         [self release];
         return( nil );
      }

      // Retrieve stream information
      ret = avformat_find_stream_info(_pFormatCtx, NULL);
      if ( ret < 0 )
      {
         NSLog( @"Could not find any stream info");
         [self release];
         return( nil );
      }

      // Find the first video stream
      _videoStream = -1;
      for ( i = 0; i < _pFormatCtx->nb_streams; i++ )
      {
         if( _pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO )
         {
            _videoStream = i;
            codecParams = _pFormatCtx->streams[i]->codecpar;
            break;
         }
      }

      if( _videoStream == -1 )
      {
         NSLog( @"Could not find a video stream");
         [self release];
         return( nil );
      }

      // Find the decoder for the video stream
      pCodec = avcodec_find_decoder(codecParams->codec_id);
      if ( pCodec == NULL )
      {
         NSLog( @"Codec not found");
         [self release];
         return( nil );
      }

      // Allocate a codec context for the video stream
      _pCodecCtx = avcodec_alloc_context3(pCodec);

      // Open codec
      if (   avcodec_parameters_to_context(_pCodecCtx, codecParams) < 0
          || avcodec_open2(_pCodecCtx, pCodec, NULL) < 0)
      {
         NSLog( @"Can't open the codec" );
         [self release];
         return( nil );
      }

      AVRational aspect = _pCodecCtx->sample_aspect_ratio;
      if (aspect.num == 0 || aspect.den == 0)
      {
         _width = _pCodecCtx->width;
         _height = _pCodecCtx->height;
      }
      else if (aspect.num >= aspect.den)
      {
         _width = (_pCodecCtx->width * aspect.num) / aspect.den;
         _height = _pCodecCtx->height;
      }
      else
      {
         _width = _pCodecCtx->width;
         _height = (_pCodecCtx->height * aspect.num) / aspect.den;
      }
      const AVPixFmtDescriptor *fmtDesc = av_pix_fmt_desc_get(_pCodecCtx->pix_fmt);
      if (fmtDesc == NULL)
      {
         NSLog( @"Unknown pixel format" );
         [self release];
         return( nil );
      }
      _numberOfPlanes = (fmtDesc->nb_components == 1 ? 1 : 3);

      // Allocate video frame
      _pCurrentFrame = av_frame_alloc();

      // Allocate a RGB converter for NSImage conversion
      _displayConverter = sws_getContext(_pCodecCtx->width, _pCodecCtx->height,
                                         _pCodecCtx->pix_fmt,
                                         _width, _height,
                                         AV_PIX_FMT_RGBA, SWS_LANCZOS,
                                         NULL, NULL, NULL);

      // And allocate another for getImageSample
      _procConverter = sws_getContext(_pCodecCtx->width, _pCodecCtx->height,
                                      _pCodecCtx->pix_fmt,
                                      _width, _height,
                                      _numberOfPlanes == 3 ? AV_PIX_FMT_RGB48 : AV_PIX_FMT_GRAY16,
                                      SWS_LANCZOS, NULL, NULL, NULL);

      // Allocate also the conversion buffer
      const int vectSize = sizeof(u_short __attribute__  ((vector_size (8))));
      _bufLineLength = (_width * _numberOfPlanes * sizeof(u_short) + vectSize - 1) / vectSize;
      _bufLineLength *= vectSize;
      _convBuffer = (u_short*)malloc(_bufLineLength * _height);

      if(_displayConverter == NULL || _procConverter == NULL)
      {
         NSLog(@"Cannot initialize the conversion context!");
         [self release];
         return( nil );
      }

      // Get the frames times
      arraySize = 0;
//      startTime = _pFormatCtx->streams[_videoStream]->start_time;
      //frameDuration = (int64_t)
      //          ((double)_pFormatCtx->streams[_videoStream]->r_frame_rate.den
      //	  / (double)_pFormatCtx->streams[_videoStream]->r_frame_rate.num
      //	  * (double)_pFormatCtx->streams[_videoStream]->time_base.den
      //	  / (double)_pFormatCtx->streams[_videoStream]->time_base.num
      //	  + 0.5 );
//      timestamp = startTime;
      keyIndex = 0;
      keyTimestamp = 0/*startTime*/;
      for( validFrame = YES; validFrame; )
      {
         validFrame = [self nextFrame];

         if ( validFrame )
         {
            if ( _numberOfFrames >= arraySize )
            {
               arraySize += K_TIME_PAGE_SIZE;
               _times = (KeyFrames_t*)realloc( _times, arraySize*sizeof(KeyFrames_t) );
            }
/*
            if ( _pCurrentFrame->key_frame )
            {
               keyTimestamp = timestamp;
               keyIndex = _numberOfFrames;
            }
 */
            _times[_numberOfFrames].timestamp = keyTimestamp;
            _times[_numberOfFrames].keyFrame = keyIndex;

            _numberOfFrames ++;
//            timestamp = _pFormatCtx->streams[_videoStream]->cur_dts;
         }
      }
      // We are now pointing beyond sequence end
      _nextIndex = _numberOfFrames + 1;
   }

   return( self );
}

- (void) dealloc
{
   [_mutex release];
   if ( _pCodecCtx != NULL )
      avcodec_free_context(&_pCodecCtx);
   // Deallocate the current frame only if it is not already in the cache (otherwise, the cache will free it)
   if ( _pCurrentFrame != NULL )
      av_free(_pCurrentFrame);
   if ( _displayConverter != NULL )
      sws_freeContext( _displayConverter );
   if ( _procConverter != NULL )
      sws_freeContext( _procConverter );
   if (_convBuffer != NULL)
      free(_convBuffer);
   av_packet_free( &_packet );
   if ( _pFormatCtx != NULL )
      avformat_close_input( &_pFormatCtx );
   if ( _times != NULL )
      free( _times );

   [super dealloc];
}

- (void) imageWidth:(u_short*)w height:(u_short*)h
{   
   *w = _width;
   *h = _height;
}

- (u_short) numberOfPlanes
{
   return( _numberOfPlanes );
}

- (void) getMinLevel:(double*)vmin maxLevel:(double*)vmax
{
   *vmin = 0.0;
   *vmax = 255.0;
}

- (u_long) numberOfFrames
{
   return( _numberOfFrames );
}

- (NSImage*) getNSImageAtIndex:(u_long)index
{
   NSImage *image = nil;
   NSBitmapImageRep* bitmap;

   NSAssert( index < _numberOfFrames, @"Access beyond sequence end" );

   // Create a RGB bitmap
   bitmap = [[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
                                                   pixelsWide:_width
                                                   pixelsHigh:_height
                                                  bitsPerSample:8
                                                samplesPerPixel:4
                                                       hasAlpha:YES
                                                       isPlanar:NO
                                                 colorSpaceName:NSCalibratedRGBColorSpace
                                                    bytesPerRow:0
                                                   bitsPerPixel:32]
             autorelease];

   if ( bitmap != nil )
   {
      u_char *pixels = (u_char*)[bitmap bitmapData];
      int bpr = (int)[bitmap bytesPerRow];
      AVFrame *frame;
      int ret;

      [_mutex lock];

      frame = [self getFrame:index];

      if ( frame != NULL )
      {
         ret = sws_scale(_displayConverter, (const uint8_t *const *)frame->data, frame->linesize,
                         0, _height, &pixels, &bpr);
      }

      [_mutex unlock];

      image = [[[NSImage alloc] initWithSize:NSMakeSize(_width, _height)]
               autorelease];

      if ( image != nil )
         [image addRepresentation:bitmap];
   }

   return( image );
}

- (void) getImageSample:(REAL * const * const)sample atIndex:(u_long)index
             withPlanes:(u_short)nPlanes
                    atX:(short)x Y:(short)y W:(u_short)w H:(u_short)h
              lineWidth:(u_short)lineW
{
   u_short xs, ys, cs;
   int ret;

   NSAssert( index < _numberOfFrames, @"Access beyond sequence end" );
   NSAssert( x+w <= _width && y+h <= _height,
             @"Sample at least partly outside the image" );

   [_mutex lock];

   AVFrame *frame;
   frame = [self getFrame:index];

   if ( frame == NULL )
   {
      [_mutex unlock];
      NSAssert( NO, @"Could not access FFMpeg frame" );
   }

   // Convert the whole image, as only a slice seems to fail
   ret = sws_scale(_procConverter, (const uint8_t *const *)frame->data, frame->linesize,
                   0, _height,
                   (uint8_t *const *)&_convBuffer, &_bufLineLength);
   if ( ret < 0 )
   {
      NSLog( @"Could not convert image : %s", av_err2str(ret) );
      [_mutex unlock];
      return;
   }

   const u_short samplesPerLine = _bufLineLength / sizeof(u_short);
   for ( ys = 0; ys < h; ys++ )
   {
      const u_short yy = y + ys;

      for( xs = 0; xs < w; xs++ )
      {
         const u_short xx = x + xs;
         REAL v;

         if ( nPlanes != _numberOfPlanes && nPlanes == 1 )
         {
            // Convert to monochrome
            v = 0;
            for (cs = 0; cs < _numberOfPlanes; cs++)
               v += _convBuffer[samplesPerLine*yy + xx * _numberOfPlanes + cs];
            SET_SAMPLE( sample[0], xs, ys, lineW, v/(REAL)_numberOfPlanes/256.0 );
         }
         else
         {
            for( cs = 0; cs < nPlanes; cs++ )
            {
               v = (REAL)_convBuffer[samplesPerLine*yy + xx * _numberOfPlanes + cs];
               SET_SAMPLE( sample[cs], xs, ys, lineW, v/256.0);
            }
         }
      }
   }

   [_mutex unlock];
}

- (NSDictionary*) getMetaData 
{
   return( nil );
}

@end