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

« back to all changes in this revision

Viewing changes to avidemux/ADM_editor/ADM_edRender.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:
233
233
    }
234
234
  //*************************
235
235
  // completly async frame
236
 
  // rewind
 
236
  // rewind ?
237
237
  //*************************
 
238
    
238
239
  aprintf("async  frame, wanted : %lu last %lu (%lu - %lu seg)\n",relframe,_lastframe,seg,_lastseg);
239
240
  uint32_t rewind;
240
241
  uint32_t seekFlag=0;
242
243
  _videos[ref]._aviheader->getFlags (relframe, &seekFlag);
243
244
  
244
245
  flags = 0;
 
246
  uint32_t need_rewind=1;
245
247
  rewind = relframe;
246
248
  ADM_assert(rewind); // the first frame should be a keyframe !
247
249
  while (!(flags & AVI_KEY_FRAME))
249
251
        rewind--;
250
252
        _videos[ref]._aviheader->getFlags (rewind, &flags);     
251
253
   }
 
254
   // Optimize for resample FPS*************************
 
255
   // If we are in the same segment, look if it is better to decode
 
256
    // From where we are or to seek the previous intra
 
257
   if ((seg == _lastseg))
 
258
   {
 
259
     if(rewind<_lastframe && relframe>_lastframe) // we have a better position to go from
 
260
     { 
 
261
       for (uint32_t i = _lastframe+1; i <relframe; i++)
 
262
       {
 
263
 
 
264
         _videos[ref]._aviheader->getFlags (i, &flags);
 
265
      // Skip B frames, there can be a lot of them
 
266
         if((flags&AVI_B_FRAME)) continue;
 
267
     
 
268
         if(!decodeCache(i,ref, result))                  
 
269
         {
 
270
           printf("Editor: Cannot read ip frame %lu\n",relframe);
 
271
           return 0;
 
272
         }
 
273
         result=cache->getFreeImage();      
 
274
       }
 
275
       need_rewind=0;
 
276
     }
 
277
   }
 
278
   // Optimize for resample FPS*************************
 
279
   // 
252
280
  // now forward
253
281
  // IP seen is the last P (or I) before the frame we seek
254
 
  for (uint32_t i = rewind; i <relframe; i++)
255
 
    {
 
282
   if(need_rewind)
 
283
   {
 
284
        for (uint32_t i = rewind; i <relframe; i++)
 
285
        {
256
286
 
257
 
      _videos[ref]._aviheader->getFlags (i, &flags);
258
 
      // Skip B frames, there can be a lot of them
259
 
      if((flags&AVI_B_FRAME)) continue;
 
287
                _videos[ref]._aviheader->getFlags (i, &flags);
 
288
                // Skip B frames, there can be a lot of them
 
289
                if((flags&AVI_B_FRAME)) continue;
260
290
     
261
 
       if(!decodeCache(i,ref, result))                  
262
 
        {
263
 
                printf("Editor: Cannot read ip frame %lu\n",relframe);
264
 
                return 0;
265
 
        }
266
 
        result=cache->getFreeImage();      
267
 
      }
268
 
      
 
291
                if(!decodeCache(i,ref, result))                 
 
292
                {
 
293
                      printf("Editor: Cannot read ip frame %lu\n",relframe);
 
294
                      return 0;
 
295
                }
 
296
                result=cache->getFreeImage();      
 
297
        }
 
298
   }
269
299
      // Time to decode our frame
270
300
      // if it is not a B, just decode it
271
301
      // it it is a B, the usual stuff
336
366
uint32_t flags,sumit;
337
367
float    sum;
338
368
EditorCache *cache=_videos[seg]._videoCache;    
 
369
ADMImage *tmpImage=NULL;
 
370
uint8_t refOnly=0;
 
371
uint32_t left,ww;
 
372
 
339
373
         if (!_videos[seg]._aviheader->getFrameNoAlloc (frame,
340
374
                                                     compBuffer,
341
375
                                                     &len, &flags))
343
377
          printf ("\nEditor: last decoding failed.%ld)\n",   frame );
344
378
          return 0;
345
379
        }
346
 
        ADM_assert(_imageBuffer);
 
380
        ADM_assert(_imageBuffer);
 
381
 
 
382
        // if len is 0 then take the previous image                 
 
383
        //
 
384
 
 
385
        refOnly=_videos[seg].decoder->dontcopy(); // can we skip one memcpy ?
 
386
 
 
387
        if(!len & refOnly & !frame)      // Size is null = no image and we only got a pointer
 
388
                                // copy the previous one
 
389
        {                
 
390
                // First image
 
391
                
 
392
                uint32_t page=_imageBuffer->_width*_imageBuffer->_height;
 
393
                        memset(YPLANE(image),0,page);
 
394
                        memset(UPLANE(image),128,page>>2);
 
395
                        memset(VPLANE(image),128,page>>2);
 
396
                        if(!frame)
 
397
                                image->flags=AVI_KEY_FRAME;
 
398
                        else            
 
399
                                image->flags=AVI_P_FRAME;
 
400
                        image->_Qp=2;
 
401
                        image->_qStride=0;
 
402
                        return 1;        
 
403
        }
 
404
 
 
405
        if(refOnly)
 
406
        {       // This is only an empty Shell
 
407
                tmpImage=new ADMImage(_imageBuffer->_width,_imageBuffer->_height,1);
 
408
                ww=_imageBuffer->_width & 0xfffff0;
 
409
                left=_imageBuffer->_width & 0xf;
 
410
 
 
411
        }
 
412
        else
 
413
        {
 
414
                tmpImage=_imageBuffer;
 
415
                ww=_imageBuffer->_width;
 
416
                left=0;
 
417
       }
 
418
        
347
419
        // Do pp, and use imageBuffer as intermediate buffer
348
 
        if (!_videos[seg].decoder->uncompress (compBuffer, _imageBuffer, len, &flags))
 
420
        if (!_videos[seg].decoder->uncompress (compBuffer, tmpImage, len, &flags))
349
421
            {
350
422
              printf ("\nEditor: Last Decoding2 failed for frame %lu\n",frame);
351
423
              return 0;
352
424
            }
353
425
        // Quant ?
354
 
        if(!_imageBuffer->quant || !_imageBuffer->_qStride)
355
 
        {
356
 
                _imageBuffer->_Qp=2;
357
 
                image->duplicate(_imageBuffer);
 
426
        if(!tmpImage->quant || !tmpImage->_qStride)
 
427
        {      
 
428
                image->_Qp=2;
 
429
                image->duplicate(tmpImage);
358
430
                cache->updateFrameNum(image,frame);
 
431
                if(refOnly) delete tmpImage;
359
432
                aprintf("EdCache: No quant avail\n");
360
433
                return 1;
361
434
        }
363
436
        // 1 compute average quant
364
437
        int qz;
365
438
        sumit=0;
366
 
        for(uint32_t z=0;z<_imageBuffer->_qSize;z++)
 
439
        // Dupe infos
 
440
        image->copyInfo(tmpImage);
 
441
        //image->copyQuantInfo(tmpImage);
 
442
 
 
443
        // Do postprocessing if any
 
444
        for(uint32_t z=0;z<tmpImage->_qSize;z++)
367
445
        {
368
 
            qz=(int)_imageBuffer->quant[z];
 
446
            qz=(int)tmpImage->quant[z];
369
447
                        sumit+=qz;
370
448
        }
371
 
        sumit+=(_imageBuffer->_qSize-1);
 
449
        sumit+=(tmpImage->_qSize-1);
372
450
//      sumit*=2;
373
451
        sum=(float)sumit;
374
 
        sum/=_imageBuffer->_qSize;
 
452
        sum/=tmpImage->_qSize;
375
453
        if(sum>31) sum=31;
376
454
        if(sum<1) sum=1;
377
 
                
378
 
        _imageBuffer->_Qp=(uint32_t)floor(sum);
 
455
        
 
456
        // update average Q     
 
457
        tmpImage->_Qp=image->_Qp=(uint32_t)floor(sum);
379
458
        
380
459
        // Pp deactivated ?
381
460
        if(!_pp.postProcType || !_pp.postProcStrength)
382
461
        {
383
 
                image->duplicate(_imageBuffer);
 
462
                // nothing to do
 
463
                image->duplicate(tmpImage);
384
464
                cache->updateFrameNum(image,frame);
 
465
                if(refOnly) delete tmpImage;
385
466
                aprintf("EdCache: Postproc disabled\n");
386
467
                return 1;       
387
468
        }
388
469
        
389
470
        int type;       
390
471
        #warning FIXME should be FF_I_TYPE/B/P
391
 
        if(_imageBuffer->flags & AVI_KEY_FRAME) type=1;
392
 
                else if(_imageBuffer->flags & AVI_B_FRAME) type=3;
 
472
        if(tmpImage->flags & AVI_KEY_FRAME) type=1;
 
473
                else if(tmpImage->flags & AVI_B_FRAME) type=3;
393
474
                        else type=2;
394
475
        
395
476
        // we do postproc !
400
481
        aviInfo _info;
401
482
                
402
483
                getVideoInfo(&_info);
403
 
                iBuff[0]= YPLANE((_imageBuffer));
404
 
                if(1)
405
 
                {
406
 
                        iBuff[1]= UPLANE((_imageBuffer));
407
 
                        iBuff[2]= VPLANE((_imageBuffer));
408
 
                }
409
 
                else
410
 
                {
411
 
                        iBuff[2]= UPLANE((_imageBuffer));
412
 
                        iBuff[1]= VPLANE((_imageBuffer));
413
 
                }
414
 
                        oBuff[0]= YPLANE(image);
415
 
                        oBuff[1]= UPLANE(image);
416
 
                        oBuff[2]= VPLANE(image);
417
 
                                
418
 
                        strideTab[0]=strideTab2[0]=_info.width;
419
 
                        strideTab[1]=strideTab2[1]=_info.width>>1;
420
 
                        strideTab[2]=strideTab2[2]=_info.width>>1;
421
 
                                
 
484
                if(refOnly)
 
485
                {
 
486
                        iBuff[0]= tmpImage->_planes[0];
 
487
                        iBuff[1]= tmpImage->_planes[1];
 
488
                        iBuff[2]= tmpImage->_planes[2];
 
489
        
 
490
                        strideTab2[0]=_info.width;
 
491
                        strideTab2[1]=_info.width>>1;
 
492
                        strideTab2[2]=_info.width>>1;
 
493
 
 
494
                        strideTab[0]=tmpImage->_planeStride[0];
 
495
                        strideTab[1]=tmpImage->_planeStride[1];
 
496
                        strideTab[2]=tmpImage->_planeStride[2];
 
497
                        
 
498
                }
 
499
                else
 
500
                {
 
501
                        iBuff[0]= YPLANE((tmpImage));
 
502
                        iBuff[1]= UPLANE((tmpImage));
 
503
                        iBuff[2]= VPLANE((tmpImage));
 
504
                
 
505
                        
 
506
                                                                
 
507
                        strideTab[0]=strideTab2[0]=_info.width;
 
508
                        strideTab[1]=strideTab2[1]=_info.width>>1;
 
509
                        strideTab[2]=strideTab2[2]=_info.width>>1;
 
510
                }
 
511
                        oBuff[0]= YPLANE(image);
 
512
                        oBuff[1]= UPLANE(image);
 
513
                        oBuff[2]= VPLANE(image);                
 
514
 
422
515
                 pp_postprocess(
423
516
                                iBuff,
424
517
                                strideTab,
425
518
                                oBuff,
426
519
                                strideTab2,
427
 
                                _info.width,
 
520
                                ww,
428
521
                                _info.height,
429
 
                                (int8_t *)(_imageBuffer->quant),
430
 
                                _imageBuffer->_qStride,
 
522
                                (int8_t *)(tmpImage->quant),
 
523
                                tmpImage->_qStride,
431
524
                                _pp.ppMode,
432
525
                                _pp.ppContext,
433
526
                                type);                  // img type
434
 
                                // update some infos
435
 
                image->copyInfo(_imageBuffer);
 
527
                /* 
 
528
                        If there is a chroma block that needs padding
 
529
                        (width not multiple of 16) while postprocessing,
 
530
                        we process up to the nearest 16 multiple and
 
531
                        just copy luma & chroma info that was left over
 
532
                */
 
533
                if(refOnly && left)
 
534
                {
 
535
                        uint8_t *src,*dst;
 
536
                        uint32_t stridein,strideout,right;
 
537
                        right=_info.width-left;
 
538
                        // Luma
 
539
                        dst=YPLANE(image)+right;
 
540
                        src=tmpImage->_planes[0]+right;
 
541
                        stridein=tmpImage->_planeStride[0];
 
542
                        strideout=_info.width;
 
543
                        for(uint32_t y=_info.height;y>0;y--)
 
544
                        {
 
545
                                memcpy(dst,src,left);
 
546
                                dst+=strideout;
 
547
                                src+=stridein;
 
548
                        }
 
549
                        // Chroma
 
550
                        left>>=1;
 
551
                        right>>=1;
 
552
                        //
 
553
                        dst=UPLANE(image)+right;
 
554
                        src=tmpImage->_planes[1]+right;
 
555
                        stridein=tmpImage->_planeStride[1];
 
556
                        strideout=_info.width>>1;
 
557
                        for(uint32_t y=_info.height>>1;y>0;y--)
 
558
                        {
 
559
                                memcpy(dst,src,left);
 
560
                                dst+=strideout;
 
561
                                src+=stridein;
 
562
                        }
 
563
                        //
 
564
                        dst=VPLANE(image)+right;
 
565
                        src=tmpImage->_planes[2]+right;
 
566
                        stridein=tmpImage->_planeStride[2];
 
567
                        strideout=_info.width>>1;
 
568
                        for(uint32_t y=_info.height>>1;y>0;y--)
 
569
                        {
 
570
                                memcpy(dst,src,left);
 
571
                                dst+=strideout;
 
572
                                src+=stridein;
 
573
                        }
 
574
                        
 
575
 
 
576
                }
 
577
 
 
578
                // update some infos
 
579
                if(refOnly) delete tmpImage;
436
580
                cache->updateFrameNum(image,frame);
437
581
                aprintf("EdCache: Postproc done\n");
438
582
                return 1;