~ubuntu-branches/ubuntu/trusty/libtheora/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/enc/encoder_toplevel.c

  • Committer: Bazaar Package Importer
  • Author(s): Clint Adams
  • Date: 2008-04-23 14:08:37 UTC
  • mfrom: (1.1.6 upstream) (3.1.2 lenny)
  • Revision ID: james.westby@ubuntu.com-20080423140837-t981xtgv43carzk8
Tags: 1.0~beta3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 ********************************************************************
12
12
 
13
13
  function:
14
 
  last mod: $Id: encoder_toplevel.c 13945 2007-10-09 14:41:01Z msmith $
 
14
  last mod: $Id: encoder_toplevel.c 14372 2008-01-05 23:52:28Z giles $
15
15
 
16
16
 ********************************************************************/
17
17
 
26
26
#include "dsp.h"
27
27
#include "codec_internal.h"
28
28
 
 
29
#ifdef _TH_DEBUG_
 
30
FILE *debugout=NULL;
 
31
long dframe=0;
 
32
#endif
 
33
 
29
34
#define A_TABLE_SIZE        29
30
35
#define DF_CANDIDATE_WINDOW 5
31
36
 
349
354
  memset( cpi->extra_fragments, 1, cpi->pb.UnitFragments );
350
355
 
351
356
  /* Set up for a KEY FRAME */
352
 
  SetFrameType( &cpi->pb,KEY_FRAME );
 
357
  cpi->pb.FrameType = KEY_FRAME;
353
358
}
354
359
 
355
360
static void AdjustKeyFrameContext(CP_INSTANCE *cpi) {
427
432
  cpi->pb.InvLastInterDC = 0;
428
433
 
429
434
  /* Initialise bit packing mechanism. */
430
 
#ifndef LIBOGG2
431
435
  oggpackB_reset(cpi->oggbuffer);
432
 
#else
433
 
  oggpackB_writeinit(cpi->oggbuffer, cpi->oggbufferstate);
434
 
#endif
 
436
 
435
437
  /* mark as video frame */
436
438
  oggpackB_write(cpi->oggbuffer,0,1);
437
439
 
446
448
  EncodeData(cpi);
447
449
 
448
450
  /* Adjust drop frame trigger. */
449
 
  if ( GetFrameType(&cpi->pb) != KEY_FRAME ) {
 
451
  if ( cpi->pb.FrameType != KEY_FRAME ) {
450
452
    /* Apply decay factor then add in the last frame size. */
451
453
    cpi->DropFrameTriggerBytes =
452
454
      ((cpi->DropFrameTriggerBytes * (DF_CANDIDATE_WINDOW-1)) /
479
481
 
480
482
  /* Update the BpbCorrectionFactor variable according to whether or
481
483
     not we were close enough with our selection of DCT quantiser.  */
482
 
  if ( GetFrameType(&cpi->pb) != KEY_FRAME ) {
 
484
  if ( cpi->pb.FrameType != KEY_FRAME ) {
483
485
    /* Work out a size correction factor. */
484
486
    CorrectionFactor = (double)oggpackB_bytes(cpi->oggbuffer) /
485
487
      (double)cpi->ThisFrameTargetBytes;
511
513
  }
512
514
 
513
515
  /* Adjust carry over and or key frame context. */
514
 
  if ( GetFrameType(&cpi->pb) == KEY_FRAME ) {
 
516
  if ( cpi->pb.FrameType == KEY_FRAME ) {
515
517
    /* Adjust the key frame context unless the key frame was very small */
516
518
    AdjustKeyFrameContext(cpi);
517
519
  } else {
663
665
  }
664
666
 
665
667
  /* Default to delta frames. */
666
 
  SetFrameType( &cpi->pb, DELTA_FRAME );
 
668
  cpi->pb.FrameType = DELTA_FRAME;
667
669
 
668
670
  /* Clear down the difference arrays for the current frame. */
669
671
  memset( cpi->pb.display_fragments, 0, cpi->pb.UnitFragments );
895
897
 
896
898
  CP_INSTANCE *cpi;
897
899
 
 
900
#ifdef _TH_DEBUG_
 
901
  debugout=fopen("theoraenc-debugout.txt","w");
 
902
#endif
 
903
 
898
904
  memset(th, 0, sizeof(*th));
899
905
  /*Currently only the 4:2:0 format is supported.*/
900
906
  if(c->pixelformat!=OC_PF_420)return OC_IMPL;
928
934
  cpi->ExhaustiveSearchThresh = 2500;
929
935
  cpi->MinImprovementForFourMV = 100;
930
936
  cpi->FourMVThreshold = 10000;
931
 
  cpi->BitRateCapFactor = 1.50;
 
937
  cpi->BitRateCapFactor = 1.5;
932
938
  cpi->InterTripOutThresh = 5000;
933
939
  cpi->MVEnabled = 1;
934
940
  cpi->InterCodeCount = 127;
972
978
    cpi->QTargetModifier[i] = 1.0;
973
979
 
974
980
  /* Set up an encode buffer */
975
 
#ifndef LIBOGG2
976
981
  cpi->oggbuffer = _ogg_malloc(sizeof(oggpack_buffer));
977
982
  oggpackB_writeinit(cpi->oggbuffer);
978
 
#else
979
 
  cpi->oggbuffer = _ogg_malloc(oggpack_buffersize());
980
 
  cpi->oggbufferstate = ogg_buffer_create();
981
 
  oggpackB_writeinit(cpi->oggbuffer, cpi->oggbufferstate);
982
 
#endif 
983
983
 
984
984
  /* Set data rate related variables. */
985
985
  cpi->Configuration.TargetBandwidth = (c->target_bitrate) / 8;
1045
1045
  cpi->readyflag = 1;
1046
1046
  
1047
1047
  cpi->pb.HeadersWritten = 0;
 
1048
  /*We overload this flag to track header output.*/
 
1049
  cpi->doneflag=-3;
1048
1050
 
1049
1051
  return 0;
1050
1052
}
1057
1059
  CP_INSTANCE *cpi=(CP_INSTANCE *)(t->internal_encode);
1058
1060
 
1059
1061
  if(!cpi->readyflag)return OC_EINVAL;
1060
 
  if(cpi->doneflag)return OC_EINVAL;
 
1062
  if(cpi->doneflag>0)return OC_EINVAL;
1061
1063
 
1062
1064
  /* If frame size has changed, abort out for now */
1063
1065
  if (yuv->y_height != (int)cpi->pb.info.height ||
1129
1131
    ((cpi->CurrentFrame - cpi->LastKeyFrame)<<cpi->pb.keyframe_granule_shift)+
1130
1132
    cpi->LastKeyFrame - 1;
1131
1133
 
 
1134
#ifdef _TH_DEBUG_
 
1135
  dframe++;
 
1136
#endif  
 
1137
 
1132
1138
  return 0;
1133
1139
}
1134
1140
 
1138
1144
 
1139
1145
  if(!bytes)return(0);
1140
1146
  if(!cpi->packetflag)return(0);
1141
 
  if(cpi->doneflag)return(-1);
 
1147
  if(cpi->doneflag>0)return(-1);
1142
1148
 
1143
 
#ifndef LIBOGG2
1144
1149
  op->packet=oggpackB_get_buffer(cpi->oggbuffer);
1145
 
#else
1146
 
  op->packet=oggpackB_writebuffer(cpi->oggbuffer);
1147
 
#endif
1148
1150
  op->bytes=bytes;
1149
1151
  op->b_o_s=0;
1150
1152
  op->e_o_s=last_p;
1179
1181
  CP_INSTANCE *cpi=(CP_INSTANCE *)(t->internal_encode);
1180
1182
  int offset_y;
1181
1183
 
1182
 
#ifndef LIBOGG2
1183
1184
  oggpackB_reset(cpi->oggbuffer);
1184
 
#else
1185
 
  oggpackB_writeinit(cpi->oggbuffer, cpi->oggbufferstate);
1186
 
#endif
1187
1185
  oggpackB_write(cpi->oggbuffer,0x80,8);
1188
1186
  _tp_writebuffer(cpi->oggbuffer, "theora", 6);
1189
1187
 
1218
1216
 
1219
1217
  oggpackB_write(cpi->oggbuffer,0,3); /* spare config bits */
1220
1218
 
1221
 
#ifndef LIBOGG2
1222
1219
  op->packet=oggpackB_get_buffer(cpi->oggbuffer);
1223
 
#else
1224
 
  op->packet=oggpackB_writebuffer(cpi->oggbuffer);
1225
 
#endif
1226
1220
  op->bytes=oggpackB_bytes(cpi->oggbuffer);
1227
1221
 
1228
1222
  op->b_o_s=1;
1243
1237
  const int vendor_length = strlen(vendor);
1244
1238
  oggpack_buffer *opb;
1245
1239
 
1246
 
#ifndef LIBOGG2
1247
1240
  opb = _ogg_malloc(sizeof(oggpack_buffer));
1248
1241
  oggpackB_writeinit(opb);
1249
 
#else
1250
 
  opb = _ogg_malloc(oggpack_buffersize());
1251
 
  oggpackB_writeinit(opb, ogg_buffer_create());
1252
 
#endif 
1253
1242
  oggpackB_write(opb, 0x81, 8);
1254
1243
  _tp_writebuffer(opb, "theora", 6);
1255
1244
 
1270
1259
  }
1271
1260
  op->bytes=oggpack_bytes(opb);
1272
1261
 
1273
 
#ifndef LIBOGG2
1274
1262
  /* So we're expecting the application will free this? */
1275
1263
  op->packet=_ogg_malloc(oggpack_bytes(opb));
1276
1264
  memcpy(op->packet, oggpack_get_buffer(opb), oggpack_bytes(opb));
1277
1265
  oggpack_writeclear(opb);
1278
 
#else
1279
 
  op->packet = oggpack_writebuffer(opb);
1280
 
  /* When the application puts op->packet into a stream_state object,
1281
 
     it becomes the property of libogg2's internal memory management. */
1282
 
#endif
1283
1266
 
1284
1267
  _ogg_free(opb);
1285
1268
 
1297
1280
int theora_encode_tables(theora_state *t, ogg_packet *op){
1298
1281
  CP_INSTANCE *cpi=(CP_INSTANCE *)(t->internal_encode);
1299
1282
 
1300
 
#ifndef LIBOGG2
1301
1283
  oggpackB_reset(cpi->oggbuffer);
1302
 
#else
1303
 
  oggpackB_writeinit(cpi->oggbuffer, cpi->oggbufferstate);
1304
 
#endif
1305
1284
  oggpackB_write(cpi->oggbuffer,0x82,8);
1306
1285
  _tp_writebuffer(cpi->oggbuffer,"theora",6);
1307
1286
 
1308
1287
  WriteQTables(&cpi->pb,cpi->oggbuffer);
1309
1288
  WriteHuffmanTrees(cpi->pb.HuffRoot_VP3x,cpi->oggbuffer);
1310
1289
 
1311
 
#ifndef LIBOGG2
1312
1290
  op->packet=oggpackB_get_buffer(cpi->oggbuffer);
1313
 
#else
1314
 
  op->packet=oggpackB_writebuffer(cpi->oggbuffer);
1315
 
#endif
1316
1291
  op->bytes=oggpackB_bytes(cpi->oggbuffer);
1317
1292
 
1318
1293
  op->b_o_s=0;
1345
1320
    _ogg_free(cpi->oggbuffer);
1346
1321
    _ogg_free(cpi);
1347
1322
  }
 
1323
 
 
1324
#ifdef _TH_DEBUG_
 
1325
  fclose(debugout);
 
1326
  debugout=NULL;
 
1327
#endif
 
1328
 
1348
1329
  memset(th,0,sizeof(*th));
1349
1330
}
1350
1331
 
1384
1365
    ogg_int64_t iframe=granulepos>>pbi->keyframe_granule_shift;
1385
1366
    ogg_int64_t pframe=granulepos-(iframe<<pbi->keyframe_granule_shift);
1386
1367
 
1387
 
    return (iframe+pframe);
 
1368
    return (iframe+pframe-1);
1388
1369
  }
1389
1370
 
1390
1371
  return(-1);