3gpmeta.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "mp4common.h"
  2. size_t utf16len(const uint16_t *str)
  3. {
  4. size_t size=0;
  5. while (*str++) size++;
  6. return size;
  7. }
  8. void utf16swap(uint16_t *str)
  9. {
  10. while (*str)
  11. {
  12. *str = htons(*str);
  13. str++;
  14. }
  15. }
  16. bool MP4File::Get3GPMetadataString(const char *atom, uint16_t **value)
  17. {
  18. char atomstring[60];
  19. snprintf(atomstring, 60, "moov.udta.%s.metadata", atom);
  20. const uint8_t *str = (const uint8_t *)this->GetStringProperty(atomstring);
  21. if (str)
  22. {
  23. bool reverse=false;
  24. bool utf16=false;
  25. if ((str[0] == 0xFE && str[1] == 0xFF))
  26. {
  27. reverse=true;
  28. utf16=true;
  29. }
  30. if ((str[0] == 0xFF && str[1] == 0xFE))
  31. {
  32. utf16=true;
  33. }
  34. if (utf16)
  35. {
  36. uint16_t *utf16 = (uint16_t *)str;
  37. size_t len = utf16len(utf16);
  38. *value = (uint16_t *)malloc(len*sizeof(uint16_t));
  39. if (!*value)
  40. return false;
  41. memcpy(*value, utf16+1, len*sizeof(uint16_t));
  42. if (reverse)
  43. utf16swap(*value);
  44. }
  45. else
  46. {
  47. int len = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)str, -1, 0, 0);
  48. *value = (uint16_t *)malloc(len * sizeof(uint16_t));
  49. if (*value == NULL)
  50. {
  51. return false;
  52. }
  53. MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)str, -1, (LPWSTR)*value, len);
  54. }
  55. return true;
  56. }
  57. return false;
  58. }
  59. bool MP4File::Set3GPMetadataString(const char *atom, const uint16_t *value)
  60. {
  61. char atomstring[60];
  62. MP4Atom *pMetaAtom;
  63. MP4StringProperty *pMetadataProperty = NULL;
  64. MP4Integer16Property *unknown = NULL;
  65. snprintf(atomstring, 60, "moov.udta.%s", atom);
  66. pMetaAtom = m_pRootAtom->FindAtomMP4(atomstring);
  67. if (!pMetaAtom)
  68. {
  69. (void)AddDescendantAtoms("moov", atomstring+5);
  70. //if (!CreateMetadataAtom(atom))
  71. //return false;
  72. pMetaAtom = m_pRootAtom->FindAtomMP4(atomstring);
  73. if (pMetaAtom == NULL) return false;
  74. }
  75. snprintf(atomstring, 60, "%s.language", atom);
  76. ASSERT(pMetaAtom->FindProperty(atomstring,
  77. (MP4Property**)&unknown));
  78. ASSERT(unknown);
  79. unknown->SetValue(0x15C7);
  80. snprintf(atomstring, 60, "%s.metadata", atom);
  81. ASSERT(pMetaAtom->FindProperty(atomstring,
  82. (MP4Property**)&pMetadataProperty));
  83. ASSERT(pMetadataProperty);
  84. pMetadataProperty->SetUnicode(true);
  85. size_t lenWithBOM = utf16len(value) + 1;
  86. uint16_t *newVal = (uint16_t *)malloc((lenWithBOM+1) * sizeof(uint16_t));
  87. newVal[0]=0xFEFF;
  88. memcpy(newVal+1, value, lenWithBOM*sizeof(uint16_t));
  89. pMetadataProperty->SetValue((char *)newVal, 0);
  90. free(newVal);
  91. return true;
  92. }
  93. bool MP4File::Get3GPMetadataInteger(const char *atom, uint64_t *value)
  94. {
  95. char atomstring[60];
  96. snprintf(atomstring, 60, "moov.udta.%s.metadata", atom);
  97. MP4Property* pProperty;
  98. u_int32_t index;
  99. FindIntegerProperty(atomstring, &pProperty, &index);
  100. if (pProperty)
  101. {
  102. *value = ((MP4IntegerProperty*)pProperty)->GetValue(index);
  103. return true;
  104. }
  105. return false;
  106. }
  107. bool MP4File::Set3GPMetadataInteger(const char *atom, uint64_t value)
  108. {
  109. char atomstring[60] = {0};
  110. MP4Atom *pMetaAtom;
  111. MP4IntegerProperty *pMetadataProperty = NULL;
  112. snprintf(atomstring, 60, "moov.udta.%s", atom);
  113. pMetaAtom = m_pRootAtom->FindAtomMP4(atomstring);
  114. if (!pMetaAtom)
  115. {
  116. (void)AddDescendantAtoms("moov", atomstring+5);
  117. //if (!CreateMetadataAtom(atom))
  118. //return false;
  119. pMetaAtom = m_pRootAtom->FindAtomMP4(atomstring);
  120. if (pMetaAtom == NULL) return false;
  121. }
  122. snprintf(atomstring, 60, "%s.metadata", atom);
  123. ASSERT(pMetaAtom->FindProperty(atomstring,
  124. (MP4Property**)&pMetadataProperty));
  125. ASSERT(pMetadataProperty);
  126. pMetadataProperty->SetValue(value, 0);
  127. return true;
  128. }
  129. bool MP4File::Delete3GPMetadataAtom(const char* name)
  130. {
  131. MP4Atom *pMetaAtom = NULL;
  132. char s[256];
  133. snprintf(s, 256, "moov.udta.%s", name);
  134. pMetaAtom = m_pRootAtom->FindAtomMP4(s);
  135. /* if it exists, delete it */
  136. if (pMetaAtom)
  137. {
  138. MP4Atom *pParent = pMetaAtom->GetParentAtom();
  139. pParent->DeleteChildAtom(pMetaAtom);
  140. delete pMetaAtom;
  141. return true;
  142. }
  143. return false;
  144. }