Ap4SampleEntry.cpp
上传用户:xjjlds
上传日期:2015-12-05
资源大小:22823k
文件大小:38k
源码类别:

多媒体编程

开发平台:

Visual C++

  1. /*****************************************************************
  2. |
  3. |    AP4 - sample entries
  4. |
  5. |    Copyright 2002 Gilles Boccon-Gibod & Julien Boeuf
  6. |
  7. |
  8. |    This file is part of Bento4/AP4 (MP4 Atom Processing Library).
  9. |
  10. |    Unless you have obtained Bento4 under a difference license,
  11. |    this version of Bento4 is Bento4|GPL.
  12. |    Bento4|GPL is free software; you can redistribute it and/or modify
  13. |    it under the terms of the GNU General Public License as published by
  14. |    the Free Software Foundation; either version 2, or (at your option)
  15. |    any later version.
  16. |
  17. |    Bento4|GPL is distributed in the hope that it will be useful,
  18. |    but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. |    GNU General Public License for more details.
  21. |
  22. |    You should have received a copy of the GNU General Public License
  23. |    along with Bento4|GPL; see the file COPYING.  If not, write to the
  24. |    Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  25. |    02111-1307, USA.
  26. |
  27.  ****************************************************************/
  28. /*----------------------------------------------------------------------
  29. |       includes
  30. +---------------------------------------------------------------------*/
  31. #include "Ap4SampleEntry.h"
  32. #include "Ap4Utils.h"
  33. #include "Ap4AtomFactory.h"
  34. #include "Ap4TimsAtom.h"
  35. #include "Ap4SampleDescription.h"
  36. #include "Ap4FtabAtom.h"
  37. /*----------------------------------------------------------------------
  38. |       AP4_SampleEntry::AP4_SampleEntry
  39. +---------------------------------------------------------------------*/
  40. AP4_SampleEntry::AP4_SampleEntry(AP4_Atom::Type format,
  41.                                  AP4_UI16       data_reference_index) :
  42.     AP4_ContainerAtom(format, AP4_ATOM_HEADER_SIZE+8, false),
  43.     m_DataReferenceIndex(data_reference_index)
  44. {
  45.     m_Reserved1[0] = 0;
  46.     m_Reserved1[1] = 0;
  47.     m_Reserved1[2] = 0;
  48.     m_Reserved1[3] = 0;
  49.     m_Reserved1[4] = 0;
  50.     m_Reserved1[5] = 0;
  51. }
  52. /*----------------------------------------------------------------------
  53. |       AP4_SampleEntry::AP4_SampleEntry
  54. +---------------------------------------------------------------------*/
  55. AP4_SampleEntry::AP4_SampleEntry(AP4_Atom::Type format,
  56.                                  AP4_Size       size) :
  57.     AP4_ContainerAtom(format, size)
  58. {
  59. }
  60. /*----------------------------------------------------------------------
  61. |       AP4_SampleEntry::AP4_SampleEntry
  62. +---------------------------------------------------------------------*/
  63. AP4_SampleEntry::AP4_SampleEntry(AP4_Atom::Type   format,
  64.                                  AP4_Size         size,
  65.                                  AP4_ByteStream&  stream,
  66.                                  AP4_AtomFactory& atom_factory) :
  67.     AP4_ContainerAtom(format, size)
  68. {
  69.     // read the fields before the children atoms
  70.     AP4_Size fields_size = GetFieldsSize();
  71.     ReadFields(stream);
  72.     // read children atoms (ex: esds and maybe others)
  73.     ReadChildren(atom_factory, stream, size-AP4_ATOM_HEADER_SIZE-fields_size);
  74. }
  75. /*----------------------------------------------------------------------
  76. |       AP4_SampleEntry::GetFieldsSize
  77. +---------------------------------------------------------------------*/
  78. AP4_Size
  79. AP4_SampleEntry::GetFieldsSize()
  80. {
  81.     return 8;
  82. }
  83. /*----------------------------------------------------------------------
  84. |       AP4_SampleEntry::ReadFields
  85. +---------------------------------------------------------------------*/
  86. AP4_Result
  87. AP4_SampleEntry::ReadFields(AP4_ByteStream& stream)
  88. {
  89.     stream.Read(m_Reserved1, sizeof(m_Reserved1), NULL);
  90.     stream.ReadUI16(m_DataReferenceIndex);
  91.     return AP4_SUCCESS;
  92. }
  93. /*----------------------------------------------------------------------
  94. |       AP4_SampleEntry::WriteFields
  95. +---------------------------------------------------------------------*/
  96. AP4_Result
  97. AP4_SampleEntry::WriteFields(AP4_ByteStream& stream)
  98. {
  99.     AP4_Result result;
  100.     
  101.     // reserved1
  102.     result = stream.Write(m_Reserved1, sizeof(m_Reserved1));
  103.     if (AP4_FAILED(result)) return result;
  104.     // data reference index
  105.     result = stream.WriteUI16(m_DataReferenceIndex);
  106.     if (AP4_FAILED(result)) return result;
  107.     
  108.     return result;
  109. }
  110. /*----------------------------------------------------------------------
  111. |       AP4_SampleEntry::Write
  112. +---------------------------------------------------------------------*/
  113. AP4_Result
  114. AP4_SampleEntry::Write(AP4_ByteStream& stream)
  115. {
  116.     AP4_Result result;
  117.     // write the header
  118.     result = WriteHeader(stream);
  119.     if (AP4_FAILED(result)) return result;
  120.     // write the fields
  121.     result = WriteFields(stream);
  122.     if (AP4_FAILED(result)) return result;
  123.     // write the children atoms
  124.     return m_Children.Apply(AP4_AtomListWriter(stream));
  125. }
  126. /*----------------------------------------------------------------------
  127. |       AP4_SampleEntry::InspectFields
  128. +---------------------------------------------------------------------*/
  129. AP4_Result
  130. AP4_SampleEntry::InspectFields(AP4_AtomInspector& inspector)
  131. {
  132.     inspector.AddField("data_reference_index", m_DataReferenceIndex);
  133.     return AP4_SUCCESS;
  134. }
  135. /*----------------------------------------------------------------------
  136. |       AP4_SampleEntry::Inspect
  137. +---------------------------------------------------------------------*/
  138. AP4_Result
  139. AP4_SampleEntry::Inspect(AP4_AtomInspector& inspector)
  140. {
  141.     // inspect the header
  142.     InspectHeader(inspector);
  143.     // inspect the fields
  144.     InspectFields(inspector);
  145.     // inspect children
  146.     m_Children.Apply(AP4_AtomListInspector(inspector));
  147.     // finish
  148.     inspector.EndElement();
  149.     return AP4_SUCCESS;
  150. }
  151. /*----------------------------------------------------------------------
  152. |       AP4_SampleEntry::OnChildChanged
  153. +---------------------------------------------------------------------*/
  154. void
  155. AP4_SampleEntry::OnChildChanged(AP4_Atom*)
  156. {
  157.     // remcompute our size
  158.     m_Size = GetHeaderSize()+GetFieldsSize();
  159.     m_Children.Apply(AP4_AtomSizeAdder(m_Size));
  160.     // update our parent
  161.     if (m_Parent) m_Parent->OnChildChanged(this);
  162. }
  163. /*----------------------------------------------------------------------
  164. |       AP4_SampleEntry::ToSampleDescription
  165. +---------------------------------------------------------------------*/
  166. AP4_SampleDescription*
  167. AP4_SampleEntry::ToSampleDescription()
  168. {
  169.     return new AP4_UnknownSampleDescription(this);
  170. }
  171. /*----------------------------------------------------------------------
  172. |       AP4_MpegSampleEntry::AP4_MpegSampleEntry
  173. +---------------------------------------------------------------------*/
  174. AP4_MpegSampleEntry::AP4_MpegSampleEntry(AP4_Atom::Type    format, 
  175.                                          AP4_EsDescriptor* descriptor) :
  176.     AP4_SampleEntry(format)
  177. {
  178.     if (descriptor) AddChild(new AP4_EsdsAtom(descriptor));
  179. }
  180. /*----------------------------------------------------------------------
  181. |       AP4_MpegSampleEntry::AP4_MpegSampleEntry
  182. +---------------------------------------------------------------------*/
  183. AP4_MpegSampleEntry::AP4_MpegSampleEntry(AP4_Atom::Type format,
  184.                                          AP4_Size       size) :
  185.     AP4_SampleEntry(format, size)
  186. {
  187. }
  188. /*----------------------------------------------------------------------
  189. |       AP4_MpegSampleEntry::AP4_MpegSampleEntry
  190. +---------------------------------------------------------------------*/
  191. AP4_MpegSampleEntry::AP4_MpegSampleEntry(AP4_Atom::Type   format,
  192.                                          AP4_Size         size,
  193.                                          AP4_ByteStream&  stream,
  194.                                          AP4_AtomFactory& atom_factory) :
  195.     AP4_SampleEntry(format, size, stream, atom_factory)
  196. {
  197. }
  198. /*----------------------------------------------------------------------
  199. |       AP4_MpegSampleEntry::AP4_MpegSampleEntry
  200. +---------------------------------------------------------------------*/
  201. const AP4_DecoderConfigDescriptor* 
  202. AP4_MpegSampleEntry::GetDecoderConfigDescriptor()
  203. {
  204.     AP4_Atom* child = GetChild(AP4_ATOM_TYPE_ESDS);
  205.     if (child) {
  206.         AP4_EsdsAtom* esds = (AP4_EsdsAtom*)child;
  207.         // get the es descriptor
  208.         const AP4_EsDescriptor* es_desc = esds->GetEsDescriptor();
  209.         if (es_desc == NULL) return NULL;
  210.         // get the decoder config descriptor
  211.         return es_desc->GetDecoderConfigDescriptor();
  212.     } else {
  213.         return NULL;
  214.     }
  215. }
  216. /*----------------------------------------------------------------------
  217. |       AP4_Mp4sSampleEntry::AP4_Mp4sSampleEntry
  218. +---------------------------------------------------------------------*/
  219. AP4_Mp4sSampleEntry::AP4_Mp4sSampleEntry(AP4_EsDescriptor* descriptor) :
  220.     AP4_MpegSampleEntry(AP4_ATOM_TYPE_MP4S, descriptor)
  221. {
  222. }
  223. /*----------------------------------------------------------------------
  224. |       AP4_Mp4sSampleEntry::AP4_Mp4sSampleEntry
  225. +---------------------------------------------------------------------*/
  226. AP4_Mp4sSampleEntry::AP4_Mp4sSampleEntry(AP4_Size         size,
  227.                                          AP4_ByteStream&  stream,
  228.                                          AP4_AtomFactory& atom_factory) :
  229.     AP4_MpegSampleEntry(AP4_ATOM_TYPE_MP4S, size, stream, atom_factory)
  230. {
  231. }
  232. /*----------------------------------------------------------------------
  233. |       AP4_Mp4sSampleEntry::ToSampleDescription
  234. +---------------------------------------------------------------------*/
  235. AP4_SampleDescription*
  236. AP4_Mp4sSampleEntry::ToSampleDescription()
  237. {
  238.     // get the decoder config descriptor
  239.     const AP4_DecoderConfigDescriptor* dc_desc;
  240.     dc_desc = GetDecoderConfigDescriptor();
  241.     if (dc_desc == NULL) return NULL;
  242.     const AP4_DataBuffer* dsi = NULL;
  243.     const AP4_DecoderSpecificInfoDescriptor* dsi_desc =
  244.         dc_desc->GetDecoderSpecificInfoDescriptor();
  245.     if (dsi_desc != NULL) {
  246.         dsi = &dsi_desc->GetDecoderSpecificInfo();
  247.     }
  248.     // create a sample description
  249.     return new AP4_MpegSystemSampleDescription(
  250.         dc_desc->GetStreamType(),
  251.         dc_desc->GetObjectTypeIndication(),
  252.         dsi,
  253.         dc_desc->GetBufferSize(),
  254.         dc_desc->GetMaxBitrate(),
  255.         dc_desc->GetAvgBitrate());
  256. }
  257. /*----------------------------------------------------------------------
  258. |       AP4_AudioSampleEntry::AP4_AudioSampleEntry
  259. +---------------------------------------------------------------------*/
  260. AP4_AudioSampleEntry::AP4_AudioSampleEntry(AP4_Atom::Type    format,
  261.                                            AP4_EsDescriptor* descriptor,
  262.                                            AP4_UI32          sample_rate,
  263.                                            AP4_UI16          sample_size,
  264.                                            AP4_UI16          channel_count) :
  265.     AP4_MpegSampleEntry(format, descriptor),
  266.     m_SampleRate(sample_rate),
  267.     m_ChannelCount(channel_count),
  268.     m_SampleSize(sample_size)
  269. {
  270.     m_Predefined1 = 0;
  271.     memset(m_Reserved2, 0, sizeof(m_Reserved2));
  272.     m_Reserved3 = 0;
  273.     m_Size += 20;
  274. }
  275. /*----------------------------------------------------------------------
  276. |       AP4_AudioSampleEntry::AP4_AudioSampleEntry
  277. +---------------------------------------------------------------------*/
  278. AP4_AudioSampleEntry::AP4_AudioSampleEntry(AP4_Atom::Type   format,
  279.                                            AP4_Size         size,
  280.                                            AP4_ByteStream&  stream,
  281.                                            AP4_AtomFactory& atom_factory) :
  282.     AP4_MpegSampleEntry(format, size)
  283. {
  284.     // read fields
  285.     AP4_Size fields_size = GetFieldsSize();
  286.     ReadFields(stream);
  287.     // read children atoms (ex: esds and maybe others)
  288.     ReadChildren(atom_factory, stream, size-AP4_ATOM_HEADER_SIZE-fields_size);
  289. }
  290.     
  291. /*----------------------------------------------------------------------
  292. |       AP4_AudioSampleEntry::GetFieldsSize
  293. +---------------------------------------------------------------------*/
  294. AP4_Size
  295. AP4_AudioSampleEntry::GetFieldsSize()
  296. {
  297.     return AP4_SampleEntry::GetFieldsSize()+20;
  298. }
  299. /*----------------------------------------------------------------------
  300. |       AP4_AudioSampleEntry::ReadFields
  301. +---------------------------------------------------------------------*/
  302. AP4_Result
  303. AP4_AudioSampleEntry::ReadFields(AP4_ByteStream& stream)
  304. {
  305.     // sample entry
  306.     AP4_Result result = AP4_SampleEntry::ReadFields(stream);
  307.     if (result < 0) return result;
  308.     // read the fields of this class
  309.     stream.Read(m_Reserved2, sizeof(m_Reserved2), NULL);
  310.     stream.ReadUI16(m_ChannelCount);
  311.     stream.ReadUI16(m_SampleSize);
  312.     stream.ReadUI16(m_Predefined1);
  313.     stream.ReadUI16(m_Reserved3);
  314.     stream.ReadUI32(m_SampleRate);
  315.     return AP4_SUCCESS;
  316. }
  317. /*----------------------------------------------------------------------
  318. |       AP4_AudioSampleEntry::WriteFields
  319. +---------------------------------------------------------------------*/
  320. AP4_Result
  321. AP4_AudioSampleEntry::WriteFields(AP4_ByteStream& stream)
  322. {
  323.     AP4_Result result;
  324.     
  325.     // write the fields of the base class
  326.     result = AP4_SampleEntry::WriteFields(stream);
  327.     // reserved2
  328.     result = stream.Write(m_Reserved2, sizeof(m_Reserved2));
  329.     if (AP4_FAILED(result)) return result;
  330.     // channel count
  331.     result = stream.WriteUI16(m_ChannelCount);
  332.     if (AP4_FAILED(result)) return result;
  333.     
  334.     // sample size 
  335.     result = stream.WriteUI16(m_SampleSize);
  336.     if (AP4_FAILED(result)) return result;
  337.     // predefined1
  338.     result = stream.WriteUI16(m_Predefined1);
  339.     if (AP4_FAILED(result)) return result;
  340.     // reserved3
  341.     result = stream.WriteUI16(m_Reserved3);
  342.     if (AP4_FAILED(result)) return result;
  343.     // sample rate
  344.     result = stream.WriteUI32(m_SampleRate);
  345.     if (AP4_FAILED(result)) return result;
  346.     return result;
  347. }
  348. /*----------------------------------------------------------------------
  349. |       AP4_AudioSampleEntry::InspectFields
  350. +---------------------------------------------------------------------*/
  351. AP4_Result
  352. AP4_AudioSampleEntry::InspectFields(AP4_AtomInspector& inspector)
  353. {
  354.     // dump the fields from the base class
  355.     AP4_SampleEntry::InspectFields(inspector);
  356.     // fields
  357.     inspector.AddField("channel_count", m_ChannelCount);
  358.     inspector.AddField("sample_size", m_SampleSize);
  359.     inspector.AddField("sample_rate", m_SampleRate>>16);
  360.     return AP4_SUCCESS;
  361. }
  362. /*----------------------------------------------------------------------
  363. |       AP4_AudioSampleEntry::ToSampleDescription
  364. +---------------------------------------------------------------------*/
  365. AP4_SampleDescription*
  366. AP4_AudioSampleEntry::ToSampleDescription()
  367. {
  368.     // get the decoder config descriptor
  369.     const AP4_DecoderConfigDescriptor* dc_desc;
  370.     dc_desc = GetDecoderConfigDescriptor();
  371.     if (dc_desc == NULL) return NULL;
  372.     const AP4_DataBuffer* dsi = NULL;
  373.     const AP4_DecoderSpecificInfoDescriptor* dsi_desc =
  374.         dc_desc->GetDecoderSpecificInfoDescriptor();
  375.     if (dsi_desc != NULL) {
  376.         dsi = &dsi_desc->GetDecoderSpecificInfo();
  377.     }
  378.     // create a sample description
  379.     return new AP4_MpegAudioSampleDescription(
  380.         dc_desc->GetObjectTypeIndication(),
  381.         m_SampleRate>>16,
  382.         m_SampleSize,
  383.         m_ChannelCount,
  384.         dsi,
  385.         dc_desc->GetBufferSize(),
  386.         dc_desc->GetMaxBitrate(),
  387.         dc_desc->GetAvgBitrate());
  388. }
  389. /*----------------------------------------------------------------------
  390. |       AP4_Mp4aSampleEntry::AP4_Mp4aSampleEntry
  391. +---------------------------------------------------------------------*/
  392. AP4_Mp4aSampleEntry::AP4_Mp4aSampleEntry(AP4_UI32          sample_rate, 
  393.                                          AP4_UI16          sample_size,
  394.                                          AP4_UI16          channel_count,
  395.                                          AP4_EsDescriptor* descriptor) :
  396.     AP4_AudioSampleEntry(AP4_ATOM_TYPE_MP4A, 
  397.                          descriptor,
  398.                          sample_rate, 
  399.                          sample_size, 
  400.                          channel_count)
  401. {
  402. }
  403. /*----------------------------------------------------------------------
  404. |       AP4_Mp4aSampleEntry::AP4_Mp4aSampleEntry
  405. +---------------------------------------------------------------------*/
  406. AP4_Mp4aSampleEntry::AP4_Mp4aSampleEntry(AP4_Size         size,
  407.                                          AP4_ByteStream&  stream,
  408.                                          AP4_AtomFactory& atom_factory) :
  409.     AP4_AudioSampleEntry(AP4_ATOM_TYPE_MP4A, size, stream, atom_factory)
  410. {
  411. }
  412. /*----------------------------------------------------------------------
  413. |       AP4_VisualSampleEntry::AP4_VisualSampleEntry
  414. +---------------------------------------------------------------------*/
  415. AP4_VisualSampleEntry::AP4_VisualSampleEntry(
  416.     AP4_Atom::Type    format, 
  417.     AP4_EsDescriptor* descriptor,
  418.     AP4_UI16          width,
  419.     AP4_UI16          height,
  420.     AP4_UI16          depth,
  421.     const char*       compressor_name) :
  422.     AP4_MpegSampleEntry(format, descriptor),
  423.     m_Predefined1(0),
  424.     m_Reserved2(0),
  425.     m_Width(width),
  426.     m_Height(height),
  427.     m_HorizResolution(0x00480000),
  428.     m_VertResolution(0x00480000),
  429.     m_Reserved3(0),
  430.     m_FrameCount(1),
  431.     m_CompressorName(compressor_name),
  432.     m_Depth(depth),
  433.     m_Predefined3(0xFFFF)
  434. {
  435.     memset(m_Predefined2, 0, sizeof(m_Predefined2));
  436.     m_Size += 70;
  437. }
  438. /*----------------------------------------------------------------------
  439. |       AP4_VisualSampleEntry::AP4_VisualSampleEntry
  440. +---------------------------------------------------------------------*/
  441. AP4_VisualSampleEntry::AP4_VisualSampleEntry(AP4_Atom::Type   format,
  442.                                              AP4_Size         size, 
  443.                                              AP4_ByteStream&  stream,
  444.                                              AP4_AtomFactory& atom_factory) :
  445.     AP4_MpegSampleEntry(format, size)
  446. {
  447.     // read fields
  448.     AP4_Size fields_size = GetFieldsSize();
  449.     ReadFields(stream);
  450.     // read children atoms (ex: esds and maybe others)
  451.     ReadChildren(atom_factory, stream, size-AP4_ATOM_HEADER_SIZE-fields_size);
  452. }
  453. /*----------------------------------------------------------------------
  454. |       AP4_VisualSampleEntry::GetFieldsSize
  455. +---------------------------------------------------------------------*/
  456. AP4_Size
  457. AP4_VisualSampleEntry::GetFieldsSize()
  458. {
  459.     return AP4_SampleEntry::GetFieldsSize()+70;
  460. }
  461. /*----------------------------------------------------------------------
  462. |       AP4_VisualSampleEntry::ReadFields
  463. +---------------------------------------------------------------------*/
  464. AP4_Result
  465. AP4_VisualSampleEntry::ReadFields(AP4_ByteStream& stream)
  466. {
  467.     // sample entry
  468.     AP4_Result result = AP4_SampleEntry::ReadFields(stream);
  469.     if (result < 0) return result;
  470.     // read fields from this class
  471.     stream.ReadUI16(m_Predefined1);
  472.     stream.ReadUI16(m_Reserved2);
  473.     stream.Read(m_Predefined2, sizeof(m_Predefined2), NULL);
  474.     stream.ReadUI16(m_Width);
  475.     stream.ReadUI16(m_Height);
  476.     stream.ReadUI32(m_HorizResolution);
  477.     stream.ReadUI32(m_VertResolution);
  478.     stream.ReadUI32(m_Reserved3);
  479.     stream.ReadUI16(m_FrameCount);
  480.     char compressor_name[33];
  481.     stream.Read(compressor_name, 32);
  482.     int name_length = compressor_name[0];
  483.     if (name_length < 32) {
  484.         compressor_name[name_length+1] = 0;
  485.         m_CompressorName = &compressor_name[1];
  486.     }
  487.     stream.ReadUI16(m_Depth);
  488.     stream.ReadUI16(m_Predefined3);
  489.     return AP4_SUCCESS;
  490. }
  491. /*----------------------------------------------------------------------
  492. |       AP4_VisualSampleEntry::WriteFields
  493. +---------------------------------------------------------------------*/
  494. AP4_Result
  495. AP4_VisualSampleEntry::WriteFields(AP4_ByteStream& stream)
  496. {
  497.     AP4_Result result;
  498.         
  499.     // write the fields of the base class
  500.     result = AP4_SampleEntry::WriteFields(stream);
  501.     if (AP4_FAILED(result)) return result;
  502.     // predefined1
  503.     result = stream.WriteUI16(m_Predefined1);
  504.     if (AP4_FAILED(result)) return result;
  505.     
  506.     // reserved2
  507.     result = stream.WriteUI16(m_Reserved2);
  508.     if (AP4_FAILED(result)) return result;
  509.     
  510.     // predefined2
  511.     result = stream.Write(m_Predefined2, sizeof(m_Predefined2));
  512.     if (AP4_FAILED(result)) return result;
  513.     
  514.     // width
  515.     result = stream.WriteUI16(m_Width);
  516.     if (AP4_FAILED(result)) return result;
  517.     
  518.     // height
  519.     result = stream.WriteUI16(m_Height);
  520.     if (AP4_FAILED(result)) return result;
  521.     
  522.     // horizontal resolution
  523.     result = stream.WriteUI32(m_HorizResolution);
  524.     if (AP4_FAILED(result)) return result;
  525.     
  526.     // vertical resolution
  527.     result = stream.WriteUI32(m_VertResolution);
  528.     if (AP4_FAILED(result)) return result;
  529.     
  530.     // reserved3
  531.     result = stream.WriteUI32(m_Reserved3);
  532.     if (AP4_FAILED(result)) return result;
  533.     
  534.     // frame count
  535.     result = stream.WriteUI16(m_FrameCount);
  536.     if (AP4_FAILED(result)) return result;
  537.     
  538.     // compressor name
  539.     unsigned char compressor_name[32];
  540.     unsigned int name_length = m_CompressorName.length();
  541.     if (name_length > 31) name_length = 31;
  542.     compressor_name[0] = name_length;
  543.     for (unsigned int i=0; i<name_length; i++) {
  544.         compressor_name[i+1] = m_CompressorName[i];
  545.     }
  546.     for (unsigned int i=name_length+1; i<32; i++) {
  547.         compressor_name[i] = 0;
  548.     }
  549.     result = stream.Write(compressor_name, 32);
  550.     if (AP4_FAILED(result)) return result;
  551.     
  552.     // depth
  553.     result = stream.WriteUI16(m_Depth);
  554.     if (AP4_FAILED(result)) return result;
  555.     
  556.     // predefined3
  557.     result = stream.WriteUI16(m_Predefined3);
  558.     if (AP4_FAILED(result)) return result;
  559.     
  560.     return result;
  561. }
  562. /*----------------------------------------------------------------------
  563. |       AP4_VisualSampleEntry::InspectFields
  564. +---------------------------------------------------------------------*/
  565. AP4_Result
  566. AP4_VisualSampleEntry::InspectFields(AP4_AtomInspector& inspector)
  567. {
  568.     // dump the fields of the base class
  569.     AP4_SampleEntry::InspectFields(inspector);
  570.     // fields
  571.     inspector.AddField("width", m_Width);
  572.     inspector.AddField("height", m_Height);
  573.     inspector.AddField("compressor", m_CompressorName.c_str());
  574.     return AP4_SUCCESS;
  575. }
  576. /*----------------------------------------------------------------------
  577. |       AP4_VisualSampleEntry::ToSampleDescription
  578. +---------------------------------------------------------------------*/
  579. AP4_SampleDescription*
  580. AP4_VisualSampleEntry::ToSampleDescription()
  581. {
  582.     // get the decoder config descriptor
  583.     const AP4_DecoderConfigDescriptor* dc_desc;
  584.     dc_desc = GetDecoderConfigDescriptor();
  585.     if (dc_desc == NULL) return NULL;
  586.     const AP4_DataBuffer* dsi = NULL;
  587.     const AP4_DecoderSpecificInfoDescriptor* dsi_desc =
  588.         dc_desc->GetDecoderSpecificInfoDescriptor();
  589.     if (dsi_desc != NULL) {
  590.         dsi = &dsi_desc->GetDecoderSpecificInfo();
  591.     }
  592.     // create a sample description
  593.     return new AP4_MpegVideoSampleDescription(
  594.         dc_desc->GetObjectTypeIndication(),
  595.         m_Width,
  596.         m_Height,
  597.         m_Depth,
  598.         m_CompressorName.c_str(),
  599.         dsi,
  600.         dc_desc->GetBufferSize(),
  601.         dc_desc->GetMaxBitrate(),
  602.         dc_desc->GetAvgBitrate());
  603. }
  604. /*----------------------------------------------------------------------
  605. |       AP4_Mp4vSampleEntry::AP4_Mp4vSampleEntry
  606. +---------------------------------------------------------------------*/
  607. AP4_Mp4vSampleEntry::AP4_Mp4vSampleEntry(AP4_UI16          width,
  608.                                          AP4_UI16          height,
  609.                                          AP4_UI16          depth,
  610.                                          const char*       compressor_name,
  611.                                          AP4_EsDescriptor* descriptor) :
  612.     AP4_VisualSampleEntry(AP4_ATOM_TYPE_MP4V, 
  613.                           descriptor,
  614.                           width, 
  615.                           height, 
  616.                           depth, 
  617.                           compressor_name)
  618. {
  619. }
  620. /*----------------------------------------------------------------------
  621. |       AP4_Mp4vSampleEntry::AP4_Mp4aSampleEntry
  622. +---------------------------------------------------------------------*/
  623. AP4_Mp4vSampleEntry::AP4_Mp4vSampleEntry(AP4_Size         size,
  624.                                          AP4_ByteStream&  stream,
  625.                                          AP4_AtomFactory& atom_factory) :
  626.     AP4_VisualSampleEntry(AP4_ATOM_TYPE_MP4V, size, stream, atom_factory)
  627. {
  628. }
  629. /*----------------------------------------------------------------------
  630. |       AP4_Avc1SampleEntry::AP4_Avc1SampleEntry
  631. +---------------------------------------------------------------------*/
  632. AP4_Avc1SampleEntry::AP4_Avc1SampleEntry(AP4_UI16          width,
  633.                                          AP4_UI16          height,
  634.                                          AP4_UI16          depth,
  635.                                          const char*       compressor_name,
  636.                                          AP4_EsDescriptor* descriptor) :
  637.     AP4_VisualSampleEntry(AP4_ATOM_TYPE_AVC1, 
  638.                           descriptor,
  639.                           width, 
  640.                           height, 
  641.                           depth, 
  642.                           compressor_name)
  643. {
  644. }
  645. /*----------------------------------------------------------------------
  646. |       AP4_Avc1SampleEntry::AP4_Avc1SampleEntry
  647. +---------------------------------------------------------------------*/
  648. AP4_Avc1SampleEntry::AP4_Avc1SampleEntry(AP4_Size         size,
  649.                                          AP4_ByteStream&  stream,
  650.                                          AP4_AtomFactory& atom_factory) :
  651.     AP4_VisualSampleEntry(AP4_ATOM_TYPE_AVC1, size, stream, atom_factory)
  652. {
  653. }
  654. /*----------------------------------------------------------------------
  655. |       AP4_RtpHintSampleEntry::AP4_RtpHintSampleEntry
  656. +---------------------------------------------------------------------*/
  657. AP4_RtpHintSampleEntry::AP4_RtpHintSampleEntry(AP4_UI16 hint_track_version,
  658.                                                AP4_UI16 highest_compatible_version,
  659.                                                AP4_UI32 max_packet_size,
  660.                                                AP4_UI32 timescale):
  661.     AP4_SampleEntry(AP4_ATOM_TYPE_RTP),
  662.     m_HintTrackVersion(hint_track_version),
  663.     m_HighestCompatibleVersion(highest_compatible_version),
  664.     m_MaxPacketSize(max_packet_size)
  665. {
  666.     // build an atom for timescale
  667.     AddChild(new AP4_TimsAtom(timescale));
  668. }
  669. /*----------------------------------------------------------------------
  670. |       AP4_RtpHintSampleEntry::AP4_RtpHintSampleEntry
  671. +---------------------------------------------------------------------*/
  672. AP4_RtpHintSampleEntry::AP4_RtpHintSampleEntry(AP4_Size         size,
  673.                                                AP4_ByteStream&  stream,
  674.                                                AP4_AtomFactory& atom_factory): 
  675.     AP4_SampleEntry(AP4_ATOM_TYPE_RTP, size)
  676. {
  677.     // read fields
  678.     AP4_Size fields_size = GetFieldsSize();
  679.     ReadFields(stream);
  680.     // read children atoms (ex: esds and maybe others)
  681.     ReadChildren(atom_factory, stream, size-AP4_ATOM_HEADER_SIZE-fields_size);
  682. }
  683. /*----------------------------------------------------------------------
  684. |       AP4_RtpHintSampleEntry::~AP4_RtpHintSampleEntry
  685. +---------------------------------------------------------------------*/
  686. AP4_RtpHintSampleEntry::~AP4_RtpHintSampleEntry() 
  687. {
  688. }
  689. /*----------------------------------------------------------------------
  690. |       AP4_RtpHintSampleEntry::GetFieldsSize
  691. +---------------------------------------------------------------------*/
  692. AP4_Size
  693. AP4_RtpHintSampleEntry::GetFieldsSize()
  694. {
  695.     return AP4_SampleEntry::GetFieldsSize()+8;
  696. }
  697. /*----------------------------------------------------------------------
  698. |       AP4_RtpHintSampleEntry::ReadFields
  699. +---------------------------------------------------------------------*/
  700. AP4_Result
  701. AP4_RtpHintSampleEntry::ReadFields(AP4_ByteStream& stream)
  702. {
  703.     // sample entry
  704.     AP4_Result result = AP4_SampleEntry::ReadFields(stream);
  705.     if (result < 0) return result;
  706.     // data
  707.     result = stream.ReadUI16(m_HintTrackVersion);
  708.     if (AP4_FAILED(result)) return result;
  709.     result = stream.ReadUI16(m_HighestCompatibleVersion);
  710.     if (AP4_FAILED(result)) return result;
  711.     result = stream.ReadUI32(m_MaxPacketSize);
  712.     if (AP4_FAILED(result)) return result;
  713.     return AP4_SUCCESS;
  714. }
  715. /*----------------------------------------------------------------------
  716. |       AP4_RtpHintSampleEntry::WriteFields
  717. +---------------------------------------------------------------------*/
  718. AP4_Result
  719. AP4_RtpHintSampleEntry::WriteFields(AP4_ByteStream& stream)
  720. {
  721.     // sample entry
  722.     AP4_Result result = AP4_SampleEntry::WriteFields(stream);
  723.     if (AP4_FAILED(result)) return result;
  724.     
  725.     // data
  726.     result = stream.WriteUI16(m_HintTrackVersion);
  727.     if (AP4_FAILED(result)) return result;
  728.     result = stream.WriteUI16(m_HighestCompatibleVersion);
  729.     if (AP4_FAILED(result)) return result;
  730.     result = stream.WriteUI32(m_MaxPacketSize);
  731.     if (AP4_FAILED(result)) return result;
  732.     return result;
  733. }
  734. /*----------------------------------------------------------------------
  735. |       AP4_RtpHintSampleEntry::InspectFields
  736. +---------------------------------------------------------------------*/
  737. AP4_Result
  738. AP4_RtpHintSampleEntry::InspectFields(AP4_AtomInspector& inspector)
  739. {
  740.     // sample entry
  741.     AP4_SampleEntry::InspectFields(inspector);
  742.     
  743.     // fields
  744.     inspector.AddField("hint_track_version", m_HintTrackVersion);
  745.     inspector.AddField("highest_compatible_version", m_HighestCompatibleVersion);
  746.     inspector.AddField("max_packet_size", m_MaxPacketSize);
  747.     
  748.     return AP4_SUCCESS;
  749. }
  750. /*----------------------------------------------------------------------
  751. |       AP4_TextSampleEntry::AP4_TextSampleEntry
  752. +---------------------------------------------------------------------*/
  753. AP4_TextSampleEntry::AP4_TextSampleEntry(AP4_Size         size,
  754.                                          AP4_ByteStream&  stream,
  755.                                          AP4_AtomFactory& atom_factory): 
  756.     AP4_SampleEntry(AP4_ATOM_TYPE_TEXT, size)
  757. {
  758.     // read fields
  759.     ReadFields(stream);
  760. }
  761. /*----------------------------------------------------------------------
  762. |       AP4_TextSampleEntry::~AP4_TextSampleEntry
  763. +---------------------------------------------------------------------*/
  764. AP4_TextSampleEntry::~AP4_TextSampleEntry() 
  765. {
  766. }
  767. /*----------------------------------------------------------------------
  768. |       AP4_TextSampleEntry::ReadFields
  769. +---------------------------------------------------------------------*/
  770. AP4_Result
  771. AP4_TextSampleEntry::ReadFields(AP4_ByteStream& stream)
  772. {
  773.     // sample entry
  774.     AP4_Result result = AP4_SampleEntry::ReadFields(stream);
  775.     if (result < 0) return result;
  776.     // data
  777.     result = stream.ReadUI32(m_Description.DisplayFlags);
  778.     if (AP4_FAILED(result)) return result;
  779.     result = stream.ReadUI32(m_Description.TextJustification);
  780.     if (AP4_FAILED(result)) return result;
  781.     result = stream.Read(&m_Description.BackgroundColor, 4);
  782.     if (AP4_FAILED(result)) return result;
  783.     result = stream.ReadUI16(m_Description.TextBox.Top);
  784.     if (AP4_FAILED(result)) return result;
  785.     result = stream.ReadUI16(m_Description.TextBox.Left);
  786.     if (AP4_FAILED(result)) return result;
  787.     result = stream.ReadUI16(m_Description.TextBox.Bottom);
  788.     if (AP4_FAILED(result)) return result;
  789.     result = stream.ReadUI16(m_Description.TextBox.Right);
  790.     if (AP4_FAILED(result)) return result;
  791.     result = stream.ReadUI16(m_Description.Style.StartChar);
  792.     if (AP4_FAILED(result)) return result;
  793.     result = stream.ReadUI16(m_Description.Style.EndChar);
  794.     if (AP4_FAILED(result)) return result;
  795.     result = stream.ReadUI16(m_Description.Style.Ascent);
  796.     if (AP4_FAILED(result)) return result;
  797.     result = stream.ReadUI16(m_Description.Style.Font.Id);
  798.     if (AP4_FAILED(result)) return result;
  799.     result = stream.ReadUI08(m_Description.Style.Font.Face);
  800.     if (AP4_FAILED(result)) return result;
  801.     result = stream.ReadUI08(m_Description.Style.Font.Size);
  802.     if (AP4_FAILED(result)) return result;
  803.     result = stream.Read(&m_Description.Style.Font.Color, 4);
  804.     if (AP4_FAILED(result)) return result;
  805.     // TODO: stream.ReadString(); -> m_Description.DefaultFontName
  806.     return AP4_SUCCESS;
  807. }
  808. /*----------------------------------------------------------------------
  809. |       AP4_TextSampleEntry::WriteFields
  810. +---------------------------------------------------------------------*/
  811. AP4_Result
  812. AP4_TextSampleEntry::WriteFields(AP4_ByteStream& stream)
  813. {
  814.     // sample entry
  815.     AP4_Result result = AP4_SampleEntry::WriteFields(stream);
  816.     if (AP4_FAILED(result)) return result;
  817.     
  818.     // TODO: data
  819.     return result;
  820. }
  821. /*----------------------------------------------------------------------
  822. |       AP4_TextSampleEntry::InspectFields
  823. +---------------------------------------------------------------------*/
  824. AP4_Result
  825. AP4_TextSampleEntry::InspectFields(AP4_AtomInspector& inspector)
  826. {
  827.     // sample entry
  828.     AP4_SampleEntry::InspectFields(inspector);
  829.     
  830.     // TODO: fields
  831.     
  832.     return AP4_SUCCESS;
  833. }
  834. /*----------------------------------------------------------------------
  835. |       AP4_Tx3gSampleEntry::AP4_Tx3gSampleEntry
  836. +---------------------------------------------------------------------*/
  837. AP4_Tx3gSampleEntry::AP4_Tx3gSampleEntry(AP4_Size         size,
  838.                                          AP4_ByteStream&  stream,
  839.                                          AP4_AtomFactory& atom_factory): 
  840.     AP4_SampleEntry(AP4_ATOM_TYPE_TX3G, size)
  841. {
  842.     // read fields
  843.     AP4_Size fields_size = GetFieldsSize();
  844.     ReadFields(stream);
  845.     // read children atoms (fdat? blnk?)
  846.     ReadChildren(atom_factory, stream, size-AP4_ATOM_HEADER_SIZE-fields_size);
  847. }
  848. /*----------------------------------------------------------------------
  849. |       AP4_Tx3gSampleEntry::~AP4_Tx3gSampleEntry
  850. +---------------------------------------------------------------------*/
  851. AP4_Tx3gSampleEntry::~AP4_Tx3gSampleEntry() 
  852. {
  853. }
  854. /*----------------------------------------------------------------------
  855. |       AP4_Tx3gSampleEntry::GetFieldsSize
  856. +---------------------------------------------------------------------*/
  857. AP4_Size
  858. AP4_Tx3gSampleEntry::GetFieldsSize()
  859. {
  860.     return AP4_SampleEntry::GetFieldsSize()+4+1+1+4+2+2+2+2+2+2+2+1+1+4;
  861. }
  862. /*----------------------------------------------------------------------
  863. |       AP4_Tx3gSampleEntry::ReadFields
  864. +---------------------------------------------------------------------*/
  865. AP4_Result
  866. AP4_Tx3gSampleEntry::ReadFields(AP4_ByteStream& stream)
  867. {
  868.     // sample entry
  869.     AP4_Result result = AP4_SampleEntry::ReadFields(stream);
  870.     if (result < 0) return result;
  871.     // data
  872.     result = stream.ReadUI32(m_Description.DisplayFlags);
  873.     if (AP4_FAILED(result)) return result;
  874.     result = stream.ReadUI08(m_Description.HorizontalJustification);
  875.     if (AP4_FAILED(result)) return result;
  876.     result = stream.ReadUI08(m_Description.VerticalJustification);
  877.     if (AP4_FAILED(result)) return result;
  878.     result = stream.Read(&m_Description.BackgroundColor, 4);
  879.     if (AP4_FAILED(result)) return result;
  880.     result = stream.ReadUI16(m_Description.TextBox.Top);
  881.     if (AP4_FAILED(result)) return result;
  882.     result = stream.ReadUI16(m_Description.TextBox.Left);
  883.     if (AP4_FAILED(result)) return result;
  884.     result = stream.ReadUI16(m_Description.TextBox.Bottom);
  885.     if (AP4_FAILED(result)) return result;
  886.     result = stream.ReadUI16(m_Description.TextBox.Right);
  887.     if (AP4_FAILED(result)) return result;
  888.     result = stream.ReadUI16(m_Description.Style.StartChar);
  889.     if (AP4_FAILED(result)) return result;
  890.     result = stream.ReadUI16(m_Description.Style.EndChar);
  891.     if (AP4_FAILED(result)) return result;
  892.     result = stream.ReadUI16(m_Description.Style.Font.Id);
  893.     if (AP4_FAILED(result)) return result;
  894.     result = stream.ReadUI08(m_Description.Style.Font.Face);
  895.     if (AP4_FAILED(result)) return result;
  896.     result = stream.ReadUI08(m_Description.Style.Font.Size);
  897.     if (AP4_FAILED(result)) return result;
  898.     result = stream.Read(&m_Description.Style.Font.Color, 4);
  899.     if (AP4_FAILED(result)) return result;
  900.     return AP4_SUCCESS;
  901. }
  902. /*----------------------------------------------------------------------
  903. |       AP4_Tx3gSampleEntry::WriteFields
  904. +---------------------------------------------------------------------*/
  905. AP4_Result
  906. AP4_Tx3gSampleEntry::WriteFields(AP4_ByteStream& stream)
  907. {
  908.     // sample entry
  909.     AP4_Result result = AP4_SampleEntry::WriteFields(stream);
  910.     if (AP4_FAILED(result)) return result;
  911.     
  912.     // TODO: data
  913.     return result;
  914. }
  915. /*----------------------------------------------------------------------
  916. |       AP4_Tx3gSampleEntry::InspectFields
  917. +---------------------------------------------------------------------*/
  918. AP4_Result
  919. AP4_Tx3gSampleEntry::InspectFields(AP4_AtomInspector& inspector)
  920. {
  921.     // sample entry
  922.     AP4_SampleEntry::InspectFields(inspector);
  923.     
  924.     // TODO: fields
  925.     
  926.     return AP4_SUCCESS;
  927. }
  928. /*----------------------------------------------------------------------
  929. |       AP4_Tx3gSampleEntry::GetFontNameById
  930. +---------------------------------------------------------------------*/
  931. AP4_Result 
  932. AP4_Tx3gSampleEntry::GetFontNameById(AP4_Ordinal Id, AP4_String& Name)
  933. {
  934. if(AP4_FtabAtom* ftab = dynamic_cast<AP4_FtabAtom*>(GetChild(AP4_ATOM_TYPE_FTAB)))
  935. {
  936. AP4_Array<AP4_FtabAtom::AP4_Tx3gFontRecord> FontRecords = ftab->GetFontRecords();
  937. for(int i = 0, j = FontRecords.ItemCount(); i < j; i++)
  938. {
  939. if(Id == FontRecords[i].Id)
  940. {
  941. Name = FontRecords[i].Name;
  942. return AP4_SUCCESS;
  943. }
  944. }
  945. }
  946. return AP4_FAILURE;
  947. }