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

流媒体/Mpeg4/MP4

开发平台:

Visual C++

  1. /*
  2.  * The contents of this file are subject to the Mozilla Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/MPL/
  6.  * 
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  * 
  12.  * The Original Code is MPEG4IP.
  13.  * 
  14.  * The Initial Developer of the Original Code is Cisco Systems Inc.
  15.  * Portions created by Cisco Systems Inc. are
  16.  * Copyright (C) Cisco Systems Inc. 2000, 2001.  All Rights Reserved.
  17.  * 
  18.  * Contributor(s): 
  19.  * Dave Mackie dmackie@cisco.com
  20.  */
  21. #include "quicktime.h"
  22. int quicktime_ctts_init(quicktime_ctts_t *ctts)
  23. {
  24. ctts->version = 0;
  25. ctts->flags = 0;
  26. ctts->total_entries = 0;
  27. ctts->entries_allocated = 0;
  28. }
  29. int quicktime_ctts_init_table(quicktime_ctts_t *ctts)
  30. {
  31. if (!ctts->entries_allocated) {
  32. ctts->entries_allocated = 1;
  33. ctts->table = (quicktime_ctts_table_t*)
  34. malloc(sizeof(quicktime_ctts_table_t) * ctts->entries_allocated);
  35. ctts->total_entries = 1;
  36. }
  37. }
  38. int quicktime_ctts_init_common(quicktime_t *file, quicktime_ctts_t *ctts)
  39. {
  40. quicktime_ctts_table_t *table;
  41. quicktime_ctts_init_table(ctts);
  42. table = &(ctts->table[0]);
  43. table->sample_count = 0;      /* need to set this when closing */
  44. table->sample_offset = 0;
  45. }
  46. int quicktime_ctts_delete(quicktime_ctts_t *ctts)
  47. {
  48. if (ctts->total_entries) {
  49. free(ctts->table);
  50. }
  51. ctts->total_entries = 0;
  52. }
  53. int quicktime_ctts_dump(quicktime_ctts_t *ctts)
  54. {
  55. int i;
  56. printf("     composition time to samplen");
  57. printf("      version %dn", ctts->version);
  58. printf("      flags %dn", ctts->flags);
  59. printf("      total_entries %dn", ctts->total_entries);
  60. for(i = 0; i < ctts->total_entries; i++) {
  61. printf("       count %ld offset %ldn", 
  62. ctts->table[i].sample_count,
  63. ctts->table[i].sample_offset);
  64. }
  65. }
  66. int quicktime_read_ctts(quicktime_t *file, quicktime_ctts_t *ctts)
  67. {
  68. int i;
  69. ctts->version = quicktime_read_char(file);
  70. ctts->flags = quicktime_read_int24(file);
  71. ctts->total_entries = quicktime_read_int32(file);
  72. ctts->table = (quicktime_ctts_table_t*)
  73. malloc(sizeof(quicktime_ctts_table_t) * ctts->total_entries);
  74. for (i = 0; i < ctts->total_entries; i++) {
  75. ctts->table[i].sample_count = quicktime_read_int32(file);
  76. ctts->table[i].sample_offset = quicktime_read_int32(file);
  77. }
  78. }
  79. int quicktime_write_ctts(quicktime_t *file, quicktime_ctts_t *ctts)
  80. {
  81. int i;
  82. quicktime_atom_t atom;
  83. if (!file->use_mp4) {
  84. return;
  85. }
  86. if (ctts->total_entries == 1 && ctts->table[0].sample_offset == 0) {
  87. return;
  88. }
  89. quicktime_atom_write_header(file, &atom, "ctts");
  90. quicktime_write_char(file, ctts->version);
  91. quicktime_write_int24(file, ctts->flags);
  92. quicktime_write_int32(file, ctts->total_entries);
  93. for(i = 0; i < ctts->total_entries; i++) {
  94. quicktime_write_int32(file, ctts->table[i].sample_count);
  95. quicktime_write_int32(file, ctts->table[i].sample_offset);
  96. }
  97. quicktime_atom_write_footer(file, &atom);
  98. }
  99. int quicktime_update_ctts(quicktime_ctts_t *ctts, long sample_offset)
  100. {
  101. if (sample_offset == ctts->table[ctts->total_entries-1].sample_offset) {
  102. ctts->table[ctts->total_entries-1].sample_count++;
  103. } else {
  104. /* need a new entry in the table */
  105. /* allocate more entries if necessary */
  106. if (ctts->total_entries >= ctts->entries_allocated) {
  107. ctts->entries_allocated *= 2;
  108. ctts->table = (quicktime_ctts_table_t*)realloc(ctts->table,
  109. sizeof(quicktime_ctts_table_t) * ctts->entries_allocated);
  110. }
  111. ctts->table[ctts->total_entries].sample_count = 1;
  112. ctts->table[ctts->total_entries].sample_offset = sample_offset;
  113. ctts->total_entries++;
  114. }
  115. }