1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
|
/*****************************************************************
|
| AP4 - Atoms
|
| Copyright 2002-2008 Axiomatic Systems, LLC
|
|
| This file is part of Bento4/AP4 (MP4 Atom Processing Library).
|
| Unless you have obtained Bento4 under a difference license,
| this version of Bento4 is Bento4|GPL.
| Bento4|GPL is free software; you can redistribute it and/or modify
| it under the terms of the GNU General Public License as published by
| the Free Software Foundation; either version 2, or (at your option)
| any later version.
|
| Bento4|GPL is distributed in the hope that it will be useful,
| but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
| GNU General Public License for more details.
|
| You should have received a copy of the GNU General Public License
| along with Bento4|GPL; see the file COPYING. If not, write to the
| Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
| 02111-1307, USA.
|
****************************************************************/
/*----------------------------------------------------------------------
| includes
+---------------------------------------------------------------------*/
#include "Ap4Types.h"
#include "Ap4Atom.h"
#include "Ap4Utils.h"
#include "Ap4ContainerAtom.h"
#include "Ap4AtomFactory.h"
#include "Ap4Debug.h"
#include "Ap4UuidAtom.h"
/*----------------------------------------------------------------------
| constants
+---------------------------------------------------------------------*/
static const unsigned int AP4_ATOM_MAX_CLONE_SIZE = 1048576; // 1 meg
static const unsigned int AP4_UNKNOWN_ATOM_MAX_LOCAL_PAYLOAD_SIZE = 4096;
/*----------------------------------------------------------------------
| dynamic cast support
+---------------------------------------------------------------------*/
AP4_DEFINE_DYNAMIC_CAST_ANCHOR(AP4_Atom)
AP4_DEFINE_DYNAMIC_CAST_ANCHOR(AP4_AtomParent)
AP4_DEFINE_DYNAMIC_CAST_ANCHOR(AP4_NullTerminatedStringAtom)
/*----------------------------------------------------------------------
| AP4_Atom::TypeFromString
+---------------------------------------------------------------------*/
AP4_Atom::Type
AP4_Atom::TypeFromString(const char* s)
{
// convert the name into an atom type
return ((AP4_UI32)s[0])<<24 |
((AP4_UI32)s[1])<<16 |
((AP4_UI32)s[2])<< 8 |
((AP4_UI32)s[3]);
}
/*----------------------------------------------------------------------
| AP4_Atom::AP4_Atom
+---------------------------------------------------------------------*/
AP4_Atom::AP4_Atom(Type type, AP4_UI32 size /* = AP4_ATOM_HEADER_SIZE */) :
m_Type(type),
m_Size32(size),
m_Size64(0),
m_IsFull(false),
m_Version(0),
m_Flags(0),
m_Parent(NULL)
{
}
/*----------------------------------------------------------------------
| AP4_Atom::AP4_Atom
+---------------------------------------------------------------------*/
AP4_Atom::AP4_Atom(Type type, AP4_UI64 size, bool force_64) :
m_Type(type),
m_Size32(0),
m_Size64(0),
m_IsFull(false),
m_Version(0),
m_Flags(0),
m_Parent(NULL)
{
SetSize(size, force_64);
}
/*----------------------------------------------------------------------
| AP4_Atom::AP4_Atom
+---------------------------------------------------------------------*/
AP4_Atom::AP4_Atom(Type type,
AP4_UI32 size,
AP4_UI08 version,
AP4_UI32 flags) :
m_Type(type),
m_Size32(size),
m_Size64(0),
m_IsFull(true),
m_Version(version),
m_Flags(flags),
m_Parent(NULL)
{
}
/*----------------------------------------------------------------------
| AP4_Atom::AP4_Atom
+---------------------------------------------------------------------*/
AP4_Atom::AP4_Atom(Type type,
AP4_UI64 size,
bool force_64,
AP4_UI08 version,
AP4_UI32 flags) :
m_Type(type),
m_Size32(0),
m_Size64(0),
m_IsFull(true),
m_Version(version),
m_Flags(flags),
m_Parent(NULL)
{
SetSize(size, force_64);
}
/*----------------------------------------------------------------------
| AP4_Atom::ReadFullHeader
+---------------------------------------------------------------------*/
AP4_Result
AP4_Atom::ReadFullHeader(AP4_ByteStream& stream,
AP4_UI08& version,
AP4_UI32& flags)
{
AP4_UI32 header;
AP4_CHECK(stream.ReadUI32(header));
version = (header>>24)&0x000000FF;
flags = (header )&0x00FFFFFF;
return AP4_SUCCESS;
}
/*----------------------------------------------------------------------
| AP4_Atom::SetSize
+---------------------------------------------------------------------*/
void
AP4_Atom::SetSize(AP4_UI64 size, bool force_64)
{
if (!force_64) {
// see if we need to implicitely force 64-bit encoding
if (m_Size32 == 1 && m_Size64 <= 0xFFFFFFFF) {
// we have a forced 64-bit encoding
force_64 = true;
}
}
if ((size >> 32) == 0 && !force_64) {
m_Size32 = (AP4_UI32)size;
m_Size64 = 0;
} else {
m_Size32 = 1;
m_Size64 = size;
}
}
/*----------------------------------------------------------------------
| AP4_Atom::GetHeaderSize
+---------------------------------------------------------------------*/
AP4_Size
AP4_Atom::GetHeaderSize() const
{
return (m_IsFull ? AP4_FULL_ATOM_HEADER_SIZE : AP4_ATOM_HEADER_SIZE)+(m_Size32==1?8:0);
}
/*----------------------------------------------------------------------
| AP4_Atom::WriteHeader
+---------------------------------------------------------------------*/
AP4_Result
AP4_Atom::WriteHeader(AP4_ByteStream& stream)
{
AP4_Result result;
// write the size
result = stream.WriteUI32(m_Size32);
if (AP4_FAILED(result)) return result;
// write the type
result = stream.WriteUI32(m_Type);
if (AP4_FAILED(result)) return result;
// handle 64-bit sizes
if (m_Size32 == 1) {
result = stream.WriteUI64(m_Size64);
if (AP4_FAILED(result)) return result;
}
// for full atoms, write version and flags
if (m_IsFull) {
result = stream.WriteUI08(m_Version);
if (AP4_FAILED(result)) return result;
result = stream.WriteUI24(m_Flags);
if (AP4_FAILED(result)) return result;
}
return AP4_SUCCESS;
}
/*----------------------------------------------------------------------
| AP4_Atom::Write
+---------------------------------------------------------------------*/
AP4_Result
AP4_Atom::Write(AP4_ByteStream& stream)
{
AP4_Result result;
#if defined(AP4_DEBUG)
AP4_Position before;
stream.Tell(before);
#endif
// write the header
result = WriteHeader(stream);
if (AP4_FAILED(result)) return result;
// write the fields
result = WriteFields(stream);
if (AP4_FAILED(result)) return result;
#if defined(AP4_DEBUG)
AP4_Position after;
stream.Tell(after);
AP4_UI64 atom_size = GetSize();
if (after-before != atom_size) {
AP4_Debug("ERROR: atom size mismatch (declared size=%d, actual size=%d)\n",
(AP4_UI32)atom_size, (AP4_UI32)(after-before));
AP4_Atom* atom = this;
while (atom) {
char name[7];
name[0] = '[';
AP4_FormatFourCharsPrintable(&name[1], atom->GetType());
name[5] = ']';
name[6] = '\0';
AP4_Debug(" while writing %s\n", name);
atom = AP4_DYNAMIC_CAST(AP4_Atom, atom->GetParent());
}
AP4_ASSERT(after-before == atom_size);
}
#endif
return AP4_SUCCESS;
}
/*----------------------------------------------------------------------
| AP4_Atom::Inspect
+---------------------------------------------------------------------*/
AP4_Result
AP4_Atom::Inspect(AP4_AtomInspector& inspector)
{
InspectHeader(inspector);
InspectFields(inspector);
inspector.EndAtom();
return AP4_SUCCESS;
}
/*----------------------------------------------------------------------
| AP4_Atom::InspectHeader
+---------------------------------------------------------------------*/
AP4_Result
AP4_Atom::InspectHeader(AP4_AtomInspector& inspector)
{
char name[5];
AP4_FormatFourCharsPrintable(name, m_Type);
name[4] = '\0';
inspector.StartAtom(name,
m_Version,
m_Flags,
GetHeaderSize(),
GetSize());
return AP4_SUCCESS;
}
/*----------------------------------------------------------------------
| AP4_Atom::Detach
+---------------------------------------------------------------------*/
AP4_Result
AP4_Atom::Detach()
{
if (m_Parent) {
return m_Parent->RemoveChild(this);
} else {
return AP4_SUCCESS;
}
}
/*----------------------------------------------------------------------
| AP4_Atom::Clone
+---------------------------------------------------------------------*/
AP4_Atom*
AP4_Atom::Clone()
{
AP4_Atom* clone = NULL;
// check the size (refuse to clone atoms that are too large)
AP4_LargeSize size = GetSize();
if (size > AP4_ATOM_MAX_CLONE_SIZE) return NULL;
// create a memory byte stream to which we can serialize
AP4_MemoryByteStream* mbs = new AP4_MemoryByteStream((AP4_Size)GetSize());
// serialize to memory
if (AP4_FAILED(Write(*mbs))) {
mbs->Release();
return NULL;
}
// create the clone from the serialized form
mbs->Seek(0);
AP4_DefaultAtomFactory atom_factory;
atom_factory.CreateAtomFromStream(*mbs, clone);
// release the memory stream
mbs->Release();
return clone;
}
/*----------------------------------------------------------------------
| AP4_UnknownAtom::AP4_UnknownAtom
+---------------------------------------------------------------------*/
AP4_UnknownAtom::AP4_UnknownAtom(Type type,
AP4_UI64 size,
AP4_ByteStream& stream) :
AP4_Atom(type, size),
m_SourceStream(&stream)
{
if (size <= AP4_UNKNOWN_ATOM_MAX_LOCAL_PAYLOAD_SIZE &&
type != AP4_ATOM_TYPE_MDAT) {
m_SourcePosition = 0;
m_SourceStream = NULL;
AP4_UI32 payload_size = (AP4_UI32)size-GetHeaderSize();
m_Payload.SetDataSize(payload_size);
stream.Read(m_Payload.UseData(), payload_size);
return;
}
// store source stream position
stream.Tell(m_SourcePosition);
// clamp to the file size
AP4_UI64 file_size;
if (AP4_SUCCEEDED(stream.GetSize(file_size))) {
if (m_SourcePosition-GetHeaderSize()+size > file_size) {
if (m_Size32 == 1) {
// size is encoded as a large size
m_Size64 = file_size-m_SourcePosition;
} else {
AP4_ASSERT(size <= 0xFFFFFFFF);
m_Size32 = (AP4_UI32)(file_size-m_SourcePosition);
}
}
}
// keep a reference to the source stream
m_SourceStream->AddReference();
}
/*----------------------------------------------------------------------
| AP4_UnknownAtom::AP4_UnknownAtom
+---------------------------------------------------------------------*/
AP4_UnknownAtom::AP4_UnknownAtom(Type type,
const AP4_UI08* payload,
AP4_Size payload_size) :
AP4_Atom(type, AP4_ATOM_HEADER_SIZE+payload_size, false),
m_SourceStream(NULL),
m_SourcePosition(0)
{
m_Payload.SetData(payload, payload_size);
}
/*----------------------------------------------------------------------
| AP4_UnknownAtom::AP4_UnknownAtom
+---------------------------------------------------------------------*/
AP4_UnknownAtom::AP4_UnknownAtom(const AP4_UnknownAtom& other) :
AP4_Atom(other.m_Type, (AP4_UI32)0),
m_SourceStream(other.m_SourceStream),
m_SourcePosition(other.m_SourcePosition),
m_Payload(other.m_Payload)
{
m_Size32 = other.m_Size32;
m_Size64 = other.m_Size64;
if (m_SourceStream) {
m_SourceStream->AddReference();
}
}
/*----------------------------------------------------------------------
| AP4_UnknownAtom::~AP4_UnknownAtom
+---------------------------------------------------------------------*/
AP4_UnknownAtom::~AP4_UnknownAtom()
{
// release the source stream reference
if (m_SourceStream) {
m_SourceStream->Release();
}
}
/*----------------------------------------------------------------------
| AP4_UnknownAtom::WriteFields
+---------------------------------------------------------------------*/
AP4_Result
AP4_UnknownAtom::WriteFields(AP4_ByteStream& stream)
{
AP4_Result result;
// if we don't have a source, write from the buffered payload
if (m_SourceStream == NULL) {
return stream.Write(m_Payload.GetData(), m_Payload.GetDataSize());
}
// remember the source position
AP4_Position position;
m_SourceStream->Tell(position);
// seek into the source at the stored offset
result = m_SourceStream->Seek(m_SourcePosition);
if (AP4_FAILED(result)) return result;
// copy the source stream to the output
AP4_UI64 payload_size = GetSize()-GetHeaderSize();
result = m_SourceStream->CopyTo(stream, payload_size);
if (AP4_FAILED(result)) return result;
// restore the original stream position
m_SourceStream->Seek(position);
return AP4_SUCCESS;
}
/*----------------------------------------------------------------------
| AP4_UnknownAtom::Clone
+---------------------------------------------------------------------*/
AP4_Atom*
AP4_UnknownAtom::Clone()
{
return new AP4_UnknownAtom(*this);
}
/*----------------------------------------------------------------------
| AP4_NullTerminatedStringAtom::AP4_NullTerminatedStringAtom
+---------------------------------------------------------------------*/
AP4_NullTerminatedStringAtom::AP4_NullTerminatedStringAtom(AP4_Atom::Type type, const char* value) :
AP4_Atom(type, AP4_ATOM_HEADER_SIZE),
m_Value(value)
{
m_Size32 += m_Value.GetLength()+1;
}
/*----------------------------------------------------------------------
| AP4_NullTerminatedStringAtom::AP4_NullTerminatedStringAtom
+---------------------------------------------------------------------*/
AP4_NullTerminatedStringAtom::AP4_NullTerminatedStringAtom(AP4_Atom::Type type,
AP4_UI64 size,
AP4_ByteStream& stream) :
AP4_Atom(type, size)
{
AP4_Size str_size = (AP4_Size)size - AP4_ATOM_HEADER_SIZE;
if (str_size) {
char* str = new char[str_size];
stream.Read(str, str_size);
str[str_size - 1] = '\0'; // force null-termination
m_Value = str;
delete[] str;
}
}
/*----------------------------------------------------------------------
| AP4_NullTerminatedStringAtom::WriteFields
+---------------------------------------------------------------------*/
AP4_Result
AP4_NullTerminatedStringAtom::WriteFields(AP4_ByteStream& stream)
{
if (m_Size32 > AP4_ATOM_HEADER_SIZE) {
AP4_Result result = stream.Write(m_Value.GetChars(), m_Value.GetLength()+1);
if (AP4_FAILED(result)) return result;
// pad with zeros if necessary
AP4_Size padding = m_Size32-(AP4_ATOM_HEADER_SIZE+m_Value.GetLength()+1);
while (padding--) stream.WriteUI08(0);
}
return AP4_SUCCESS;
}
/*----------------------------------------------------------------------
| AP4_NullTerminatedStringAtom::InspectFields
+---------------------------------------------------------------------*/
AP4_Result
AP4_NullTerminatedStringAtom::InspectFields(AP4_AtomInspector& inspector)
{
inspector.AddField("string value", m_Value.GetChars());
return AP4_SUCCESS;
}
/*----------------------------------------------------------------------
| AP4_AtomParent::~AP4_AtomParent
+---------------------------------------------------------------------*/
AP4_AtomParent::~AP4_AtomParent()
{
m_Children.DeleteReferences();
}
/*----------------------------------------------------------------------
| AP4_AtomParent::AddChild
+---------------------------------------------------------------------*/
AP4_Result
AP4_AtomParent::AddChild(AP4_Atom* child, int position)
{
// check that the child does not already have a parent
if (child->GetParent() != NULL) return AP4_ERROR_INVALID_PARAMETERS;
// attach the child
AP4_Result result;
if (position == -1) {
// insert at the tail
result = m_Children.Add(child);
} else if (position == 0) {
// insert at the head
result = m_Children.Insert(NULL, child);
} else {
// insert after <n-1>
AP4_List<AP4_Atom>::Item* insertion_point = m_Children.FirstItem();
unsigned int count = position;
while (insertion_point && --count) {
insertion_point = insertion_point->GetNext();
}
if (insertion_point) {
result = m_Children.Insert(insertion_point, child);
} else {
result = AP4_ERROR_OUT_OF_RANGE;
}
}
if (AP4_FAILED(result)) return result;
// notify the child of its parent
child->SetParent(this);
// get a chance to update
OnChildAdded(child);
return AP4_SUCCESS;
}
/*----------------------------------------------------------------------
| AP4_AtomParent::RemoveChild
+---------------------------------------------------------------------*/
AP4_Result
AP4_AtomParent::RemoveChild(AP4_Atom* child)
{
// check that this is our child
if (child->GetParent() != this) return AP4_ERROR_INVALID_PARAMETERS;
// remove the child
AP4_Result result = m_Children.Remove(child);
if (AP4_FAILED(result)) return result;
// notify that child that it is orphaned
child->SetParent(NULL);
// get a chance to update
OnChildRemoved(child);
return AP4_SUCCESS;
}
/*----------------------------------------------------------------------
| AP4_AtomParent::DeleteChild
+---------------------------------------------------------------------*/
AP4_Result
AP4_AtomParent::DeleteChild(AP4_Atom::Type type, AP4_Ordinal index /* = 0 */)
{
// find the child
AP4_Atom* child = GetChild(type, index);
if (child == NULL) return AP4_FAILURE;
// remove the child
AP4_Result result = RemoveChild(child);
if (AP4_FAILED(result)) return result;
// delete the child
delete child;
return AP4_SUCCESS;
}
/*----------------------------------------------------------------------
| AP4_AtomParent::GetChild
+---------------------------------------------------------------------*/
AP4_Atom*
AP4_AtomParent::GetChild(AP4_Atom::Type type, AP4_Ordinal index /* = 0 */) const
{
AP4_Atom* atom;
AP4_Result result = m_Children.Find(AP4_AtomFinder(type, index), atom);
if (AP4_SUCCEEDED(result)) {
return atom;
} else {
return NULL;
}
}
/*----------------------------------------------------------------------
| AP4_AtomParent::GetChild
+---------------------------------------------------------------------*/
AP4_Atom*
AP4_AtomParent::GetChild(const AP4_UI08* uuid, AP4_Ordinal index /* = 0 */) const
{
for (AP4_List<AP4_Atom>::Item* item = m_Children.FirstItem();
item;
item = item->GetNext()) {
AP4_Atom* atom = item->GetData();
if (atom->GetType() == AP4_ATOM_TYPE_UUID) {
AP4_UuidAtom* uuid_atom = AP4_DYNAMIC_CAST(AP4_UuidAtom, atom);
if (AP4_CompareMemory(uuid_atom->GetUuid(), uuid, 16) == 0) {
if (index == 0) return atom;
--index;
}
}
}
return NULL;
}
/*----------------------------------------------------------------------
| AP4_AtomParent::FindChild
+---------------------------------------------------------------------*/
AP4_Atom*
AP4_AtomParent::FindChild(const char* path,
bool auto_create,
bool auto_create_full)
{
// start from here
AP4_AtomParent* parent = this;
// walk the path
while (path[0] && path[1] && path[2] && path[3]) {
// we have 4 valid chars
const char* end = &path[4];
// look for the end or a separator
while (*end != '\0' && *end != '/' && *end != '[') {
++end;
}
// decide if this is a 4-character code or a UUID
AP4_UI08 uuid[16];
AP4_Atom::Type type = 0;
bool is_uuid = false;
if (end == path+4) {
// 4-character code
type = AP4_ATOM_TYPE(path[0], path[1], path[2], path[3]);
} else if (end == path+32) {
// UUID
is_uuid = true;
AP4_ParseHex(path, uuid, sizeof(uuid));
} else {
// malformed path
return NULL;
}
// parse the array index, if any
int index = 0;
if (*end == '[') {
const char* x = end+1;
while (*x >= '0' && *x <= '9') {
index = 10*index+(*x++ - '0');
}
if (*x != ']') {
// malformed path
return NULL;
}
end = x+1;
}
// check what's at the end now
if (*end == '/') {
++end;
} else if (*end != '\0') {
// malformed path
return NULL;
}
// look for this atom in the current list
AP4_Atom* atom = NULL;
if (is_uuid) {
atom = parent->GetChild(uuid, index);
} else {
atom = parent->GetChild(type, index);
}
if (atom == NULL) {
// not found
if (auto_create && (index == 0)) {
if (auto_create_full) {
atom = new AP4_ContainerAtom(type, (AP4_UI32)0, (AP4_UI32)0);
} else {
atom = new AP4_ContainerAtom(type);
}
parent->AddChild(atom);
} else {
return NULL;
}
}
if (*end) {
path = end;
// if this atom is an atom parent, recurse
parent = AP4_DYNAMIC_CAST(AP4_ContainerAtom, atom);
if (parent == NULL) return NULL;
} else {
return atom;
}
}
// not found
return NULL;
}
/*----------------------------------------------------------------------
| AP4_AtomParent::CopyChildren
+---------------------------------------------------------------------*/
AP4_Result
AP4_AtomParent::CopyChildren(AP4_AtomParent& destination) const
{
for (AP4_List<AP4_Atom>::Item* child = m_Children.FirstItem(); child; child=child->GetNext()) {
AP4_Atom* child_clone = child->GetData()->Clone();
destination.AddChild(child_clone);
}
return AP4_SUCCESS;
}
/*----------------------------------------------------------------------
| AP4_AtomListWriter::Action
+---------------------------------------------------------------------*/
const unsigned int AP4_ATOM_LIST_WRITER_MAX_PADDING=1024;
AP4_Result
AP4_AtomListWriter::Action(AP4_Atom* atom) const
{
AP4_Position before;
m_Stream.Tell(before);
atom->Write(m_Stream);
AP4_Position after;
m_Stream.Tell(after);
AP4_UI64 bytes_written = after-before;
AP4_ASSERT(bytes_written <= atom->GetSize());
if (bytes_written < atom->GetSize()) {
AP4_Debug("WARNING: atom serialized to fewer bytes than declared size\n");
AP4_UI64 padding = atom->GetSize()-bytes_written;
if (padding > AP4_ATOM_LIST_WRITER_MAX_PADDING) {
AP4_Debug("WARNING: padding would be too large\n");
return AP4_FAILURE;
} else {
for (unsigned int i=0; i<padding; i++) {
m_Stream.WriteUI08(0);
}
}
}
return AP4_SUCCESS;
}
/*----------------------------------------------------------------------
| AP4_MakePrefixString
+---------------------------------------------------------------------*/
static void
AP4_MakePrefixString(unsigned int indent, char* prefix, AP4_Size size)
{
if (size == 0) return;
if (indent >= size-1) indent = size-1;
for (unsigned int i=0; i<indent; i++) {
prefix[i] = ' ';
}
prefix[indent] = '\0';
}
/*----------------------------------------------------------------------
| AP4_PrintInspector::AP4_PrintInspector
+---------------------------------------------------------------------*/
AP4_PrintInspector::AP4_PrintInspector(AP4_ByteStream& stream, AP4_Cardinal indent) :
m_Stream(&stream)
{
m_Stream->AddReference();
PushContext(Context::TOP_LEVEL);
}
/*----------------------------------------------------------------------
| AP4_PrintInspector::~AP4_PrintInspector
+---------------------------------------------------------------------*/
AP4_PrintInspector::~AP4_PrintInspector()
{
m_Stream->Release();
}
/*----------------------------------------------------------------------
| AP4_PrintInspector::PushContext
+---------------------------------------------------------------------*/
void
AP4_PrintInspector::PushContext(Context::Type type)
{
m_Contexts.Append(Context(type));
}
/*----------------------------------------------------------------------
| AP4_PrintInspector::PopContext
+---------------------------------------------------------------------*/
void
AP4_PrintInspector::PopContext()
{
m_Contexts.RemoveLast();
}
/*----------------------------------------------------------------------
| AP4_PrintInspector::PrintPrefix
+---------------------------------------------------------------------*/
void
AP4_PrintInspector::PrintPrefix()
{
if (LastContext().m_Type == Context::COMPACT_OBJECT) {
if (LastContext().m_ArrayIndex++) {
m_Stream->WriteString(", ");
}
return;
}
if (m_Contexts.ItemCount() >= 1) {
char prefix[256];
AP4_MakePrefixString((m_Contexts.ItemCount() - 1) * 2, prefix, sizeof(prefix));
m_Stream->WriteString(prefix);
if (LastContext().m_Type == Context::ARRAY) {
char index[32];
AP4_FormatString(index, sizeof(index), "(%8d) ", (int)LastContext().m_ArrayIndex);
m_Stream->WriteString(index);
++LastContext().m_ArrayIndex;
}
}
}
/*----------------------------------------------------------------------
| AP4_PrintInspector::PrintSuffix
+---------------------------------------------------------------------*/
void
AP4_PrintInspector::PrintSuffix()
{
if (LastContext().m_Type != Context::COMPACT_OBJECT) {
m_Stream->WriteString("\n");
}
}
/*----------------------------------------------------------------------
| AP4_PrintInspector::StartAtom
+---------------------------------------------------------------------*/
void
AP4_PrintInspector::StartAtom(const char* name,
AP4_UI08 version,
AP4_UI32 flags,
AP4_Size header_size,
AP4_UI64 size)
{
PrintPrefix();
PushContext(Context::ATOM);
// write atom name
char info[128];
char extra[32] = "";
if (header_size == 28 || header_size == 12 || header_size == 20) {
if (version && flags) {
AP4_FormatString(extra, sizeof(extra),
", version=%d, flags=%x",
version,
flags);
} else if (version) {
AP4_FormatString(extra, sizeof(extra),
", version=%d",
version);
} else if (flags) {
AP4_FormatString(extra, sizeof(extra),
", flags=%x",
flags);
}
}
AP4_FormatString(info, sizeof(info),
"size=%d+%lld%s",
header_size,
size-header_size,
extra);
m_Stream->WriteString("[");
m_Stream->WriteString(name);
m_Stream->Write("] ", 2);
m_Stream->WriteString(info);
PrintSuffix();
}
/*----------------------------------------------------------------------
| AP4_PrintInspector::EndAtom
+---------------------------------------------------------------------*/
void
AP4_PrintInspector::EndAtom()
{
PopContext();
}
/*----------------------------------------------------------------------
| AP4_PrintInspector::StartDescriptor
+---------------------------------------------------------------------*/
void
AP4_PrintInspector::StartDescriptor(const char* name,
AP4_Size header_size,
AP4_UI64 size)
{
PrintPrefix();
PushContext(Context::ATOM);
// write atom name
char info[128];
AP4_FormatString(info, sizeof(info),
"size=%d+%lld",
header_size,
size-header_size);
m_Stream->Write("[", 1);
m_Stream->WriteString(name);
m_Stream->Write("] ", 2);
m_Stream->WriteString(info);
PrintSuffix();
}
/*----------------------------------------------------------------------
| AP4_PrintInspector::EndDescriptor
+---------------------------------------------------------------------*/
void
AP4_PrintInspector::EndDescriptor()
{
EndAtom();
}
/*----------------------------------------------------------------------
| AP4_PrintInspector::StartArray
+---------------------------------------------------------------------*/
void
AP4_PrintInspector::StartArray(const char* name, unsigned int /* element_count */)
{
PrintPrefix();
PushContext(Context::ARRAY);
if (name) {
m_Stream->WriteString(name);
m_Stream->WriteString(":");
}
PrintSuffix();
}
/*----------------------------------------------------------------------
| AP4_PrintInspector::EndArray
+---------------------------------------------------------------------*/
void
AP4_PrintInspector::EndArray()
{
PopContext();
}
/*----------------------------------------------------------------------
| AP4_PrintInspector::StartObject
+---------------------------------------------------------------------*/
void
AP4_PrintInspector::StartObject(const char* name, unsigned int /* field_count */, bool compact)
{
PrintPrefix();
PushContext(compact ? Context::COMPACT_OBJECT : Context::OBJECT);
if (name) {
m_Stream->WriteString(name);
m_Stream->WriteString(": ");
}
PrintSuffix();
}
/*----------------------------------------------------------------------
| AP4_PrintInspector::EndObject
+---------------------------------------------------------------------*/
void
AP4_PrintInspector::EndObject()
{
if (LastContext().m_Type == Context::COMPACT_OBJECT) {
m_Stream->WriteString("\n");
}
PopContext();
}
/*----------------------------------------------------------------------
| AP4_PrintInspector::AddField
+---------------------------------------------------------------------*/
void
AP4_PrintInspector::AddField(const char* name, const char* value, FormatHint)
{
PrintPrefix();
if (name) {
m_Stream->WriteString(name);
m_Stream->WriteString(" = ");
}
m_Stream->WriteString(value);
PrintSuffix();
}
/*----------------------------------------------------------------------
| AP4_PrintInspector::AddField
+---------------------------------------------------------------------*/
void
AP4_PrintInspector::AddField(const char* name, AP4_UI64 value, FormatHint hint)
{
PrintPrefix();
if (name) {
m_Stream->WriteString(name);
m_Stream->WriteString(" = ");
}
char str[32];
AP4_FormatString(str, sizeof(str),
hint == HINT_HEX ? "%llx":"%lld",
value);
m_Stream->WriteString(str);
PrintSuffix();
}
/*----------------------------------------------------------------------
| AP4_PrintInspector::AddFieldF
+---------------------------------------------------------------------*/
void
AP4_PrintInspector::AddFieldF(const char* name, float value, FormatHint /*hint*/)
{
PrintPrefix();
if (name) {
m_Stream->WriteString(name);
m_Stream->WriteString(" = ");
}
char str[32];
AP4_FormatString(str, sizeof(str),
"%f",
value);
m_Stream->WriteString(str);
PrintSuffix();
}
/*----------------------------------------------------------------------
| AP4_PrintInspector::AddField
+---------------------------------------------------------------------*/
void
AP4_PrintInspector::AddField(const char* name,
const unsigned char* bytes,
AP4_Size byte_count,
FormatHint /* hint */)
{
PrintPrefix();
if (name) {
m_Stream->WriteString(name);
m_Stream->WriteString(" = ");
}
m_Stream->WriteString("[");
unsigned int offset = 1;
char byte[4];
for (unsigned int i=0; i<byte_count; i++) {
AP4_FormatString(byte, 4, " %02x", bytes[i]);
m_Stream->Write(&byte[offset], 3-offset);
offset = 0;
}
m_Stream->WriteString("]");
PrintSuffix();
}
/*----------------------------------------------------------------------
| AP4_JsonInspector::AP4_JsonInspector
+---------------------------------------------------------------------*/
AP4_JsonInspector::AP4_JsonInspector(AP4_ByteStream& stream) :
m_Stream(&stream)
{
m_Stream->AddReference();
m_Stream->WriteString("[\n");
PushContext(Context::TOP_LEVEL);
}
/*----------------------------------------------------------------------
| AP4_JsonInspector::~AP4_JsonInspector
+---------------------------------------------------------------------*/
AP4_JsonInspector::~AP4_JsonInspector()
{
m_Stream->WriteString("\n]\n");
m_Stream->Release();
}
/*----------------------------------------------------------------------
| AP4_JsonInspector::PushContext
+---------------------------------------------------------------------*/
void
AP4_JsonInspector::PushContext(Context::Type type)
{
m_Contexts.Append(Context(type));
AP4_MakePrefixString(m_Contexts.ItemCount() * 2, m_Prefix, sizeof(m_Prefix));
}
/*----------------------------------------------------------------------
| AP4_JsonInspector::PopContext
+---------------------------------------------------------------------*/
void
AP4_JsonInspector::PopContext()
{
m_Contexts.RemoveLast();
AP4_MakePrefixString(m_Contexts.ItemCount() * 2, m_Prefix, sizeof(m_Prefix));
}
/*----------------------------------------------------------------------
| AP4_JsonInspector::OnFieldAdded
+---------------------------------------------------------------------*/
void
AP4_JsonInspector::OnFieldAdded()
{
if (LastContext().m_FieldCount) {
m_Stream->WriteString(",\n");
}
++LastContext().m_FieldCount;
}
/*----------------------------------------------------------------------
| AP4_JsonInspector::PrintFieldName
+---------------------------------------------------------------------*/
void
AP4_JsonInspector::PrintFieldName(const char* name)
{
if (!name) return;
m_Stream->WriteString("\"");
m_Stream->WriteString(EscapeString(name).GetChars());
m_Stream->WriteString("\": ");
}
/*----------------------------------------------------------------------
| Read one code point from a UTF-8 string
+---------------------------------------------------------------------*/
static AP4_Result
ReadUTF8(const AP4_UI08* utf, AP4_Size* length, AP4_UI32* code_point) {
if (*length < 1) {
return AP4_ERROR_NOT_ENOUGH_DATA;
}
AP4_UI32 c = utf[0];
if ((c & 0x80) == 0) {
*length = 1;
*code_point = c;
return AP4_SUCCESS;
}
if (*length < 2) {
return AP4_ERROR_NOT_ENOUGH_DATA;
}
*code_point = 0;
if ((utf[1] & 0xc0) != 0x80) {
return AP4_ERROR_INVALID_FORMAT;
}
if ((c & 0xe0) == 0xe0) {
if (*length < 3) {
return AP4_ERROR_NOT_ENOUGH_DATA;
}
if ((utf[2] & 0xc0) != 0x80) {
return AP4_ERROR_INVALID_FORMAT;
}
if ((c & 0xf0) == 0xf0) {
if (*length < 4) {
return AP4_ERROR_NOT_ENOUGH_DATA;
}
if ((c & 0xf8) != 0xf0 || (utf[3] & 0xc0) != 0x80) {
return AP4_ERROR_INVALID_FORMAT;
}
*length = 4;
c = (utf[0] & 0x07) << 18;
c |= (utf[1] & 0x3f) << 12;
c |= (utf[2] & 0x3f) << 6;
c |= (utf[3] & 0x3f);
} else {
*length = 3;
c = (utf[0] & 0x0f) << 12;
c |= (utf[1] & 0x3f) << 6;
c |= (utf[2] & 0x3f);
}
} else {
/* 2-byte code */
*length = 2;
c = (utf[0] & 0x1f) << 6;
c |= (utf[1] & 0x3f);
}
*code_point = c;
return AP4_SUCCESS;
}
/*----------------------------------------------------------------------
| AP4_JsonInspector::EscapeString
|
| Not very efficient but simple function to escape characters in a
| JSON string
+---------------------------------------------------------------------*/
AP4_String
AP4_JsonInspector::EscapeString(const char* string)
{
AP4_String result(string);
// Shortcut
if (result.GetLength() == 0) {
return result;
}
// Compute the output size
AP4_Size string_length = (AP4_Size)strlen(string);
const AP4_UI08* input = (const AP4_UI08*)string;
AP4_Size input_length = string_length;
AP4_Size output_size = 0;
while (input_length) {
AP4_Size chars_available = input_length;
AP4_UI32 code_point = 0;
AP4_Result r = ReadUTF8(input, &chars_available, &code_point);
if (AP4_FAILED(r)) {
// stop, but don't fail
break;
}
if (code_point == '"' || code_point == '\\') {
output_size += 2;
} else if (code_point <= 0x1F) {
output_size += 6;
} else {
output_size += chars_available;
}
input_length -= chars_available;
input += chars_available;
}
// Shortcut
if (output_size == result.GetLength()) {
return result;
}
// Compute the escaped string in a temporary buffer
char* buffer = new char[output_size];
char* escaped = buffer;
input = (const AP4_UI08*)string;
input_length = string_length;
while (input_length) {
AP4_Size chars_available = input_length;
AP4_UI32 code_point = 0;
AP4_Result r = ReadUTF8(input, &chars_available, &code_point);
if (AP4_FAILED(r)) {
// stop, but don't fail
break;
}
if (code_point == '"' || code_point == '\\') {
*escaped++ = '\\';
*escaped++ = (char)code_point;
} else if (code_point <= 0x1F) {
*escaped++ = '\\';
*escaped++ = 'u';
*escaped++ = '0';
*escaped++ = '0';
*escaped++ = AP4_NibbleHex(code_point >> 4);
*escaped++ = AP4_NibbleHex(code_point & 0x0F);
} else {
for (AP4_Size i = 0; i < chars_available; i++) {
*escaped++ = (char)input[i];
}
}
input_length -= chars_available;
input += chars_available;
}
// Copy the buffer to a final string
result.Assign(buffer, output_size);
delete[] buffer;
return result;
}
/*----------------------------------------------------------------------
| AP4_JsonInspector::StartAtom
+---------------------------------------------------------------------*/
void
AP4_JsonInspector::StartAtom(const char* name,
AP4_UI08 version,
AP4_UI32 flags,
AP4_Size header_size,
AP4_UI64 size)
{
OnFieldAdded();
++LastContext().m_ChildrenCount;
// Starting the first atom within an atom means staring a childen array
if (LastContext().m_Type == Context::ATOM && LastContext().m_ChildrenCount == 1) {
m_Stream->WriteString(m_Prefix);
m_Stream->WriteString("\"children\":[ \n");
}
m_Stream->WriteString(m_Prefix);
m_Stream->WriteString("{\n");
PushContext(Context::ATOM);
OnFieldAdded();
m_Stream->WriteString(m_Prefix);
PrintFieldName("name");
m_Stream->WriteString("\"");
m_Stream->WriteString(EscapeString(name).GetChars());
m_Stream->WriteString("\"");
OnFieldAdded();
m_Stream->WriteString(m_Prefix);
PrintFieldName("header_size");
char val[32];
AP4_FormatString(val, sizeof(val), "%d", header_size);
m_Stream->WriteString(val);
OnFieldAdded();
m_Stream->WriteString(m_Prefix);
PrintFieldName("size");
AP4_FormatString(val, sizeof(val), "%lld", size);
m_Stream->WriteString(val);
if (version) {
OnFieldAdded();
m_Stream->WriteString(m_Prefix);
PrintFieldName("version");
AP4_FormatString(val, sizeof(val), "%d", version);
m_Stream->WriteString(val);
}
if (flags) {
OnFieldAdded();
m_Stream->WriteString(m_Prefix);
PrintFieldName("flags");
AP4_FormatString(val, sizeof(val), "%d", flags);
m_Stream->WriteString(val);
}
}
/*----------------------------------------------------------------------
| AP4_JsonInspector::EndAtom
+---------------------------------------------------------------------*/
void
AP4_JsonInspector::EndAtom()
{
// Ending an atom with children means we need to close the children array
if (LastContext().m_ChildrenCount) {
m_Stream->WriteString("]");
}
PopContext();
m_Stream->WriteString("\n");
m_Stream->WriteString(m_Prefix);
m_Stream->WriteString("}");
}
/*----------------------------------------------------------------------
| AP4_JsonInspector::StartDescriptor
+---------------------------------------------------------------------*/
void
AP4_JsonInspector::StartDescriptor(const char* name,
AP4_Size header_size,
AP4_UI64 size)
{
StartAtom(name, 0, 0, header_size, size);
}
/*----------------------------------------------------------------------
| AP4_JsonInspector::EndDescriptor
+---------------------------------------------------------------------*/
void
AP4_JsonInspector::EndDescriptor()
{
EndAtom();
}
/*----------------------------------------------------------------------
| AP4_JsonInspector::StartArray
+---------------------------------------------------------------------*/
void
AP4_JsonInspector::StartArray(const char* name, unsigned int /* element_count */)
{
OnFieldAdded();
m_Stream->WriteString(m_Prefix);
if (name) {
PrintFieldName(name);
}
m_Stream->WriteString("[\n");
PushContext(Context::ARRAY);
}
/*----------------------------------------------------------------------
| AP4_JsonInspector::EndArray
+---------------------------------------------------------------------*/
void
AP4_JsonInspector::EndArray()
{
PopContext();
m_Stream->WriteString("\n");
m_Stream->WriteString(m_Prefix);
m_Stream->WriteString("]");
}
/*----------------------------------------------------------------------
| AP4_JsonInspector::StartObject
+---------------------------------------------------------------------*/
void
AP4_JsonInspector::StartObject(const char* name, unsigned int /* field_count */, bool /* compact */)
{
OnFieldAdded();
m_Stream->WriteString(m_Prefix);
if (name) {
PrintFieldName(name);
}
m_Stream->WriteString("{\n");
PushContext(Context::ARRAY);
}
/*----------------------------------------------------------------------
| AP4_JsonInspector::EndObject
+---------------------------------------------------------------------*/
void
AP4_JsonInspector::EndObject()
{
PopContext();
m_Stream->WriteString("\n");
m_Stream->WriteString(m_Prefix);
m_Stream->WriteString("}");
}
/*----------------------------------------------------------------------
| AP4_JsonInspector::AddField
+---------------------------------------------------------------------*/
void
AP4_JsonInspector::AddField(const char* name, const char* value, FormatHint)
{
OnFieldAdded();
m_Stream->WriteString(m_Prefix);
PrintFieldName(name);
m_Stream->WriteString("\"");
m_Stream->WriteString(EscapeString(value).GetChars());
m_Stream->WriteString("\"");
}
/*----------------------------------------------------------------------
| AP4_JsonInspector::AddField
+---------------------------------------------------------------------*/
void
AP4_JsonInspector::AddField(const char* name, AP4_UI64 value, FormatHint /* hint */)
{
OnFieldAdded();
m_Stream->WriteString(m_Prefix);
PrintFieldName(name);
char str[32];
AP4_FormatString(str, sizeof(str),
"%lld",
value);
m_Stream->WriteString(str);
}
/*----------------------------------------------------------------------
| AP4_JsonInspector::AddFieldF
+---------------------------------------------------------------------*/
void
AP4_JsonInspector::AddFieldF(const char* name, float value, FormatHint /*hint*/)
{
OnFieldAdded();
m_Stream->WriteString(m_Prefix);
PrintFieldName(name);
char str[32];
AP4_FormatString(str, sizeof(str),
"%f",
value);
m_Stream->WriteString(str);
}
/*----------------------------------------------------------------------
| AP4_JsonInspector::AddField
+---------------------------------------------------------------------*/
void
AP4_JsonInspector::AddField(const char* name,
const unsigned char* bytes,
AP4_Size byte_count,
FormatHint /* hint */)
{
OnFieldAdded();
m_Stream->WriteString(m_Prefix);
PrintFieldName(name);
m_Stream->WriteString("\"[");
unsigned int offset = 1;
char byte[4];
for (unsigned int i = 0; i < byte_count; i++) {
AP4_FormatString(byte, 4, " %02x", bytes[i]);
m_Stream->Write(&byte[offset], 3 - offset);
offset = 0;
}
m_Stream->WriteString("]\"");
}
|