stts.c
上传用户:sun1608
上传日期:2007-02-02
资源大小:6116k
文件大小:4k
源码类别:

流媒体/Mpeg4/MP4

开发平台:

Visual C++

  1. #include "quicktime.h"
  2. int quicktime_stts_init(quicktime_stts_t *stts)
  3. {
  4. stts->version = 0;
  5. stts->flags = 0;
  6. stts->total_entries = 0;
  7. stts->entries_allocated = 0;
  8. }
  9. int quicktime_stts_init_table(quicktime_stts_t *stts)
  10. {
  11. if(!stts->entries_allocated)
  12. {
  13. stts->entries_allocated = 1;
  14. stts->table = (quicktime_stts_table_t*)
  15. malloc(sizeof(quicktime_stts_table_t) * stts->entries_allocated);
  16. stts->total_entries = 1;
  17. }
  18. }
  19. int quicktime_stts_init_video(quicktime_t *file, quicktime_stts_t *stts, int time_scale, float frame_rate)
  20. {
  21. quicktime_stts_table_t *table;
  22. quicktime_stts_init_table(stts);
  23. table = &(stts->table[0]);
  24. table->sample_count = 0;      /* need to set this when closing */
  25. table->sample_duration = time_scale / frame_rate;
  26. }
  27. int quicktime_stts_init_audio(quicktime_t *file, quicktime_stts_t *stts, int time_scale, int sample_duration)
  28. {
  29. quicktime_stts_table_t *table;
  30. quicktime_stts_init_table(stts);
  31. table = &(stts->table[0]);
  32. table->sample_count = 0;     /* need to set this when closing or via update function */
  33. table->sample_duration = sample_duration;
  34. }
  35. int quicktime_stts_init_hint(quicktime_t *file, quicktime_stts_t *stts, int sample_duration)
  36. {
  37. quicktime_stts_table_t *table;
  38. quicktime_stts_init_table(stts);
  39. table = &(stts->table[0]);
  40. table->sample_count = 0;
  41. table->sample_duration = sample_duration;
  42. }
  43. int quicktime_stts_delete(quicktime_stts_t *stts)
  44. {
  45. if(stts->total_entries) free(stts->table);
  46. stts->total_entries = 0;
  47. }
  48. int quicktime_stts_dump(quicktime_stts_t *stts)
  49. {
  50. int i;
  51. printf("     time to samplen");
  52. printf("      version %dn", stts->version);
  53. printf("      flags %dn", stts->flags);
  54. printf("      total_entries %dn", stts->total_entries);
  55. for(i = 0; i < stts->total_entries; i++)
  56. {
  57. printf("       count %ld duration %ldn", stts->table[i].sample_count, stts->table[i].sample_duration);
  58. }
  59. }
  60. int quicktime_read_stts(quicktime_t *file, quicktime_stts_t *stts)
  61. {
  62. int i;
  63. stts->version = quicktime_read_char(file);
  64. stts->flags = quicktime_read_int24(file);
  65. stts->total_entries = quicktime_read_int32(file);
  66. stts->table = (quicktime_stts_table_t*)malloc(sizeof(quicktime_stts_table_t) * stts->total_entries);
  67. for(i = 0; i < stts->total_entries; i++)
  68. {
  69. stts->table[i].sample_count = quicktime_read_int32(file);
  70. stts->table[i].sample_duration = quicktime_read_int32(file);
  71. }
  72. }
  73. int quicktime_write_stts(quicktime_t *file, quicktime_stts_t *stts)
  74. {
  75. int i;
  76. quicktime_atom_t atom;
  77. quicktime_atom_write_header(file, &atom, "stts");
  78. quicktime_write_char(file, stts->version);
  79. quicktime_write_int24(file, stts->flags);
  80. quicktime_write_int32(file, stts->total_entries);
  81. for(i = 0; i < stts->total_entries; i++)
  82. {
  83. quicktime_write_int32(file, stts->table[i].sample_count);
  84. quicktime_write_int32(file, stts->table[i].sample_duration);
  85. }
  86. quicktime_atom_write_footer(file, &atom);
  87. }
  88. int quicktime_update_stts(quicktime_stts_t *stts, long sample_duration)
  89. {
  90. if (sample_duration == stts->table[stts->total_entries-1].sample_duration) {
  91. stts->table[stts->total_entries-1].sample_count++;
  92. } else {
  93. /* need a new entry in the table */
  94. /* allocate more entries if necessary */
  95. if (stts->total_entries >= stts->entries_allocated) {
  96. stts->entries_allocated *= 2;
  97. stts->table = (quicktime_stts_table_t*)realloc(stts->table,
  98. sizeof(quicktime_stts_table_t) * stts->entries_allocated);
  99. }
  100. stts->table[stts->total_entries].sample_count = 1;
  101. stts->table[stts->total_entries].sample_duration = sample_duration;
  102. stts->total_entries++;
  103. }
  104. }