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

流媒体/Mpeg4/MP4

开发平台:

Visual C++

  1. #include "quicktime.h"
  2. int quicktime_stsd_table_init(quicktime_stsd_table_t *table)
  3. {
  4. int i;
  5. table->format[0] = 'y';
  6. table->format[1] = 'u';
  7. table->format[2] = 'v';
  8. table->format[3] = '2';
  9. for(i = 0; i < 6; i++) table->reserved[i] = 0;
  10. table->data_reference = 1;
  11. table->version = 0;
  12. table->revision = 0;
  13.   table->vendor[0] = 'l';
  14.   table->vendor[1] = 'n';
  15.   table->vendor[2] = 'u';
  16.   table->vendor[3] = 'x';
  17. table->temporal_quality = 0;
  18. table->spatial_quality = 258;
  19. table->width = 0;
  20. table->height = 0;
  21. table->dpi_horizontal = 72;
  22. table->dpi_vertical = 72;
  23. table->data_size = 0;
  24. table->frames_per_sample = 1;
  25. for(i = 0; i < 32; i++) table->compressor_name[i] = 0;
  26. sprintf(table->compressor_name, "Quicktime for Linux");
  27. table->depth = 24;
  28. table->ctab_id = 65535;
  29. quicktime_ctab_init(&(table->ctab));
  30. table->gamma = 0;
  31. table->fields = 0;
  32. table->field_dominance = 1;
  33. quicktime_mjqt_init(&(table->mjqt));
  34. quicktime_mjht_init(&(table->mjht));
  35. table->channels = 0;
  36. table->sample_size = 0;
  37. table->compression_id = 0;
  38. table->packet_size = 0;
  39. table->sample_rate = 0;
  40. table->maxPktSize = 0;
  41. quicktime_tims_init(&(table->tims));
  42. quicktime_esds_init(&(table->esds));
  43. }
  44. int quicktime_stsd_table_delete(quicktime_stsd_table_t *table)
  45. {
  46. quicktime_ctab_delete(&(table->ctab));
  47. quicktime_mjqt_delete(&(table->mjqt));
  48. quicktime_mjht_delete(&(table->mjht));
  49. quicktime_esds_delete(&(table->esds));
  50. }
  51. int quicktime_stsd_table_dump(void *minf_ptr, quicktime_stsd_table_t *table)
  52. {
  53. quicktime_minf_t *minf = minf_ptr;
  54. printf("       format %c%c%c%cn", table->format[0], table->format[1], table->format[2], table->format[3]);
  55. quicktime_print_chars("       reserved ", table->reserved, 6);
  56. printf("       data_reference %dn", table->data_reference);
  57. if(minf->is_audio) quicktime_stsd_audio_dump(table);
  58. if(minf->is_video) quicktime_stsd_video_dump(table);
  59. if(minf->is_hint)  quicktime_stsd_hint_dump(table);
  60. }
  61. int quicktime_stsd_audio_dump(quicktime_stsd_table_t *table)
  62. {
  63. printf("       version %dn", table->version);
  64. printf("       revision %dn", table->revision);
  65. printf("       vendor %c%c%c%cn", table->vendor[0], table->vendor[1], table->vendor[2], table->vendor[3]);
  66. printf("       channels %dn", table->channels);
  67. printf("       sample_size %dn", table->sample_size);
  68. printf("       compression_id %dn", table->compression_id);
  69. printf("       packet_size %dn", table->packet_size);
  70. printf("       sample_rate %fn", table->sample_rate);
  71. quicktime_esds_dump(&(table->esds));
  72. }
  73. int quicktime_stsd_video_dump(quicktime_stsd_table_t *table)
  74. {
  75. printf("       version %dn", table->version);
  76. printf("       revision %dn", table->revision);
  77. printf("       vendor %c%c%c%cn", table->vendor[0], table->vendor[1], table->vendor[2], table->vendor[3]);
  78. printf("       temporal_quality %ldn", table->temporal_quality);
  79. printf("       spatial_quality %ldn", table->spatial_quality);
  80. printf("       width %dn", table->width);
  81. printf("       height %dn", table->height);
  82. printf("       dpi_horizontal %fn", table->dpi_horizontal);
  83. printf("       dpi_vertical %fn", table->dpi_vertical);
  84. printf("       data_size %ldn", table->data_size);
  85. printf("       frames_per_sample %dn", table->frames_per_sample);
  86. printf("       compressor_name %sn", table->compressor_name);
  87. printf("       depth %dn", table->depth);
  88. printf("       ctab_id %dn", table->ctab_id);
  89. printf("       gamma %fn", table->gamma);
  90. if(table->fields)
  91. {
  92. printf("       fields %dn", table->fields);
  93. printf("       field dominance %dn", table->field_dominance);
  94. }
  95. if(!table->ctab_id) quicktime_ctab_dump(&(table->ctab));
  96. quicktime_mjqt_dump(&(table->mjqt));
  97. quicktime_mjht_dump(&(table->mjht));
  98. quicktime_esds_dump(&(table->esds));
  99. }
  100. int quicktime_stsd_hint_dump(quicktime_stsd_table_t *table)
  101. {
  102. printf("       version %dn", table->version);
  103. printf("       revision %dn", table->revision);
  104. printf("       maxPktSize %dn", table->maxPktSize);
  105. /* TBD dump adtab */
  106. }
  107. int quicktime_read_stsd_table(quicktime_t *file, quicktime_minf_t *minf, quicktime_stsd_table_t *table)
  108. {
  109. quicktime_atom_t leaf_atom;
  110. quicktime_atom_read_header(file, &leaf_atom);
  111. table->format[0] = leaf_atom.type[0];
  112. table->format[1] = leaf_atom.type[1];
  113. table->format[2] = leaf_atom.type[2];
  114. table->format[3] = leaf_atom.type[3];
  115. quicktime_read_data(file, table->reserved, 6);
  116. table->data_reference = quicktime_read_int16(file);
  117. if(minf->is_audio) quicktime_read_stsd_audio(file, table, &leaf_atom);
  118. if(minf->is_video) quicktime_read_stsd_video(file, table, &leaf_atom);
  119. if(minf->is_hint) quicktime_read_stsd_hint(file, table, &leaf_atom);
  120. }
  121. int quicktime_write_stsd_table(quicktime_t *file, quicktime_minf_t *minf, quicktime_stsd_table_t *table)
  122. {
  123. quicktime_atom_t atom;
  124. quicktime_atom_write_header(file, &atom, table->format);
  125. /*printf("quicktime_write_stsd_table %c%c%c%cn", table->format[0], table->format[1], table->format[2], table->format[3]); */
  126. quicktime_write_data(file, table->reserved, 6);
  127. quicktime_write_int16(file, table->data_reference);
  128. if(minf->is_audio) quicktime_write_stsd_audio(file, table);
  129. if(minf->is_video) quicktime_write_stsd_video(file, table);
  130. if(minf->is_hint) quicktime_write_stsd_hint(file, table);
  131. quicktime_atom_write_footer(file, &atom);
  132. }
  133. int quicktime_read_stsd_audio(quicktime_t *file, quicktime_stsd_table_t *table, quicktime_atom_t *parent_atom)
  134. {
  135. table->version = quicktime_read_int16(file);
  136. table->revision = quicktime_read_int16(file);
  137. quicktime_read_data(file, table->vendor, 4);
  138. table->channels = quicktime_read_int16(file);
  139. table->sample_size = quicktime_read_int16(file);
  140. table->compression_id = quicktime_read_int16(file);
  141. table->packet_size = quicktime_read_int16(file);
  142. table->sample_rate = quicktime_read_fixed32(file);
  143. while (quicktime_position(file) < parent_atom->end) {
  144. quicktime_atom_t leaf_atom;
  145. quicktime_atom_read_header(file, &leaf_atom);
  146. if(quicktime_atom_is(&leaf_atom, "esds")) {
  147. quicktime_read_esds(file, &(table->esds));
  148. quicktime_atom_skip(file, &leaf_atom);
  149. } else {
  150. quicktime_atom_skip(file, &leaf_atom);
  151. }
  152. }
  153. }
  154. int quicktime_write_stsd_audio(quicktime_t *file, quicktime_stsd_table_t *table)
  155. {
  156. if (file->use_mp4) {
  157. quicktime_write_int32(file, 0);
  158. quicktime_write_int32(file, 0);
  159. quicktime_write_int16(file, 2);
  160. quicktime_write_int16(file, 16);
  161. quicktime_write_int32(file, 0);
  162. quicktime_write_int16(file, table->sample_rate);
  163. quicktime_write_int16(file, 0);
  164. quicktime_write_esds_audio(file, &(table->esds),
  165. file->atracks[0].track->tkhd.track_id);
  166. } else {
  167. quicktime_write_int16(file, table->version);
  168. quicktime_write_int16(file, table->revision);
  169. quicktime_write_data(file, table->vendor, 4);
  170. quicktime_write_int16(file, table->channels);
  171. quicktime_write_int16(file, table->sample_size);
  172. quicktime_write_int16(file, table->compression_id);
  173. quicktime_write_int16(file, table->packet_size);
  174. quicktime_write_fixed32(file, table->sample_rate);
  175. }
  176. }
  177. int quicktime_read_stsd_video(quicktime_t *file, quicktime_stsd_table_t *table, quicktime_atom_t *parent_atom)
  178. {
  179. quicktime_atom_t leaf_atom;
  180. int len;
  181. table->version = quicktime_read_int16(file);
  182. table->revision = quicktime_read_int16(file);
  183. quicktime_read_data(file, table->vendor, 4);
  184. table->temporal_quality = quicktime_read_int32(file);
  185. table->spatial_quality = quicktime_read_int32(file);
  186. table->width = quicktime_read_int16(file);
  187. table->height = quicktime_read_int16(file);
  188. table->dpi_horizontal = quicktime_read_fixed32(file);
  189. table->dpi_vertical = quicktime_read_fixed32(file);
  190. table->data_size = quicktime_read_int32(file);
  191. table->frames_per_sample = quicktime_read_int16(file);
  192. len = quicktime_read_char(file);
  193. quicktime_read_data(file, table->compressor_name, 31);
  194. table->depth = quicktime_read_int16(file);
  195. table->ctab_id = quicktime_read_int16(file);
  196. while(quicktime_position(file) < parent_atom->end)
  197. {
  198. quicktime_atom_read_header(file, &leaf_atom);
  199. if(quicktime_atom_is(&leaf_atom, "ctab"))
  200. {
  201. quicktime_read_ctab(file, &(table->ctab));
  202. }
  203. else
  204. if(quicktime_atom_is(&leaf_atom, "gama"))
  205. {
  206. table->gamma= quicktime_read_fixed32(file);
  207. }
  208. else
  209. if(quicktime_atom_is(&leaf_atom, "fiel"))
  210. {
  211. table->fields = quicktime_read_char(file);
  212. table->field_dominance = quicktime_read_char(file);
  213. }
  214. else
  215. /*  if(quicktime_atom_is(&leaf_atom, "mjqt")) */
  216. /*  { */
  217. /*  quicktime_read_mjqt(file, &(table->mjqt)); */
  218. /*  } */
  219. /*  else */
  220. /*  if(quicktime_atom_is(&leaf_atom, "mjht")) */
  221. /*  { */
  222. /*  quicktime_read_mjht(file, &(table->mjht)); */
  223. /*  } */
  224. /*  else */
  225. if(quicktime_atom_is(&leaf_atom, "esds"))
  226. {
  227. quicktime_read_esds(file, &(table->esds));
  228. quicktime_atom_skip(file, &leaf_atom);
  229. }
  230. else
  231. {
  232. quicktime_atom_skip(file, &leaf_atom);
  233. }
  234. }
  235. }
  236. int quicktime_write_stsd_video(quicktime_t *file, quicktime_stsd_table_t *table)
  237. {
  238. if (file->use_mp4) {
  239. int i;
  240. for (i = 0; i < 4; i++) {
  241. quicktime_write_int32(file, 0);
  242. }
  243. quicktime_write_int16(file, table->width);
  244. quicktime_write_int16(file, table->height);
  245. quicktime_write_int32(file, 0x00480000);
  246. quicktime_write_int32(file, 0x00480000);
  247. quicktime_write_int32(file, 0);
  248. quicktime_write_int16(file, 1);
  249. for (i = 0; i < 32; i++) {
  250. quicktime_write_char(file, 0);
  251. }
  252. quicktime_write_int16(file, 24);
  253. quicktime_write_int16(file, -1);
  254. quicktime_write_esds_video(file, &(table->esds),
  255. file->vtracks[0].track->tkhd.track_id);
  256. } else {
  257. quicktime_write_int16(file, table->version);
  258. quicktime_write_int16(file, table->revision);
  259. quicktime_write_data(file, table->vendor, 4);
  260. quicktime_write_int32(file, table->temporal_quality);
  261. quicktime_write_int32(file, table->spatial_quality);
  262. quicktime_write_int16(file, table->width);
  263. quicktime_write_int16(file, table->height);
  264. quicktime_write_fixed32(file, table->dpi_horizontal);
  265. quicktime_write_fixed32(file, table->dpi_vertical);
  266. quicktime_write_int32(file, table->data_size);
  267. quicktime_write_int16(file, table->frames_per_sample);
  268. quicktime_write_char(file, strlen(table->compressor_name));
  269. quicktime_write_data(file, table->compressor_name, 31);
  270. quicktime_write_int16(file, table->depth);
  271. quicktime_write_int16(file, table->ctab_id);
  272. }
  273. if(table->fields)
  274. {
  275. quicktime_atom_t atom;
  276. quicktime_atom_write_header(file, &atom, "fiel");
  277. quicktime_write_char(file, table->fields);
  278. quicktime_write_char(file, table->field_dominance);
  279. quicktime_atom_write_footer(file, &atom);
  280. }
  281. }
  282. int quicktime_mjqt_init(quicktime_mjqt_t *mjqt)
  283. {
  284. }
  285. int quicktime_mjqt_delete(quicktime_mjqt_t *mjqt)
  286. {
  287. }
  288. int quicktime_mjqt_dump(quicktime_mjqt_t *mjqt)
  289. {
  290. }
  291. int quicktime_mjht_init(quicktime_mjht_t *mjht)
  292. {
  293. }
  294. int quicktime_mjht_delete(quicktime_mjht_t *mjht)
  295. {
  296. }
  297. int quicktime_mjht_dump(quicktime_mjht_t *mjht)
  298. {
  299. }
  300. int quicktime_read_stsd_hint(quicktime_t *file, quicktime_stsd_table_t *table, quicktime_atom_t *parent_atom)
  301. {
  302. quicktime_atom_t leaf_atom;
  303. table->version = quicktime_read_int16(file);
  304. table->revision = quicktime_read_int16(file);
  305. table->maxPktSize = quicktime_read_int32(file);
  306. while(quicktime_position(file) < parent_atom->end) {
  307. quicktime_atom_read_header(file, &leaf_atom);
  308. if(quicktime_atom_is(&leaf_atom, "tims")) {
  309. quicktime_read_tims(file, &(table->tims));
  310. }
  311. quicktime_atom_skip(file, &leaf_atom);
  312. }
  313. }
  314. int quicktime_write_stsd_hint(quicktime_t *file, quicktime_stsd_table_t *table)
  315. {
  316. quicktime_write_int16(file, table->version);
  317. quicktime_write_int16(file, table->revision);
  318. quicktime_write_int32(file, table->maxPktSize);
  319. quicktime_write_tims(file, &(table->tims));
  320. }
  321. int quicktime_tims_init(quicktime_tims_t *tims)
  322. {
  323. tims->timeScale = 0;
  324. }
  325. int quicktime_read_tims(quicktime_t *file, quicktime_tims_t *tims)
  326. {
  327. tims->timeScale = quicktime_read_int32(file);
  328. }
  329. int quicktime_write_tims(quicktime_t *file, quicktime_tims_t *tims)
  330. {
  331. quicktime_atom_t atom;
  332. quicktime_atom_write_header(file, &atom, "tims");
  333. quicktime_write_int32(file, tims->timeScale);
  334. quicktime_atom_write_footer(file, &atom);
  335. }