├── .gitignore ├── LICENSE.txt ├── README.md ├── app.py ├── bitstream.py ├── doc.py ├── doc ├── PLUGIN.md └── image │ ├── complicated.png │ ├── mainwnd.png │ ├── perspective.png │ └── plugin.png ├── plugin.py ├── plugins ├── h264 │ ├── doc.md │ ├── img │ │ ├── nal_unit_type1.png │ │ └── nal_unit_type2.png │ └── main.py ├── ts │ ├── doc.md │ └── main.py └── wav │ ├── doc.md │ ├── img │ └── wav-sound-format.gif │ └── main.py ├── requirements.txt ├── table.py └── viewdata.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.h264 2 | **/__pycache__ 3 | .vscode 4 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 这是一个通用的二进制数据分析工具。 2 | 3 | # 它能做什么 4 | 5 | - 分析任意格式的二进制数据,还能同时查看协议文档 6 | - 逐字节、逐位分析 7 | - 手动、自动分析 8 | - 对分析结果建透视图,发现规律,学习协议 9 | 10 | # 怎么做到的 11 | 12 | - 工具以插件化方式扩展协议的支持 13 | - 定义了易用的API供插件开发。插件中的协议解析代码几乎可以做到与协议文档逐行对应 14 | - 以markdown管理协议文档,既可以脱离工具翻阅,又可以在工具中自动呈现关联文档 15 | 16 | # 运行截图 17 | 18 | 主窗口运行效果 19 | 20 | ![mainwnd](doc/image/mainwnd.png) 21 | 22 | 透视图(根据分析需求自定义字段) 23 | 24 | ![perspective](doc/image/perspective.png) 25 | 26 | 插件开发示意(图中从左到右:wav头格式,插件解析代码,工具运行效果) 27 | 28 | ![plugin](doc/image/plugin.png) 29 | 30 | # 开发中 31 | 32 | 项目已经能基本运行,且支持了部分的wav/ts/h264协议,下载源码搭建python环境可以看到初步效果 33 | 34 | 在开发: 35 | 36 | - 界面与交互的完善 37 | - 工作区的概念(可保存和恢复操作记录) 38 | - 完善插件开发文档 39 | 40 | # 如何运行 41 | 42 | 后续计划为linux/mac/windows三个平台发布可执行程序 43 | 44 | 现在还只能在python环境下运行 45 | 46 | ``` 47 | pip install -r requirements.txt 48 | python app.py 49 | ``` 50 | 51 | 如果通过`requirements.txt`安装碰到问题,也可以直接运行`python app.py`,根据提示安装缺少的模块。 52 | 53 | 一般需要: 54 | 55 | ``` 56 | pip install markdown2 57 | pip install PySide2 58 | ``` 59 | 60 | 需要运行环境:python3 61 | 62 | # 需要你的帮助 63 | 64 | 这个工具的核心思想是“一个协议,一个插件;一次开发,永久受益”。 65 | 66 | 因此,只有众人拾柴,一起贡献各种协议、文件格式的解析插件,才能让工具真正强大起来。 67 | 68 | 插件编写说明文档:[plugin说明文档](doc/PLUGIN.md) 69 | 70 | 欢迎通过pull request的方式提交插件扩展。 71 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import random 3 | import markdown2 as mk 4 | import plugin 5 | from viewdata import * 6 | from table import * 7 | import time 8 | import traceback 9 | from PySide2.QtWidgets import * 10 | from PySide2.QtCore import * 11 | import re 12 | 13 | #菜单 文件(打开、最近文件)|插件(加载,查阅文档)|关于 14 | #最近文件 15 | #通过点选的方式创建透视图(而不是输入很长的名字) 16 | #透视图支持filter功能 17 | 18 | def show_modal_error(str): 19 | msg = QMessageBox() 20 | msg.setText(str) 21 | msg.exec_() 22 | 23 | def uneditable_item(s): 24 | item = QTableWidgetItem(s) 25 | item.setFlags(item.flags()&(~Qt.ItemFlag.ItemIsEditable)) 26 | return item 27 | 28 | class ContextViewer(QObject): 29 | show_item_requested = Signal(Table) 30 | 31 | def __init__(self): 32 | super(ContextViewer, self).__init__() 33 | self.fileds = [] 34 | self.widget = QTableWidget() 35 | self.widget.setColumnCount(3) 36 | self.widget.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows) 37 | self.widget.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection) 38 | self.widget.setHorizontalHeaderLabels(['解析器','位置', '备注']) 39 | self.widget.currentItemChanged.connect(self.__on_current_item_changed) 40 | self.widget.setContextMenuPolicy(Qt.CustomContextMenu) 41 | self.widget.customContextMenuRequested.connect(self.show_context_menu) 42 | self.widget.horizontalHeader().setHighlightSections(False) 43 | self.__menu = self.__create_menu() 44 | 45 | def __on_current_item_changed(self, cur, prev): 46 | self.notify_current_selected() 47 | 48 | def notify_current_selected(self): 49 | index = self.widget.currentIndex().row() 50 | if index >= 0 and index < len(self.fileds): 51 | item = self.fileds[index] 52 | self.show_item_requested.emit(item) 53 | 54 | def add(self, d:Table): 55 | row = len(self.fileds) 56 | self.fileds.append(d) 57 | 58 | self.widget.insertRow(row) 59 | self.widget.setItem(row, 0, uneditable_item(d.name)) 60 | self.widget.setItem(row, 1, uneditable_item(str(d.begin)+'=>'+str(d.end))) 61 | self.widget.horizontalHeader().resizeSections(QHeaderView.ResizeToContents) 62 | 63 | def __create_menu(self): 64 | menu = QMenu() 65 | menu.addAction('清空').triggered.connect(self.clear) 66 | menu.addAction('删除该项').triggered.connect(self.remove_current) 67 | return menu 68 | 69 | def remove_current(self): 70 | current = self.widget.currentIndex().row() 71 | if current >= 0 and current < len(self.fileds): 72 | self.widget.removeRow(current) 73 | del self.fileds[current] 74 | 75 | def clear(self): 76 | self.fileds.clear() 77 | self.widget.setRowCount(0) 78 | self.widget.clearContents() 79 | 80 | def get_context(self): 81 | context = Table('context', None, None) 82 | for field in self.fileds: 83 | context.add_exist(field) 84 | return context 85 | 86 | def show_context_menu(self, point): 87 | self.__menu.exec_(self.widget.mapToGlobal(point)) 88 | 89 | class HistoryViewer(QObject): 90 | show_item_requested = Signal(Table) 91 | perspective_requested = Signal(list) 92 | 93 | def __init__(self, max_count=100): 94 | super(HistoryViewer, self).__init__() 95 | self.widget = QTableWidget() 96 | self.widget.setColumnCount(4) 97 | self.widget.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows) 98 | self.widget.setSelectionMode(QAbstractItemView.SelectionMode.ExtendedSelection) 99 | self.widget.setHorizontalHeaderLabels(['时间','解析器','位置', '备注']) 100 | self.widget.currentItemChanged.connect(self.__on_current_item_changed) 101 | self.widget.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu) 102 | self.widget.horizontalHeader().setHighlightSections(False) 103 | self.__max_count = max_count 104 | self.__history = [] 105 | 106 | self.__actions = [] 107 | self.__add_action('删除', self.delete_items) 108 | self.__add_action('创建透视表', self.create_perspective_request) 109 | self.__add_action('清空', self.clear) 110 | 111 | def __add_action(self, name, proc): 112 | action = QtWidgets.QAction(name) 113 | action.triggered.connect(proc) 114 | self.__actions.append(action) 115 | self.widget.addAction(action) 116 | 117 | def get_selected_rows(self): 118 | items = self.widget.selectedItems() 119 | rows = [] 120 | for item in items: 121 | if not item.row() in rows: 122 | rows.append(item.row()) 123 | return rows 124 | 125 | def create_perspective_request(self): 126 | rows = self.get_selected_rows() 127 | rows.sort() 128 | self.perspective_requested.emit([self.__history[i] for i in rows]) 129 | 130 | def delete_items(self): 131 | rows = self.get_selected_rows() 132 | rows.sort(reverse=True) 133 | for row in rows: 134 | self.widget.removeRow(row) 135 | 136 | def __on_current_item_changed(self, cur, prev): 137 | self.notify_current_selected() 138 | 139 | def notify_current_selected(self): 140 | index = self.widget.currentIndex().row() 141 | if index >= 0 and index < len(self.__history): 142 | d = self.__history[index] 143 | self.show_item_requested.emit(d) 144 | else: 145 | self.show_item_requested.emit(None) 146 | 147 | def add(self, d:Table): 148 | if len(self.__history) >= self.__max_count: 149 | del self.__history[0] 150 | self.widget.removeRow(0) 151 | row = len(self.__history) 152 | self.__history.append(d) 153 | self.widget.insertRow(row) 154 | self.widget.setItem(row, 0, uneditable_item(time.strftime('%H:%M:%S', time.localtime()))) 155 | self.widget.setItem(row, 1, uneditable_item(d.name)) 156 | self.widget.setItem(row, 2, uneditable_item(str(d.begin)+'=>'+str(d.end))) 157 | self.widget.clearSelection() 158 | self.widget.setCurrentIndex(self.widget.model().index(row,0)) 159 | self.widget.horizontalHeader().resizeSections(QHeaderView.ResizeToContents) 160 | 161 | def clear(self): 162 | self.__history.clear() 163 | self.widget.setRowCount(0) 164 | self.widget.clearContents() 165 | 166 | 167 | 168 | class DocViewer: 169 | def __init__(self): 170 | self.editor = QTextEdit() 171 | self.editor.setReadOnly(True) 172 | self.img_path='' 173 | 174 | def __fix_img_path(self, matched): 175 | return '![%s](%s)'%(matched.group(1), self.img_path+'/'+matched.group(2)) 176 | 177 | def __markup_doc(self, text): 178 | text=re.sub(r'!\[(\w+)\]\((\w+/\w+.\w+)\)', self.__fix_img_path, text) 179 | body = mk.markdown(text, extras=['tables','code-friendly']) 180 | html = ''' 181 | 182 | 193 | 194 | '''+body+'' 195 | return html 196 | 197 | def clear(self): 198 | self.editor.clear() 199 | 200 | def load(self,text): 201 | self.editor.setHtml(self.__markup_doc(text)) 202 | 203 | class TableViewer(QObject): 204 | field_selected = Signal(Field, str) 205 | add_field_to_context = Signal(Table) 206 | 207 | def __init__(self): 208 | super(TableViewer, self).__init__() 209 | self.__fields = {} 210 | self.__setup_ui() 211 | 212 | def __setup_ui(self): 213 | self.widget = QTreeWidget() 214 | self.widget.setColumnCount(3) 215 | self.widget.setHeaderLabels(['名字','值','长度(bits)']) 216 | self.widget.currentItemChanged.connect(self.__on_current_item_changed) 217 | self.widget.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu) 218 | 219 | self.__actions = [] 220 | self.__add_action('添加到上下文', self.add_current_to_context) 221 | self.__add_action('复制', self.copy_item) 222 | 223 | def __add_action(self, name, proc): 224 | action = QtWidgets.QAction(name) 225 | action.triggered.connect(proc) 226 | self.__actions.append(action) 227 | self.widget.addAction(action) 228 | 229 | def copy_item(self): 230 | item = self.widget.currentItem() 231 | col = self.widget.currentColumn() 232 | if self.__fields.__contains__(item): 233 | (field, dockey) = self.__fields[item] 234 | clipboard = QApplication.clipboard() 235 | if col == 0: 236 | clipboard.setText(field.name) 237 | elif col == 1: 238 | clipboard.setText(str(field.value)) 239 | elif col == 2: 240 | clipboard.setText(str(field.end.bits_count()-field.begin.bits_count())) 241 | 242 | def show_context_menu(self, point): 243 | self.__menu.exec_(self.widget.mapToGlobal(point)) 244 | 245 | def __on_current_item_changed(self): 246 | item = self.widget.currentItem() 247 | if self.__fields.__contains__(item): 248 | (field, dockey) = self.__fields[item] 249 | self.field_selected.emit(field, dockey) 250 | else: 251 | self.field_selected.emit(None, None) 252 | 253 | def add_current_to_context(self): 254 | item = self.widget.currentItem() 255 | if self.__fields.__contains__(item): 256 | d = self.__fields[item][0] 257 | if type(d) != Table: 258 | show_modal_error('字段值不支持加入到上下文中') 259 | return 260 | self.add_field_to_context.emit(d) 261 | 262 | 263 | def __field_value_str(self,value): 264 | if type(value) == bytearray or type(value) == bytes: 265 | return '%d字节数组'%(len(value)) 266 | else: 267 | return str(value) 268 | 269 | def __print_table(self, d:Table): 270 | item = QTreeWidgetItem() 271 | self.__fields[item] = (d, d.name) 272 | item.setText(0, d.name) 273 | item.setText(1, str(d.begin)+'=>'+str(d.end)) 274 | item.setText(2, str(d.end.bits_count() - d.begin.bits_count())) 275 | for f in d.fields: 276 | if type(f) == Table: 277 | item.addChild(self.__print_table(f)) 278 | else: 279 | child = QTreeWidgetItem() 280 | self.__fields[child] = (f, d.name+'.'+f.name) 281 | child.setText(0, f.name) 282 | child.setText(1, self.__field_value_str(getattr(d, f.name))) 283 | child.setText(2, str(f.end.bits_count() - f.begin.bits_count())) 284 | item.addChild(child) 285 | return item 286 | 287 | def load(self, d: Table): 288 | self.__fields.clear() 289 | self.widget.clear() 290 | if d != None: 291 | top = self.__print_table(d) 292 | self.widget.addTopLevelItem(top) 293 | self.widget.expandAll() 294 | self.widget.header().resizeSections(QHeaderView.ResizeToContents) 295 | self.widget.setCurrentIndex(self.widget.model().index(0,0)) 296 | 297 | class ChooseParserDlg(QDialog): 298 | default_changed = Signal(str) 299 | 300 | def __init__(self, p, default): 301 | super(ChooseParserDlg, self).__init__() 302 | self.setWindowTitle('选择解析器') 303 | self.parsers = list(p.exports.keys()) 304 | self.default = default 305 | self.list = QListWidget() 306 | self.list.itemDoubleClicked.connect(self.accept) 307 | self.__udpate_list() 308 | 309 | self.input = QLineEdit() 310 | self.input.setText(default) 311 | self.input.textEdited.connect(self.__udpate_list) 312 | 313 | self.count = QSpinBox() 314 | self.count.setMinimum(1) 315 | self.count.setMaximum(10000) 316 | self.count.setValue(1) 317 | 318 | hbox = QHBoxLayout() 319 | btn = QPushButton('设为默认') 320 | btn.clicked.connect(self.__on_set_default_clicked) 321 | hbox.addWidget(btn) 322 | btn = QPushButton('确定') 323 | btn.setDefault(True) 324 | btn.clicked.connect(self.accept) 325 | hbox.addWidget(btn) 326 | btn = QPushButton('取消') 327 | btn.clicked.connect(self.reject) 328 | hbox.addWidget(btn) 329 | 330 | vbox = QVBoxLayout() 331 | vbox.addWidget(self.input) 332 | vbox.addWidget(self.list) 333 | vbox.addWidget(self.count) 334 | vbox.addLayout(hbox) 335 | vbox.setStretch(1, 1) 336 | 337 | self.setLayout(vbox) 338 | 339 | def __udpate_list(self, filter=None): 340 | self.list.clear() 341 | if filter != None and len(filter) > 0: 342 | self.list.addItems([p for p in self.parsers if filter in p]) 343 | else: 344 | self.list.addItems(self.parsers) 345 | self.list.setCurrentRow(self.parsers.index(self.default)) 346 | 347 | def __on_set_default_clicked(self): 348 | self.default = self.list.currentItem().text() 349 | self.default_changed.emit(self.default) 350 | 351 | #todo 改造为向导视图 352 | #Step1 可预览地创建fields 353 | #Step2 查看透视图结果 354 | #返回Step1或关闭 355 | class PerspectiveDlg(QDialog): 356 | show_item_requested = Signal(Table) 357 | 358 | def header_labels(self, fields): 359 | labels = [] 360 | for f in fields: 361 | labels.append(f.split('.')[-1]) 362 | return labels 363 | 364 | def __init__(self, items, fields): 365 | super(PerspectiveDlg, self).__init__() 366 | self.setWindowTitle('透视图') 367 | self.items = items 368 | self.table = QTableWidget() 369 | self.table.setColumnCount(len(fields)) 370 | self.table.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows) 371 | self.table.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection) 372 | self.table.setHorizontalHeaderLabels(self.header_labels(fields)) 373 | self.table.currentItemChanged.connect(self.__on_current_item_changed) 374 | for i in range(len(items)): 375 | item = items[i] 376 | self.table.insertRow(i) 377 | for j in range(len(fields)): 378 | text = str(item.get_value_by_path(fields[j].strip())) 379 | self.table.setItem(i, j, QTableWidgetItem(text)) 380 | self.table.horizontalHeader().resizeSections(QHeaderView.ResizeToContents) 381 | 382 | layout = QVBoxLayout() 383 | layout.addWidget(self.table) 384 | self.setLayout(layout) 385 | self.resize(500, 800) 386 | 387 | def __on_current_item_changed(self): 388 | r = self.table.currentRow() 389 | if r < 0 or r >= len(self.items): 390 | return 391 | 392 | self.show_item_requested.emit(self.items[r]) 393 | 394 | 395 | class MainWnd(QMainWindow): 396 | def __init__(self): 397 | super().__init__() 398 | self.file_viewer = None 399 | self.memory_viewer = None 400 | self.plugin = None 401 | self.perspective_dlg = None 402 | 403 | self.setup_ui() 404 | 405 | def setup_ui(self): 406 | self.setWindowTitle('bitinsight') 407 | layout = QGridLayout() 408 | 409 | # 左侧视图 410 | left_min_width = 250 411 | left_max_width = 400 412 | self.editor_tab = QTabWidget() 413 | self.editor_tab.currentChanged.connect(self.on_editor_tab_changed) 414 | self.data_nav = DataNav() 415 | self.editor_tab.setMinimumWidth(left_min_width) 416 | self.editor_tab.setMaximumWidth(left_max_width) 417 | self.data_nav.setMinimumWidth(left_min_width) 418 | self.data_nav.setMaximumWidth(left_max_width) 419 | 420 | # 中间视图 421 | mid_min_width = 350 422 | self.table_viewer = TableViewer() 423 | self.doc_viewer = DocViewer() 424 | self.table_viewer.field_selected.connect(self.on_field_selected) 425 | self.table_viewer.widget.setMinimumWidth(mid_min_width) 426 | 427 | # 右侧视图 428 | self.record_tab = QTabWidget() 429 | self.history_viewer = HistoryViewer() 430 | self.history_viewer.show_item_requested.connect(self.on_show_parser_ret) 431 | self.history_viewer.perspective_requested.connect(self.create_perspective) 432 | self.context_viewer = ContextViewer() 433 | self.table_viewer.add_field_to_context.connect(self.on_add_field_context) 434 | self.context_viewer.show_item_requested.connect(self.on_show_parser_ret) 435 | self.record_tab.addTab(self.history_viewer.widget, '历史记录') 436 | self.record_tab.addTab(self.context_viewer.widget, '上下文') 437 | self.record_tab.currentChanged.connect(self.on_record_tab_changed) 438 | 439 | # 布局 440 | c1=1 441 | c2=1 442 | c3=1 443 | r1=9 444 | r2=1 445 | r_1=7 446 | r_2=3 447 | layout.addWidget(self.editor_tab, 0, 0, r1, c1) 448 | layout.addWidget(self.data_nav, r1, 0, r2, c1) 449 | layout.addWidget(self.table_viewer.widget, 0, c1, r_1, c2) 450 | layout.addWidget(self.record_tab, 0, c1+c2, r_1, c3) 451 | layout.addWidget(self.doc_viewer.editor, r_1, c1, r_2, c2+c3) 452 | layout.setColumnStretch(c1, 1) 453 | layout.setSpacing(3) 454 | 455 | self.setMenuBar(self.create_menu_bar()) 456 | self.setCentralWidget(QWidget()) 457 | self.centralWidget().setLayout(layout) 458 | 459 | def create_perspective(self, items): 460 | (text, ok) = QInputDialog.getMultiLineText(self, '输入透视图字段', '每行输入一个字段,按照如"header.size"这样的点号分割形式填写') 461 | if ok: 462 | self.perspective_dlg = PerspectiveDlg(items, text.split('\n')) 463 | self.perspective_dlg.show_item_requested.connect(self.on_show_parser_ret) 464 | self.perspective_dlg.show() 465 | 466 | def on_add_field_context(self,f): 467 | self.context_viewer.add(f) 468 | 469 | def __del_memory_viewer(self): 470 | if self.memory_viewer != None: 471 | del self.memory_viewer 472 | self.memory_viewer = None 473 | self.editor_tab.removeTab(1) 474 | 475 | def __check_memory_viewer(self, f:Field): 476 | bs = f.bs 477 | if bs.mem != self.file_viewer.mem: 478 | if self.memory_viewer != None and self.memory_viewer.mem == bs.mem: 479 | self.editor_tab.setCurrentIndex(1) 480 | return self.memory_viewer 481 | self.__del_memory_viewer() 482 | 483 | self.memory_viewer = MemoryViewer(bs.mem) 484 | self.memory_viewer.editor.analyse_requested.connect(self.on_analyse_request) 485 | self.memory_viewer.editor.default_analyse_requested.connect(self.on_default_parser_analyse) 486 | self.editor_tab.addTab(self.memory_viewer.editor, f.name) 487 | self.editor_tab.setCurrentIndex(1) 488 | return self.memory_viewer 489 | else: 490 | self.__del_memory_viewer() 491 | self.editor_tab.setCurrentIndex(0) 492 | return self.file_viewer 493 | 494 | def on_show_parser_ret(self, d: Table): 495 | self.table_viewer.load(d) 496 | 497 | def current_data_viewer(self): 498 | index = self.editor_tab.currentIndex() 499 | if index < 0: 500 | return None 501 | viewer = self.file_viewer if index == 0 else self.memory_viewer 502 | return viewer 503 | 504 | def on_editor_tab_changed(self): 505 | viewer = self.current_data_viewer() 506 | if viewer == None: 507 | return 508 | self.data_nav.set_data_viewer(viewer) 509 | 510 | def on_record_tab_changed(self): 511 | viewer = self.history_viewer if self.record_tab.currentIndex() == 0 else self.context_viewer 512 | viewer.notify_current_selected() 513 | 514 | def on_field_selected(self, f:Field, dockey:str): 515 | if dockey == None: 516 | self.doc_viewer.clear() 517 | else: 518 | self.doc_viewer.load(self.plugin.doc.get(dockey)) 519 | 520 | if f == None: 521 | if self.memory_viewer: 522 | self.file_viewer.clear_higlight() 523 | if self.memory_viewer: 524 | self.memory_viewer.clear_higlight() 525 | else: 526 | viewer = self.__check_memory_viewer(f) 527 | viewer.highlight(f.begin, f.end) 528 | 529 | def set_plugin_default(self, s): 530 | self.plugin.default = s 531 | if self.file_viewer: 532 | self.file_viewer.editor.set_default_parser(s) 533 | if self.memory_viewer: 534 | self.memory_viewer.editor.set_default_parser(s) 535 | 536 | def on_analyse(self, bs:BitStream, parser, count): 537 | for i in range(count): 538 | d = Table('root', self.context_viewer.get_context(), bs) 539 | try: 540 | d.add_table(self.plugin.exports[parser]) 541 | except Exception as e: 542 | if type(e) == EOFError: 543 | show_modal_error('数据不足,请加载更多数据') 544 | else: 545 | et, ev, tb = sys.exc_info() 546 | msg = traceback.format_exception(et, ev, tb) 547 | show_modal_error('解析过程出错:%s\n%s'%(str(e), ''.join(msg))) 548 | return 549 | 550 | ret = getattr(d, parser) 551 | self.history_viewer.add(ret) 552 | self.current_data_viewer().moveto(bs.pos()) 553 | 554 | def on_default_parser_analyse(self, bs: BitStream): 555 | self.on_analyse(bs, self.plugin.default, 1) 556 | 557 | def on_analyse_request(self, bs: BitStream): 558 | if self.plugin == None: 559 | show_modal_error('还未加载插件') 560 | return 561 | 562 | dlg = ChooseParserDlg(self.plugin, self.plugin.default) 563 | dlg.default_changed.connect(self.set_plugin_default) 564 | if QDialog.Rejected == dlg.exec_(): 565 | return 566 | 567 | parser = dlg.list.currentItem().text() 568 | count = dlg.count.value() 569 | 570 | self.on_analyse(bs, parser, count) 571 | 572 | def menu_open_file(self): 573 | ret = QFileDialog.getOpenFileName(self, '选择要打开的文件') 574 | path = ret[0] 575 | if len(path) == 0: 576 | return 577 | 578 | if self.file_viewer: 579 | e = self.file_viewer.editor 580 | 581 | self.editor_tab.clear() 582 | self.file_viewer = None 583 | self.memory_viewer = None 584 | #在不同文件间共享有意义吗?(开启会导致highlight失败) 585 | self.history_viewer.clear() 586 | self.context_viewer.clear() 587 | 588 | file_info = QFileInfo(path) 589 | self.file_viewer = FileViewer(path) 590 | self.file_viewer.editor.analyse_requested.connect(self.on_analyse_request) 591 | self.file_viewer.editor.default_analyse_requested.connect(self.on_default_parser_analyse) 592 | self.editor_tab.insertTab(0, self.file_viewer.editor, file_info.fileName()) 593 | 594 | self.table_viewer.widget.clear() 595 | 596 | suffix = file_info.suffix() 597 | if not self.plugin or self.plugin.name != suffix: 598 | self.plugin = plugin.load_plugin(file_info.suffix()) 599 | self.doc_viewer.img_path = 'plugins/'+suffix 600 | self.set_plugin_default(self.plugin.default) 601 | 602 | 603 | def config_recent_files_menu(self, menu): 604 | pass 605 | 606 | def config_file_menu(self,menu): 607 | menu.addAction('打开...').triggered.connect(self.menu_open_file) 608 | self.config_recent_files_menu(menu.addMenu('最近打开的文件')) 609 | 610 | def create_menu_bar(self): 611 | menubar = QMenuBar() 612 | self.config_file_menu(menubar.addMenu('文件')) 613 | return menubar 614 | 615 | if __name__ == "__main__": 616 | app = QApplication([]) 617 | 618 | widget = MainWnd() 619 | widget.resize(800, 600) 620 | widget.show() 621 | 622 | sys.exit(app.exec_()) 623 | -------------------------------------------------------------------------------- /bitstream.py: -------------------------------------------------------------------------------- 1 | from copy import copy 2 | from abc import ABCMeta, abstractmethod 3 | import math 4 | import mmap 5 | 6 | class BitPos: 7 | def __init__(self, byte_pos, bit_pos): 8 | self.byte = byte_pos 9 | self.bit = bit_pos 10 | 11 | def step(self): 12 | self.bit += 1 13 | if self.bit >= 8: 14 | self.byte += 1 15 | self.bit = 0 16 | 17 | def add(self, byte, bit): 18 | self.byte += byte 19 | self.bit += bit 20 | while self.bit >= 8: 21 | self.bit -= 8 22 | self.byte += 1 23 | 24 | def bits_to_bound(self): 25 | return 8 - self.bit 26 | 27 | def bits_count(self): 28 | return (self.byte << 3) + self.bit 29 | 30 | def __str__(self): 31 | return "(%d,%d)" % (self.byte, self.bit) 32 | 33 | 34 | class BitStream: 35 | __metaclass__ = ABCMeta 36 | 37 | def __init__(self, mem): 38 | self._pos = BitPos(0, 0) 39 | self.bigendien = True 40 | self._end_pos = BitPos(len(mem), 0) 41 | self.mem = mem 42 | 43 | def pos(self): 44 | return copy(self._pos) 45 | 46 | def set_end_pos(self, byte, bit): 47 | self._end_pos.byte = byte 48 | self._end_pos.bit = bit 49 | 50 | def moveto(self, byte, bit): 51 | self._pos.byte = byte 52 | self._pos.bit = bit 53 | 54 | def skip(self, byte, bit = 0): 55 | self._pos.add(byte, bit) 56 | 57 | def eos(self): 58 | return self._pos.byte >= self._end_pos.byte 59 | 60 | def available(self): 61 | return self._end_pos.bits_count() - self._pos.bits_count() 62 | 63 | def __check_eos(self): 64 | if self.eos(): 65 | raise EOFError() 66 | 67 | def read_bytes_to_str(self, count, forward=True): 68 | ret = self.read_bytes_to_array(count, forward) 69 | return ret.decode('utf-8') 70 | 71 | def read_bytes_to_array(self, count, forward=True): 72 | self.to_byte_end() 73 | e = None 74 | ret = bytearray() 75 | 76 | if not forward: 77 | pos = copy(self._pos) 78 | 79 | while count > 0: 80 | if self.eos(): 81 | e = EOFError() 82 | break 83 | ret.append(self.mem[self._pos.byte]) 84 | self._pos.byte += 1 85 | count -= 1 86 | 87 | if not forward: 88 | self._pos = pos 89 | 90 | if not e == None: 91 | raise e 92 | 93 | return ret 94 | 95 | def read_a_byte(self): 96 | self.to_byte_end() 97 | self.__check_eos() 98 | 99 | ret = self.mem[self._pos.byte] 100 | self._pos.byte += 1 101 | 102 | return ret 103 | 104 | def read_bytes(self, count, forward=True): 105 | ret = self.read_bytes_to_array(count, forward) 106 | return self.__to_number(ret) 107 | 108 | def read_a_bit(self): 109 | bit = ((self.mem[self._pos.byte] >> (8 - self._pos.bit - 1)) & 0x01) 110 | self._pos.step() 111 | return bit 112 | 113 | def read_bits_to_array(self, count, forward=True): 114 | bits = bytearray() 115 | e = None 116 | 117 | if not forward: 118 | pos = copy(self._pos) 119 | 120 | for i in range(count): 121 | if self.eos(): 122 | e = EOFError() 123 | break 124 | bits.append(self.read_a_bit()) 125 | 126 | if not forward: 127 | self._pos = pos 128 | 129 | if not e == None: 130 | raise e 131 | 132 | ret = bytearray() 133 | tail = count - int(math.floor(count/8)) * 8 134 | index = 0 135 | if tail > 0: 136 | b = 0 137 | for i in range(tail): 138 | b |= (bits[i] << (tail - i - 1)) 139 | ret.append(b) 140 | index += tail 141 | 142 | while index < count: # 剩下肯定是8的倍数了 143 | b = 0 144 | for i in range(8): 145 | b |= (bits[index+i] << (7 - i)) 146 | ret.append(b) 147 | index += 8 148 | 149 | return ret 150 | 151 | def __to_number(self,arr): 152 | num=0 153 | size=len(arr) 154 | for i in range(size): 155 | if self.bigendien: 156 | num=(num << 8) | arr[i] 157 | else: 158 | num=(num << 8) | arr[size - i -1] 159 | return num 160 | 161 | def read_bits(self, count, forward=True): 162 | ret=self.read_bits_to_array(count, forward) 163 | return self.__to_number(ret) 164 | 165 | def read_bitss(self, count, times, forward=True): 166 | ret = [] 167 | for i in range(times): 168 | ret.append(self.read_bits(count, forward)) 169 | return ret 170 | 171 | def read_ue_golomb(self): 172 | i=0 173 | while not self.eos() and i < 32 and self.read_a_bit() == 0: 174 | i += 1 175 | ret=self.read_bits(i) 176 | ret += (1 << i)-1 177 | return ret 178 | 179 | def read_ue_golombs(self, count): 180 | ret=[] 181 | for i in range(count): 182 | ret.append(self.read_ue_golomb()) 183 | return ret 184 | 185 | def read_se_golomb(self): 186 | ret=self.read_ue_golomb() 187 | if (ret & 0x01) > 0: 188 | ret=(ret + 1) / 2 189 | else: 190 | ret=-(ret/2) 191 | return ret 192 | 193 | def read_se_golombs(self, count): 194 | ret=[] 195 | for i in range(count): 196 | ret.append(self.read_se_golomb()) 197 | return ret 198 | 199 | def to_byte_end(self): 200 | if self._pos.bit > 0: 201 | self._pos.byte += 1 202 | self._pos.bit=0 -------------------------------------------------------------------------------- /doc.py: -------------------------------------------------------------------------------- 1 | class Doc: 2 | def __init__(self): 3 | self.__fields = {} 4 | 5 | def __parse_title(self, line): 6 | if line.startswith('# '): 7 | return (1, line[2:].strip('\r\n ')) 8 | elif line.startswith('## '): 9 | return (2, line[3:].strip()) 10 | return (0,line) 11 | 12 | def parse(self,path): 13 | try: 14 | f = open(path, 'r') 15 | except FileNotFoundError as e: 16 | print(e) 17 | return 18 | 19 | prefix = [] 20 | level = 0 21 | content = [] 22 | line = f.readline() 23 | while line: 24 | cur_level,title = self.__parse_title(line) 25 | if cur_level > 0: 26 | if level > 0: 27 | self.__fields['.'.join(prefix)] = ''.join(content) 28 | content.clear() 29 | 30 | if cur_level == 1: 31 | prefix = [title] 32 | elif cur_level == 2: 33 | prefix = [prefix[0], title] 34 | level = cur_level 35 | 36 | content.append(line) 37 | line = f.readline() 38 | 39 | if level > 0: 40 | self.__fields['.'.join(prefix)] = ''.join(content) 41 | content.clear() 42 | 43 | f.close() 44 | 45 | def get(self, name): 46 | if self.__fields.__contains__(name): 47 | return self.__fields[name] 48 | else: 49 | return '**无文档**' -------------------------------------------------------------------------------- /doc/PLUGIN.md: -------------------------------------------------------------------------------- 1 | # 目录结构 2 | 3 | 插件在目录`plugins/`下 4 | 5 | 一个插件需要具备以下目录结构体(以`plugins/wav`为例): 6 | 7 | ``` 8 | wav 9 | ├── doc.md 10 | └── main.py 11 | ``` 12 | 13 | - wav: 顶级目录名。一般以文件格式的后缀作为目录名,这允许程序在打开文件的时候能够自动加载所需插件。 14 | - main.py: 这里定义了为这个格式设计的解析器。加载插件的时候会扫描这个文件内的函数,创建解析器 15 | - doc.md: 这里描述解析语法 16 | 17 | # 解析器 18 | 19 | 签名形如`def func(t:Table, bs:BitStream)`的函数被称为解析器。 20 | 21 | 在一个`main.py`中可以定义多个解析器(比如h264插件,就定义了`nal_unit/slice_header/pic_parameter_set_rbsp`等解析器) 22 | 23 | 编写解析器只需要了解两个类:`Table`和`BitStream` 24 | 25 | `Table`定义在`table.py`。解析器输出的字段存放在这里。 26 | 27 | 常见函数有`add_field`和`add_table`,可以分别添加一个字段,或添加字段集(即Table嵌套)。 28 | 29 | 比如wav插件中,要输出一个`ChunkID`字段,调用`add_field`即可:`t.add_field('ChunkID',...` 30 | 31 | `BitStream`定义在`bitstream.py`。为解析器提供比特流,可以按比特或按字节读取数据。 32 | 33 | 基础函数有`read_bits`,`read_bytes`,`read_a_bit`,`read_a_byte`,`read_bytes_to_str`等 34 | 35 | 还会根据需要,提供和扩展一些解码函数,如`read_ue_glomb` 36 | 37 | 了解了这两个文件的API后,就可以开始编写插件了。 38 | 39 | 如`wav`插件的编写,示例如下。 40 | 41 | 首先理解wav格式: 42 | 43 | ![wav](../plugins/wav/img/wav-sound-format.gif) 44 | 45 | 接着,按照插件目录结构,创建`plugins/wav`目录: 46 | 47 | ``` 48 | wav 49 | ├── doc.md 50 | └── main.py 51 | ``` 52 | 53 | 最后,在main.py中根据wav格式编写代码: 54 | 55 | ```python 56 | from table import Table 57 | from bitstream import BitStream 58 | 59 | def header(t:Table, bs:BitStream): 60 | bs.bigendien = False 61 | t.add_field('ChunkID', bs.read_bytes_to_str, count=4) 62 | t.add_field('ChunkSize', bs.read_bytes, count=4) 63 | t.add_field('Format', bs.read_bytes_to_str, count=4) 64 | t.add_field('Subchunk1ID', bs.read_bytes_to_str, count=4) 65 | t.add_field('Subchunk1Size', bs.read_bytes, count=4) 66 | t.add_field('AudioFormat', bs.read_bytes, count=2) 67 | t.add_field('NumChannels', bs.read_bytes, count=2) 68 | t.add_field('SampleRate', bs.read_bytes, count=4) 69 | t.add_field('ByteRate', bs.read_bytes, count=4) 70 | t.add_field('BlockAlign', bs.read_bytes, count=2) 71 | t.add_field('BitsPerSample', bs.read_bytes, count=2) 72 | t.add_field('Subchunk2ID', bs.read_bytes_to_str, count=4) 73 | t.add_field('Subchunk2Size', bs.read_bytes, count=4) 74 | ``` 75 | 76 | > bs.bigendien = False,让bitstream以小端形式读取整形值。(字符串默认都是大端,即使bigendien=False) 77 | 78 | 这样就完成了一个wav插件的编写(只是这里我们只定义了一个解析器) 79 | 80 | # 文档 81 | 82 | 文档以markdown语法编写。 83 | 84 | 编写过程中**标题**需要遵守本工具的特定规则,以允许工具能正确建立字段和文档的对应关系。 85 | 86 | 标题规则如下: 87 | 88 | - 一级标题需要是解析器名(考虑嵌套Table时,准确的说法是一级标题需要是Table名) 89 | - 二级标题需要是字段名 90 | - 不支持三级及以上标题 91 | 92 | 如,wav中doc.md截取如下: 93 | 94 | ```markdown 95 | # header 96 | 97 | ![fmt](img/wav-sound-format.gif) 98 | 99 | ## ChunkID 100 | 101 | Contains the letters "RIFF" in ASCII form 102 | ``` 103 | 104 | 一级标题header与解析器`header`同名; 105 | 106 | 二级标题ChunkID与解析器`header`中的`ChunkID`同名,其中`ChunkID`是表`header`中通过`add_field`添加的 107 | 108 | 这样当用户通过工具选择到header时,将会在文档窗口呈现`img/wav-sound-format.gif`图片,选择`ChunkID`时,将会呈现`Contains the letters "RIFF" in ASCII form`这段文本。 109 | 110 | # 高级特性 111 | 112 | ## 默认解析器 113 | 114 | 默认解析器是方便用户快捷使用的别名,在工具中通过`Ctrl+D`触发。 115 | 116 | 在`main.py`中名为`default_parser`的函数将被加载为默认解析器。 117 | 118 | 因为默认解析器一般会被连续调用,因此,默认解析器应当具备自动识别语法边界的能力。 119 | 120 | 具体而言,以h264为例。 h264的nalu一般以`00 00 00 01`或`00 00 01`开头,因此,其`default_parser`会先在`bitstream`中查找该特征定位到nalu,然后开始解析。 121 | 122 | 如果`main.py`没有指定默认解析器,则插件加载时第一个被加载的解析器作为默认解析器(一般是按字母序) 123 | 124 | ## 上下文 125 | 126 | 某些格式的解析需要依赖之前的解析结果。 127 | 128 | 比如h264的`slice_header`解析中需要参考pps/sps。 129 | 130 | 用户使用工具时,会手动将所需的pps/sps添加到上下文中。 131 | 132 | 插件编写中,需要从`Table.context`中读取,如果未读到,需要抛出异常提示用户。 133 | 134 | 如: 135 | 136 | ```python 137 | try: 138 | nal_unit = d.context.nal_unit 139 | except AttributeError as e: 140 | raise Exception('no nal_unit in ctx, abort!') 141 | ``` 142 | 143 | ## 复杂字段 144 | 145 | 有时候会碰上复杂的字段定义,比如h264 pps中的: 146 | 147 | ![pps](image/complicated.png) 148 | 149 | 因为`top_left`和`bottom_right`是在一个循环中交替赋值的,所以不能通过简单地`add_field`或者`add_table`取定义。 150 | 151 | ```python 152 | def __read_topleft_bottomright(d, bs): 153 | d.top_left = [] 154 | d.bottom_right = [] 155 | for i in range(d.num_slice_groups_minus1+1): 156 | d.top_left.append(bs.read_ue_golomb()) 157 | d.bottom_right.append(bs.read_ue_golomb()) 158 | 159 | d.add_fields(__read_topleft_bottomright, 'top_left', 'bottom_right') 160 | ``` 161 | 162 | 这时通过`add_fields`一次性定义多个字段(缺点是这几个字段的位置信息会是一样的) -------------------------------------------------------------------------------- /doc/image/complicated.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/compilelife/bitinsight/40d0a7f280211bae979500d49cb5cd35e741c174/doc/image/complicated.png -------------------------------------------------------------------------------- /doc/image/mainwnd.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/compilelife/bitinsight/40d0a7f280211bae979500d49cb5cd35e741c174/doc/image/mainwnd.png -------------------------------------------------------------------------------- /doc/image/perspective.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/compilelife/bitinsight/40d0a7f280211bae979500d49cb5cd35e741c174/doc/image/perspective.png -------------------------------------------------------------------------------- /doc/image/plugin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/compilelife/bitinsight/40d0a7f280211bae979500d49cb5cd35e741c174/doc/image/plugin.png -------------------------------------------------------------------------------- /plugin.py: -------------------------------------------------------------------------------- 1 | from importlib import import_module 2 | import inspect 3 | from table import Table 4 | from bitstream import BitStream 5 | from doc import Doc 6 | 7 | class Plugin: 8 | def __init__(self): 9 | self.exports = {} 10 | self.doc = Doc() 11 | self.default = '' 12 | self.name = '' 13 | 14 | def load_plugin(name): 15 | mod = import_module('.main', 'plugins.'+name) 16 | bs = BitStream(bytes([])) 17 | t = Table('inspect', None, bs) 18 | plugin = Plugin() 19 | plugin.name = name 20 | 21 | for attr in dir(mod): 22 | if attr.startswith('__'): 23 | continue 24 | obj = getattr(mod, attr) 25 | if not inspect.isfunction(obj): 26 | continue 27 | sig = inspect.signature(obj) 28 | try: 29 | sig.bind(t, bs) 30 | if attr == 'default_parser': 31 | plugin.default = attr 32 | plugin.exports[attr] = obj 33 | except TypeError as e: 34 | continue 35 | 36 | if len(plugin.default) == 0 and len(plugin.exports) > 0: 37 | plugin.default = list(plugin.exports.keys())[0] 38 | plugin.doc.parse('plugins/'+name+'/doc.md') 39 | return plugin 40 | -------------------------------------------------------------------------------- /plugins/h264/doc.md: -------------------------------------------------------------------------------- 1 | # nal_unit 2 | 3 | ## nal_ref_idc 4 | 5 | 不等于 0 时,规定 NAL 单元的内容包含一个序列参数集,或一个图像参数集,或一个参考图像条带,或一个参考图像的条带数据分割。如果一个包含一个条带或条带数据分割的 NAL 单元的 nal_ref_idc 等于 0 时,该条带或条带数据分割是一个非参考图像的一部分。对于序列参数集或序列参数集扩展或图像参数集的 NAL 单元,nal_ref_idc 不应等于 0。当一个特定的图像的一个条带或条带数据分割 NAL 单元的 nal_ref_idc 等于 0,该图像的所有条带或条带数据划分 NAL 单元都应该等于 0。IDR NAL 单元的 nal_ref_idc 不应等于 0,即 nal_unit_type 等于 5 的 NAL 单元。所有 nal_unit_type 等于 6、9、10、11 或 12 的 NAL 单元其 nal_ref_idc 都应等于 0 6 | 7 | ## nal_unit_type 8 | 9 | ![1563934403914](img/nal_unit_type1.png) 10 | 11 | ![1563934439659](img/nal_unit_type2.png) 12 | 13 | # slice_header 14 | 15 | ## slice_type 16 | 17 | | slice_type | Name of slice_type | 18 | | ---------- | ------------------ | 19 | | 0 | P (P slice) | 20 | | 1 | B (B slice) | 21 | | 2 | I (I slice) | 22 | | 3 | SP (SP slice) | 23 | | 4 | SI (SI slice) | 24 | | 5 | P (P slice) | 25 | | 6 | B (B slice) | 26 | | 7 | I (I slice) | 27 | | 8 | SP (SP slice) | 28 | | 9 | SI (SI slice) | 29 | 30 | When slice_type has a value in the range 5..9, it is a requirement of bitstream conformance that all other slices of the 31 | current coded picture shall have a value of slice_type equal to the current value of slice_type or equal to the current valueof slice_type minus 5.NOTE 1 – Values of slice_type in the range 5..9 can be used by an encoder to indicate that all slices of a picture have the same value of (slice_type % 5). Values of slice_type in the range 5..9 are otherwise equivalent to corresponding values in the range 0..4.When nal_unit_type is equal to 5 (IDR picture), slice_type shall be equal to 2, 4, 7, or 9.When max_num_ref_frames is equal to 0, slice_type shall be equal to 2, 4, 7, or 9. 32 | 33 | ## frame_num 34 | 35 | 图像标识,主要表示当前帧是否作为其他帧的参考帧。 36 | 37 | IDR帧frame_num为0。 38 | 39 | 根据编码顺序,设当前帧作为某一帧的参考帧,则下一帧的frame_num在其基础上加1,否则,保持frame_num不变 40 | 41 | 42 | ## pic_order_cnt_lsb 43 | 44 | 即POC。 45 | 46 | pic_order_cnt_lsb 表示一个编码帧的顶场或一个编码场的图像顺序数对 MaxPicOrderCntLsb 取模。pic_order_cnt_lsb 语法元素的大小是 log2_max_pic_order_cnt_lsb_minus4 + 4 个比特。pic_order_cnt_lsb 的值应该在0 到 MaxPicOrderCntLsb – 1 的范围内(包括边界值) -------------------------------------------------------------------------------- /plugins/h264/img/nal_unit_type1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/compilelife/bitinsight/40d0a7f280211bae979500d49cb5cd35e741c174/plugins/h264/img/nal_unit_type1.png -------------------------------------------------------------------------------- /plugins/h264/img/nal_unit_type2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/compilelife/bitinsight/40d0a7f280211bae979500d49cb5cd35e741c174/plugins/h264/img/nal_unit_type2.png -------------------------------------------------------------------------------- /plugins/h264/main.py: -------------------------------------------------------------------------------- 1 | from table import Table 2 | from bitstream import BitStream 3 | import math 4 | 5 | def more_rbsp_data(bs: BitStream): 6 | n = bs.available() 7 | if n > 8: 8 | return True 9 | elif n <= 0: 10 | return False 11 | 12 | b = bs.read_bits(n, forward=False) 13 | return not b == (1 << (n-1)) 14 | 15 | 16 | def find_start_code(bs: BitStream): 17 | start_code_len = 0 18 | try: 19 | while True: 20 | head = bs.read_bytes_to_array(4) 21 | pos = bs.pos() 22 | 23 | if head[0] == 0 and head[1] == 0 and head[2] == 1: 24 | bs.moveto(pos.byte-1, pos.bit) 25 | start_code_len = 3 26 | break 27 | elif head[0] == 0 and head[1] == 0 and head[2] == 0 and head[3] == 1: 28 | start_code_len = 4 29 | break 30 | else: 31 | bs.moveto(pos.byte-3, pos.bit) 32 | except EOFError as e: 33 | return 0 34 | return start_code_len 35 | 36 | 37 | def rbsp_data(bs: BitStream): 38 | from_byte = bs.pos().byte 39 | n = find_start_code(bs) 40 | to_byte = bs.pos().byte 41 | 42 | num = to_byte - from_byte - n 43 | 44 | bs.moveto(from_byte, 0) 45 | 46 | rbsp = bytearray() 47 | i = 0 48 | while i < num: 49 | if i <= num - 3: 50 | next_3_byte = bs.read_bytes_to_array(3, forward=False) 51 | if next_3_byte[0] == 0 and next_3_byte[1] == 0 and next_3_byte[2] == 3: 52 | rbsp.extend([0, 0]) 53 | bs.skip(3, 0) 54 | i += 3 55 | continue 56 | 57 | rbsp.extend(bs.read_bytes_to_array(1)) 58 | i += 1 59 | 60 | return rbsp 61 | 62 | def __read_topleft_bottomright(d, bs): 63 | d.top_left = [] 64 | d.bottom_right = [] 65 | for i in range(d.num_slice_groups_minus1+1): 66 | d.top_left.append(bs.read_ue_golomb()) 67 | d.bottom_right.append(bs.read_ue_golomb()) 68 | 69 | def pic_parameter_set_rbsp(d:Table, bs:BitStream): 70 | d.add_field('pic_parameter_set_id', bs.read_ue_golomb) 71 | d.add_field('seq_parameter_set_id', bs.read_ue_golomb) 72 | d.add_field('entropy_coding_mode_flag', bs.read_a_bit) 73 | d.add_field('bottom_field_pic_order_in_frame_present_flag', bs.read_a_bit) 74 | d.add_field('num_slice_groups_minus1', bs.read_ue_golomb) 75 | if d.num_slice_groups_minus1 > 0: 76 | d.add_field('slice_group_map_type', bs.read_ue_golomb) 77 | if d.slice_group_map_type == 0: 78 | d.add_field('run_length_minus1', bs.read_ue_golombs, count=(d.num_slice_groups_minus1+1)) 79 | elif d.slice_group_map_type == 2: 80 | d.add_fields(__read_topleft_bottomright, 'top_left', 'bottom_right') 81 | elif d.slice_group_map_type in [3,4,5]: 82 | d.add_field('slice_group_change_direction_flag', bs.read_a_bit) 83 | d.add_field('slice_group_change_rate_minus', bs.read_ue_golomb) 84 | elif d.slice_group_map_type == 6: 85 | d.add_field('pic_size_in_map_units_minus1', bs.read_ue_golomb) 86 | d.add_field('slice_group_id', bs.readbitss, count=math.ceil(math.log2(d.num_slice_groups_minus1+1)), times=(pic_size_in_map_units_minus1+1)) 87 | d.add_field('num_ref_idx_l0_default_active_minus1', bs.read_ue_golomb) 88 | d.add_field('num_ref_idx_l1_default_active_minus1', bs.read_ue_golomb) 89 | d.add_field('weighted_pred_flag', bs.read_a_bit) 90 | d.add_field('weighted_bipred_idc', bs.read_bits, count=2) 91 | d.add_field('pic_init_qp_minus26', bs.read_se_golomb) 92 | d.add_field('pic_init_qs_minus26', bs.read_se_golomb) 93 | d.add_field('chroma_qp_index_offset', bs.read_se_golomb) 94 | d.add_field('deblocking_filter_control_present_flag', bs.read_a_bit) 95 | d.add_field('constrained_intra_pred_flag', bs.read_a_bit) 96 | d.add_field('redundant_pic_cnt_present_flag', bs.read_a_bit) 97 | if more_rbsp_data(bs): 98 | d.add_field('transform_8x8_mode_flag', bs.read_a_bit) 99 | d.add_field('pic_scaling_matrix_present_flag', bs.read_a_bit) 100 | if d.pic_scaling_matrix_present_flag: 101 | d.add_fields(__pps_fields_in_scaling_list, 'pps_scaling_list_present_flag', 'ScalingList4x4', 102 | 'UseDefaultScalingMatrix4x4Flag', 'ScalingList8x8', 'UseDefaultScalingMatrix8x8Flag') 103 | d.add_field('second_chroma_qp_index_offset', bs.read_se_golomb) 104 | rbsp_trailing_bits(d, bs) 105 | 106 | 107 | def nal_unit_header_svc_extension(d:Table, bs:BitStream): 108 | d.add_field('idr_flag priority_id', bs.read_bits, count=1) 109 | d.add_field('no_inter_layer_pred_flag', bs.read_bits, count=6) 110 | d.add_field('dependency_id', bs.read_bits, count=1) 111 | d.add_field('quality_id', bs.read_bits, count=3) 112 | d.add_field('temporal_id', bs.read_bits, count=4) 113 | d.add_field('use_ref_base_pic_flag', bs.read_bits, count=1) 114 | d.add_field('discardable_flag', bs.read_bits, count=1) 115 | d.add_field('output_flag', bs.read_bits, count=1) 116 | d.add_field('reserved_three_2bits', bs.read_bits, count=5) 117 | 118 | def nal_unit_header_3davc_extension(d:Table, bs:BitStream): 119 | d.add_field('view_idx', bs.read_bits, count=8) 120 | d.add_field('depth_flag', bs.read_bits, count=1) 121 | d.add_field('non_idr_flag', bs.read_bits, count=1) 122 | d.add_field('temporal_id', bs.read_bits, count=3) 123 | d.add_field('anchor_pic_flag', bs.read_bits, count=1) 124 | d.add_field('inter_view_flag', bs.read_bits, count=1) 125 | 126 | def nal_unit_header_mvc_extension(d:Table, bs:BitStream): 127 | d.add_field('non_idr_flag', bs.read_bits, count=1) 128 | d.add_field('priority_id', bs.read_bits, count=6) 129 | d.add_field('view_id', bs.read_bits, count=10) 130 | d.add_field('temporal_id', bs.read_bits, count=3) 131 | d.add_field('anchor_pic_flag', bs.read_bits, count=1) 132 | d.add_field('inter_view_flag', bs.read_bits, count=1) 133 | d.add_field('reserved_one_bit', bs.read_bits, count=1) 134 | 135 | def nal_unit(d:Table, bs:BitStream): 136 | d.add_field('forbidden_zero_bit', bs.read_bits, 137 | count=1) 138 | d.add_field('nal_ref_idc', bs.read_bits, count=2) 139 | d.add_field('nal_unit_type', bs.read_bits, count=5) 140 | if d.nal_unit_type in [14,20,21]: 141 | if not d.nal_unit_type == 21: 142 | d.add_field('svc_extension_flag', bs.read_a_bit) 143 | else: 144 | d.add_field('avc_extension_flag', bs.read_a_bit) 145 | if d.svc_extension_flag: 146 | d.add_table(nal_unit_header_svc_extension) 147 | pass 148 | elif d.get_value('avc_extension_flag',0): 149 | d.add_table(nal_unit_header_3davc_extension) 150 | pass 151 | else: 152 | d.add_table(nal_unit_header_mvc_extension) 153 | pass 154 | d.add_field('rbsp', rbsp_data, bs=bs) 155 | 156 | 157 | def rbsp_trailing_bits(d:Table, bs:BitStream): 158 | d.add_field('rbsp_stop_one_bit', bs.read_bits, count=1) 159 | bs.to_byte_end() 160 | 161 | 162 | def access_unit_delimiter_rbsp(d:Table, bs:BitStream): 163 | d.add_field('primary_picture_type', bs.read_bits, count=3) 164 | rbsp_trailing_bits(d,bs) 165 | 166 | 167 | def sei_ff_coding(bs: BitStream): 168 | value = 0 169 | next_byte = 0xFF 170 | while next_byte == 0xFF: 171 | next_byte = bs.read_bits(8) 172 | value += next_byte 173 | return value 174 | 175 | 176 | def sei_message(d:Table, bs:BitStream): 177 | d.add_field('payload_type', sei_ff_coding, bs=bs) 178 | d.add_field('payload_size', sei_ff_coding, bs=bs) 179 | d.add_field('sei_payload', bs.read_bytes_to_array, count=d.payload_size) 180 | 181 | 182 | def sei_rbsp(d:Table, bs:BitStream): 183 | while more_rbsp_data(bs): 184 | d.add_table(sei_message) 185 | rbsp_trailing_bits(d,bs) 186 | 187 | 188 | def __find_pps_in_ctx(id, ctx:Table): 189 | for f in ctx.fields: 190 | if f.name == 'pic_parameter_set_rbsp': 191 | if f.pic_parameter_set_id == id: 192 | return f 193 | return None 194 | 195 | def __find_sps_in_ctx(id, ctx:Table): 196 | for f in ctx.fields: 197 | if f.name == 'seq_parameter_set_rbsp' or f.name == 'subset_seq_parameter_set_rbsp': 198 | if f.seq_parameter_set_data.seq_parameter_set_id == id: 199 | return f.seq_parameter_set_data 200 | return None 201 | 202 | def __read_delta_pic_order_cnt(sps, pps, d, bs): 203 | ret = [] 204 | if sps.pic_order_cnt_type == 1 and not sps.delta_pic_order_always_zero_flag: 205 | ret.append(bs.read_se_golomb) 206 | if pps.bottom_field_pic_order_in_frame_present_flag and not d.get_value('field_pic_flag', 0): 207 | ret.append(bs.read_se_golomb) 208 | return ret 209 | 210 | def ref_pic_list_mvc_modification(d:Table, bs:BitStream): 211 | pass 212 | 213 | def __read_ref_pic_list(d:Table, bs:BitStream): 214 | d.modification_of_pic_num_idc = [] 215 | d.abs_diff_pic_num_minus1 = [] 216 | d.long_term_pic_num = [] 217 | while True: 218 | d.modification_of_pic_num_idc.append(bs.read_se_golomb()) 219 | if d.modification_of_pic_num_idc[-1] in [0,1]: 220 | d.abs_diff_pic_num_minus1.append(bs.read_se_golomb()) 221 | elif d.modification_of_pic_num_idc[-1] == 2: 222 | d.long_term_pic_num.append(bs.read_se_golomb()) 223 | elif d.modification_of_pic_num_idc[-1] == 3: 224 | break 225 | 226 | def ref_pic_list_modification(d:Table, bs:BitStream, type): 227 | if type in [2,4]: #I SI 228 | d.add_field('ref_pic_list_modification_flag_l0', bs.read_a_bit) 229 | if d.ref_pic_list_modification_flag_l0: 230 | d.add_fields(__read_ref_pic_list, 'modification_of_pic_num_idc', 'abs_diff_pic_num_minus1', 'long_term_pic_num') 231 | elif type == 1: #B 232 | d.add_field('ref_pic_list_modification_flag_l1', bs.read_a_bit) 233 | if d.ref_pic_list_modification_flag_l1: 234 | d.add_fields(__read_ref_pic_list, 'modification_of_pic_num_idc', 'abs_diff_pic_num_minus1', 'long_term_pic_num') 235 | 236 | 237 | def slice_header(d:Table, bs:BitStream): 238 | d.add_field('first_mb_in_slice', bs.read_ue_golomb) 239 | d.add_field('slice_type', bs.read_ue_golomb) 240 | d.add_field('pic_parameter_set_id', bs.read_ue_golomb) 241 | pps = __find_pps_in_ctx(d.pic_parameter_set_id, d.context) 242 | if pps == None: 243 | raise Exception('no pps in ctx, abort!') 244 | sps = __find_sps_in_ctx(pps.seq_parameter_set_id, d.context) 245 | if pps == None: 246 | raise Exception('no sps in ctx, abort!') 247 | if sps.get_value('separate_colour_plane_flag', 0): 248 | d.add_field('colour_plane_id', bs.read_bits, count=2) 249 | try: 250 | nal_unit = d.context.nal_unit 251 | except AttributeError as e: 252 | raise Exception('no nal_unit in ctx, abort!') 253 | 254 | d.add_field('frame_num', bs.read_bits, count = (sps.log2_max_frame_num_minus4 + 4)) 255 | if not sps.frame_mbs_only_flag: 256 | d.add_field('field_pic_flag', bs.read_a_bit) 257 | if d.field_pic_flag: 258 | d.add_field('bottom_field_flag', bs.read_a_bit) 259 | 260 | if nal_unit.nal_unit_type == 5: #IdrPicFlag 261 | d.add_field('idr_pic_id', bs.read_ue_golomb) 262 | 263 | if sps.pic_order_cnt_type == 0: 264 | d.add_field('pic_order_cnt_lsb', bs.read_bits, count=(sps.log2_max_pic_order_cnt_lsb_minus4+4)) 265 | if pps.bottom_field_pic_order_in_frame_present_flag and not d.get_value('field_pic_flag', 0): 266 | bs.add_field('delta_pic_order_cnt_bottom', bs.read_se_golomb) 267 | 268 | d.add_field('delta_pic_order_cnt', __read_delta_pic_order_cnt, sps=sps, pps=pps, d=d, bs=bs) 269 | if pps.redundant_pic_cnt_present_flag: 270 | d.add_field('redundant_pic_cnt', bs.read_ue_golomb) 271 | 272 | slice_type = d.slice_type % 5 273 | if slice_type == 1: #B 274 | d.add_field('direct_spatial_mv_pred_flag', bs.read_a_bit) 275 | if slice_type in [0,1,3]: #P SP B 276 | d.add_field('num_ref_idx_active_override_flag', bs.read_a_bit) 277 | if d.num_ref_idx_active_override_flag: 278 | d.add_field('num_ref_idx_l0_active_minus1', bs.read_ue_golomb) 279 | if slice_type == 1: #B 280 | d.add_field('num_ref_idx_l1_active_minus1', bs.read_ue_golomb) 281 | 282 | if nal_unit.nal_unit_type in [20,21]: 283 | d.add_table(ref_pic_list_mvc_modification, type=slice_type) 284 | else: 285 | d.add_table(ref_pic_list_modification, type=slice_type) 286 | 287 | 288 | 289 | def slice_data(d:Table, bs:BitStream): 290 | pass 291 | 292 | 293 | def slice_layer_without_partitioning_rbsp(d:Table, bs:BitStream): 294 | d.add_table(slice_header) 295 | d.add_table(slice_data) 296 | rbsp_trailing_bits(d,bs) 297 | 298 | 299 | def slice_layer_extension_rbsp(d:Table, bs:BitStream): 300 | try: 301 | nal_unit = d.context.nal_unit 302 | except AttributeError as e: 303 | raise Exception('no nal_unit in ctx, abort!') 304 | if nal_unit.get_value('svc_extension_flag', 0): 305 | pass 306 | elif nal_unit.get_value('avc_3d_extension_flag', 0): 307 | pass 308 | else: 309 | d.add_table(slice_header) 310 | d.add_table(slice_data) 311 | rbsp_trailing_bits(d, bs) 312 | 313 | 314 | def scaling_list(ScalingList, bs, UseDefaultScalingMatrix, i): 315 | last_scale = 8 316 | next_scale = 8 317 | size_of_scalinglist = len(ScalingList) 318 | 319 | for j in range(size_of_scalinglist): 320 | if next_scale != 0: 321 | delta_scale = bs.read_se_golomb() 322 | next_scale = (last_scale + delta_scale + 256) % 256 323 | UseDefaultScalingMatrix[i] = (j == 0 and next_scale == 0) 324 | ScalingList[j] = last_scale if next_scale == 0 else next_scale 325 | last_scale = ScalingList[j] 326 | 327 | 328 | def __fields_by_cpb_cnt_minus1(d:Table, bs:BitStream): 329 | d.bit_rate_value_minus1 = [0]*(d.cpb_cnt_minus1+1) 330 | d.cpb_size_value_minus1 = [0]*(d.cpb_cnt_minus1+1) 331 | d.cbr_flag = [0]*(d.cpb_cnt_minus1+1) 332 | for i in range(d.cpb_cnt_minus1+1): 333 | d.bit_rate_value_minus1[i] = bs.read_ue_golomb() 334 | d.cpb_size_value_minus1[i] = bs.read_ue_golomb() 335 | d.cbr_flag[i] = bs.read_a_bit() 336 | 337 | 338 | def hrd_parameters(d:Table, bs:BitStream): 339 | d.add_field('cpb_cnt_minus1', bs.read_ue_golomb) 340 | d.add_field('bit_rate_scale', bs.read_bits, count=4) 341 | d.add_field('cpb_size_scale', bs.read_bits, count=4) 342 | 343 | d.add_fields(__fields_by_cpb_cnt_minus1, 'bit_rate_value_minus1', 344 | 'cpb_size_value_minus1', 'cbr_flag') 345 | 346 | d.add_field('initial_cpb_removal_delay_length_minus1', 347 | bs.read_bits, count=5) 348 | d.add_field('cpb_removal_delay_length_minus1', bs.read_bits, count=5) 349 | d.add_field('dpb_output_delay_length_minus1', bs.read_bits, count=5) 350 | d.add_field('time_offset_length', bs.read_bits, count=5) 351 | 352 | 353 | def vui_parameters(d:Table, bs:BitStream): 354 | d.add_field('aspect_ratio_info_present_flag', bs.read_a_bit) 355 | if d.aspect_ratio_info_present_flag: 356 | d.add_field('aspect_ratio_idc', bs.read_bits, count=8) 357 | if d.aspect_ratio_idc == 255: # Extended_SAR 358 | d.add_field('sar_width', bs.read_bits, count=16) 359 | d.add_field('sar_height', bs.read_bits, count=16) 360 | d.add_field('overscan_info_present_flag', bs.read_a_bit) 361 | if d.overscan_info_present_flag: 362 | d.add_field('overscan_appropriate_flag', bs.read_a_bit) 363 | d.add_field('video_signal_type_present_flag', bs.read_a_bit) 364 | if d.video_signal_type_present_flag: 365 | d.add_field('video_format', bs.read_bits, count=3) 366 | d.add_field('video_full_range_flag', bs.read_a_bit) 367 | d.add_field('colour_description_present_flag', bs.read_a_bit) 368 | if d.colour_description_present_flag: 369 | d.add_field('colour_primaries', bs.read_bits, count=8) 370 | d.add_field('transfer_characteristics', bs.read_bits, count=8) 371 | d.add_field('matrix_coefficients', bs.read_bits, count=8) 372 | d.add_field('chroma_loc_info_present_flag', bs.read_a_bit) 373 | if d.chroma_loc_info_present_flag: 374 | d.add_field('chroma_sample_loc_type_top_field', bs.read_ue_golomb) 375 | d.add_field('chroma_sample_loc_type_bottom_field', bs.read_ue_golomb) 376 | d.add_field('timing_info_present_flag', bs.read_a_bit) 377 | if d.timing_info_present_flag: 378 | d.add_field('num_units_in_tick', bs.read_bits, count=32) 379 | d.add_field('timescale', bs.read_bits, count=32) 380 | d.add_field('fixed_frame_rate_flag', bs.read_a_bit) 381 | d.add_field('nal_hrd_parameters_present_flag', bs.read_a_bit) 382 | if d.nal_hrd_parameters_present_flag: 383 | d.add_table(hrd_parameters) 384 | d.add_field('vcl_hrd_parameters_present_flag', bs.read_a_bit) 385 | if d.vcl_hrd_parameters_present_flag: 386 | d.add_table(hrd_parameters) 387 | if d.nal_hrd_parameters_present_flag or d.vcl_hrd_parameters_present_flag: 388 | d.add_field('low_delay_hrd_flag', bs.read_a_bit) 389 | d.add_field('pic_struct_present_flag', bs.read_a_bit) 390 | d.add_field('bitstream_restriction_flag', bs.read_a_bit) 391 | if d.bitstream_restriction_flag: 392 | d.add_field('motion_vectors_over_pic_boundaries_flag', bs.read_a_bit) 393 | d.add_field('max_bytes_per_pic_denom', bs.read_ue_golomb) 394 | d.add_field('max_bits_per_mb_denom', bs.read_ue_golomb) 395 | d.add_field('log2_max_mv_length_horizontal', bs.read_ue_golomb) 396 | d.add_field('log2_max_mv_length_vertical', bs.read_ue_golomb) 397 | d.add_field('max_num_reorder_frames', bs.read_ue_golomb) 398 | d.add_field('max_dec_frame_buffering', bs.read_ue_golomb) 399 | 400 | 401 | def __pps_fields_in_scaling_list(d:Table, bs:BitStream): 402 | sps = __find_sps_in_ctx(d.seq_parameter_set_id, d.context) 403 | num = 8 if sps.chroma_format_idc != 3 else 12 404 | d.pic_scaling_list_present_flag = [0]*num 405 | d.ScalingList4x4 = [[0]*16 for i in range(6)] 406 | d.UseDefaultScalingMatrix4x4Flag = [False]*6 407 | d.ScalingList8x8 = [[0]*64 for i in range(6)] 408 | d.UseDefaultScalingMatrix8x8Flag = [False]*6 409 | for i in range(num): 410 | d.pic_scaling_list_present_flag[i] = bs.read_bits(1) 411 | if d.pic_scaling_list_present_flag[i]: 412 | if i < 6: 413 | scaling_list( 414 | d.ScalingList4x4[i], bs, d.UseDefaultScalingMatrix4x4Flag, i) 415 | else: 416 | scaling_list( 417 | d.ScalingList8x8[i-6], bs, d.UseDefaultScalingMatrix8x8Flag, i-6) 418 | 419 | def __fields_in_scaling_list(d:Table, bs:BitStream): 420 | num = 8 if d.chroma_format_idc != 3 else 12 421 | d.seq_scaling_list_present_flag = [0]*num 422 | d.ScalingList4x4 = [[0]*16 for i in range(6)] 423 | d.UseDefaultScalingMatrix4x4Flag = [False]*6 424 | d.ScalingList8x8 = [[0]*64 for i in range(6)] 425 | d.UseDefaultScalingMatrix8x8Flag = [False]*6 426 | for i in range(num): 427 | d.seq_scaling_list_present_flag[i] = bs.read_bits(1) 428 | if d.seq_scaling_list_present_flag[i]: 429 | if i < 6: 430 | scaling_list( 431 | d.ScalingList4x4[i], bs, d.UseDefaultScalingMatrix4x4Flag, i) 432 | else: 433 | scaling_list( 434 | d.ScalingList8x8[i-6], bs, d.UseDefaultScalingMatrix8x8Flag, i-6) 435 | 436 | 437 | def seq_parameter_set_data(d:Table, bs:BitStream): 438 | d.add_field('profile_idc', bs.read_bits, count=8) 439 | d.add_field('constraint_set0_flag', bs.read_bits, count=1) 440 | d.add_field('constraint_set1_flag', bs.read_bits, count=1) 441 | d.add_field('constraint_set2_flag', bs.read_bits, count=1) 442 | d.add_field('constraint_set3_flag', bs.read_bits, count=1) 443 | d.add_field('constraint_set4_flag', bs.read_bits, count=1) 444 | d.add_field('constraint_set5_flag', bs.read_bits, count=1) 445 | bs.to_byte_end() # reserved_zero_2bits 446 | d.add_field('level_idc', bs.read_bits, count=8) 447 | d.add_field('seq_parameter_set_id', bs.read_ue_golomb) 448 | if d.profile_idc in [100, 110, 122, 244, 44, 83, 86, 118, 128, 138, 139, 134, 135]: 449 | d.add_field('chroma_format_idc', bs.read_ue_golomb) 450 | if d.chroma_format_idc == 3: 451 | d.add_field('separate_colour_plane_flag', bs.read_bits, count=1) 452 | d.add_field('bit_depth_luma_minus8', bs.read_ue_golomb) 453 | d.add_field('bit_depth_chroma_minus', bs.read_ue_golomb) 454 | d.add_field('qpprime_y_zero_transform_bypass_flag', 455 | bs.read_bits, count=1) 456 | d.add_field('seq_scaling_matrix_present_flag', bs.read_bits, count=1) 457 | if d.seq_scaling_matrix_present_flag: # todo test this branch 458 | d.add_fields(__fields_in_scaling_list, 'seq_scaling_list_present_flag', 'ScalingList4x4', 459 | 'UseDefaultScalingMatrix4x4Flag', 'ScalingList8x8', 'UseDefaultScalingMatrix8x8Flag') 460 | d.add_field('log2_max_frame_num_minus4', bs.read_ue_golomb) 461 | d.add_field('pic_order_cnt_type', bs.read_ue_golomb) 462 | if d.pic_order_cnt_type == 0: 463 | d.add_field('log2_max_pic_order_cnt_lsb_minus4', bs.read_ue_golomb) 464 | elif d.pic_order_cnt_type == 1: 465 | d.add_field('delta_pic_order_always_zero_flag', bs.read_bits, count=1) 466 | d.add_field('offset_for_non_ref_pic', bs.read_se_golomb) 467 | d.add_field('offset_for_top_to_bottom_field', bs.read_se_golomb) 468 | d.add_field('num_ref_frames_in_pic_order_cnt_cycle', bs.read_ue_golomb) 469 | d.add_field('offset_for_ref_fram', bs.read_se_golombs, 470 | count=d.num_ref_frames_in_pic_order_cnt_cycle) 471 | d.add_field('max_num_ref_frames', bs.read_ue_golomb) 472 | d.add_field('gaps_in_frame_num_value_allowed_flag', bs.read_bits, count=1) 473 | d.add_field('pic_width_in_mbs_minus1', bs.read_ue_golomb) 474 | d.add_field('pic_height_in_map_units_minus1', bs.read_ue_golomb) 475 | d.add_field('frame_mbs_only_flag', bs.read_bits, count=1) 476 | if d.frame_mbs_only_flag == 0: 477 | d.add_field('mb_adaptive_frame_field_flag', bs.read_bits, count=1) 478 | d.add_field('direct_8x8_inference_flag', bs.read_bits, count=1) 479 | d.add_field('frame_cropping_flag', bs.read_bits, count=1) 480 | if d.frame_cropping_flag == 1: 481 | d.add_field('frame_crop_left_offset', bs.read_ue_golomb) 482 | d.add_field('frame_crop_right_offset', bs.read_ue_golomb) 483 | d.add_field('frame_crop_top_offset', bs.read_ue_golomb) 484 | d.add_field('frame_crop_bottom_offset', bs.read_ue_golomb) 485 | d.add_field('vui_parameters_present_flag', bs.read_bits, count=1) 486 | if d.vui_parameters_present_flag: 487 | d.add_table(vui_parameters) 488 | 489 | 490 | def seq_parameter_set_rbsp(d:Table, bs:BitStream): 491 | d.add_table(seq_parameter_set_data) 492 | rbsp_trailing_bits(d,bs) 493 | 494 | 495 | def seq_parameter_set_svc_extension(d:Table, bs:BitStream): 496 | pass 497 | 498 | 499 | def svc_vui_parameters_extension(d:Table, bs:BitStream): 500 | pass 501 | 502 | 503 | def seq_parameter_set_mvc_extension(d:Table, bs:BitStream): 504 | 505 | pass 506 | 507 | 508 | def mvc_vui_parameters_extension(d:Table, bs:BitStream): 509 | pass 510 | 511 | 512 | def seq_parameter_set_mvcd_extension(d:Table, bs:BitStream): 513 | pass 514 | 515 | 516 | def seq_parameter_set_3davc_extension(d:Table, bs:BitStream): 517 | pass 518 | 519 | 520 | def subset_seq_parameter_set_rbsp(d:Table, bs:BitStream): 521 | d.add_table(seq_parameter_set_data) 522 | # rbsp_trailing_bits(d,bs) 523 | 524 | 525 | #============================================================= 526 | # 以下是在H264标准基础上,根据自己的需求定义的处理过程 527 | # 为了方便区分,均以analyse_开头定义(这不是强制的) 528 | # 一般分析函数都会被循环调用,所以要保证每次调用分析函数时流位置刚好在包头,或者可以自动定位到包头 529 | #============================================================= 530 | def analyse_nalu_header(d:Table, bs:BitStream): 531 | find_start_code(bs) 532 | d.add_table(nal_unit) 533 | 534 | def default_parser(d:Table, bs:BitStream): 535 | analyse_nalu_header(d, bs) 536 | rbsp_bs = BitStream(d.nal_unit.rbsp) 537 | 538 | d.context.add_exist(d.nal_unit) 539 | 540 | if d.nal_unit.nal_unit_type == 9: 541 | d.add_table(access_unit_delimiter_rbsp, bs=rbsp_bs) 542 | elif d.nal_unit.nal_unit_type == 6: 543 | d.add_table(sei_rbsp, bs=rbsp_bs) 544 | elif d.nal_unit.nal_unit_type in [1, 5]: 545 | d.add_table(slice_layer_without_partitioning_rbsp, bs=rbsp_bs) 546 | elif d.nal_unit.nal_unit_type == 7: 547 | d.add_table(seq_parameter_set_rbsp, bs=rbsp_bs) 548 | elif d.nal_unit.nal_unit_type == 8: 549 | d.add_table(pic_parameter_set_rbsp, bs=rbsp_bs) 550 | elif d.nal_unit.nal_unit_type == 15: 551 | d.add_table(subset_seq_parameter_set_rbsp, bs=rbsp_bs) 552 | elif d.nal_unit.nal_unit_type == 20: 553 | d.add_table(slice_layer_extension_rbsp, bs=rbsp_bs) 554 | 555 | 556 | def analyse_slice_header(d:Table, bs:BitStream): 557 | analyse_nalu_header(d, bs) 558 | type = d.nal_unit.nal_unit_type 559 | if type in [1,5,20]: 560 | d.context.add_exist(d.nal_unit) 561 | d.add_table(slice_header, BitStream(d.nal_unit.rbsp)) -------------------------------------------------------------------------------- /plugins/ts/doc.md: -------------------------------------------------------------------------------- 1 | # program_association_section 2 | 3 | The Program Association Table provides the correspondence between a program_number and the PID value of the Transport Stream packets which carry the program definition. The program_number is the numeric label associated with a program. 4 | 5 | ## table_id 6 | 7 | The table_id field identifies the content of a Transport Stream PSI section as shown in table 2-27 below. 8 | 9 | | value | description | 10 | | :-------- | :------------------------------------------- | 11 | | 0x00 | program_association_section | 12 | | 0x01 | conditional_access_section(CA_section) | 13 | | 0x02 | TS_program_map_section | 14 | | 0x03-0x3F | ITU-T Rec. H.222.0 \| ISO/IEC 13818 reserved | 15 | | 0x40-0xFE | User private | 16 | | 0xFF | forbidden | -------------------------------------------------------------------------------- /plugins/ts/main.py: -------------------------------------------------------------------------------- 1 | from table import Table 2 | from bitstream import BitStream 3 | 4 | def program_association_section(d:Table, bs:BitStream): 5 | d.add_field('table_id', bs.read_bits, count=8) 6 | d.add_field('section_syntax_indicator', bs.read_bits, count=1) 7 | d.add_field('0', bs.read_bits, count=1) 8 | d.add_field('reserved', bs.read_bits, count=2) 9 | d.add_field('section_length', bs.read_bits, count=12) 10 | d.add_field('transport_stream_id', bs.read_bits, count=16) 11 | d.add_field('reserved', bs.read_bits, count=2) 12 | 13 | 14 | def transport_packet(d, bs): 15 | d.add_field('sync_byte', bs.read_bits, count=8) 16 | d.add_field('transport_error_indicator', bs.read_bits, count=1) 17 | d.add_field('payload_unit_start_indicator', bs.read_bits, count=1) 18 | d.add_field('transport_priority', bs.read_bits, count=1) 19 | d.add_field('PID', bs.read_bits, count=13) 20 | d.add_field('transport_scrambling_control', bs.read_bits, count=2) 21 | d.add_field('adaptation_field_control', bs.read_bits, count=2) 22 | d.add_field('continuity_counter', bs.read_bits, count=4) 23 | if d.PID == 0: 24 | d.add_table(program_association_section) -------------------------------------------------------------------------------- /plugins/wav/doc.md: -------------------------------------------------------------------------------- 1 | # header 2 | 3 | ![fmt](img/wav-sound-format.gif) 4 | 5 | ## ChunkID 6 | 7 | Contains the letters "RIFF" in ASCII form 8 | 9 | ## ChunkSize 10 | 11 | 36 + SubChunk2Size, or more precisely: 12 | 13 | 4 + (8 + SubChunk1Size) + (8 + SubChunk2Size) 14 | 15 | This is the size of the rest of the chunk following this number. This is the size of the entire file in bytes minus 8 bytes for the two fields not included in this count: 16 | ChunkID and ChunkSize. 17 | 18 | ## Format 19 | 20 | Contains the letters "WAVE" 21 | 22 | ## Subchunk1ID 23 | 24 | Contains the letters "fmt " 25 | 26 | ## Subchunk1Size 27 | 28 | 16 for PCM. This is the size of the rest of the Subchunk which follows this number. 29 | 30 | ## AudioFormat 31 | 32 | PCM = 1. Values other than 1 indicate some form of compression. 33 | 34 | ## NumChannels 35 | 36 | Mono = 1, Stereo = 2, etc 37 | 38 | ## SampleRate 39 | 40 | 8000, 44100, etc. 41 | 42 | ## ByteRate 43 | 44 | == SampleRate * NumChannels * BitsPerSample/8 45 | 46 | ## BlockAlign 47 | 48 | == NumChannels * BitsPerSample/8 49 | 50 | ## BitsPerSample 51 | 52 | 8 bits = 8, 16 bits = 16, etc. 53 | 54 | ## Subchunk2ID 55 | 56 | Contains the letters "data" 57 | 58 | ## Subchunk2Size 59 | 60 | == NumSamples * NumChannels * BitsPerSample/8 61 | 62 | This is the number of bytes in the data. 63 | 64 | You can also think of this as the size of the read of the subchunk following this number. -------------------------------------------------------------------------------- /plugins/wav/img/wav-sound-format.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/compilelife/bitinsight/40d0a7f280211bae979500d49cb5cd35e741c174/plugins/wav/img/wav-sound-format.gif -------------------------------------------------------------------------------- /plugins/wav/main.py: -------------------------------------------------------------------------------- 1 | from table import Table 2 | from bitstream import BitStream 3 | 4 | def header(t:Table, bs:BitStream): 5 | bs.bigendien = False 6 | t.add_field('ChunkID', bs.read_bytes_to_str, count=4) 7 | t.add_field('ChunkSize', bs.read_bytes, count=4) 8 | t.add_field('Format', bs.read_bytes_to_str, count=4) 9 | t.add_field('Subchunk1ID', bs.read_bytes_to_str, count=4) 10 | t.add_field('Subchunk1Size', bs.read_bytes, count=4) 11 | t.add_field('AudioFormat', bs.read_bytes, count=2) 12 | t.add_field('NumChannels', bs.read_bytes, count=2) 13 | t.add_field('SampleRate', bs.read_bytes, count=4) 14 | t.add_field('ByteRate', bs.read_bytes, count=4) 15 | t.add_field('BlockAlign', bs.read_bytes, count=2) 16 | t.add_field('BitsPerSample', bs.read_bytes, count=2) 17 | t.add_field('Subchunk2ID', bs.read_bytes_to_str, count=4) 18 | t.add_field('Subchunk2Size', bs.read_bytes, count=4) 19 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | altgraph==0.16.1 2 | anytree==2.6.0 3 | appdirs==1.4.3 4 | application-utility==1.1.3 5 | autopep8==1.4.4 6 | bandit==1.6.2 7 | bitstream==2.5.4 8 | btrfsutil==1.1.1 9 | CacheControl==0.12.5 10 | catfish==1.4.8 11 | chardet==3.0.4 12 | colorama==0.4.1 13 | cupshelpers==1.0 14 | distlib==0.2.9 15 | distro==1.4.0 16 | docopt==0.6.2 17 | gitdb2==2.0.5 18 | GitPython==2.1.11 19 | graphviz==0.11.1 20 | gufw==18.10.0 21 | html5lib==1.0.1 22 | idna==2.8 23 | Jade-Application-Kit==2.0.9 24 | keyutils==0.5 25 | lensfun==0.3.2 26 | lightdm-gtk-greeter-settings==1.2.2 27 | lockfile==0.12.2 28 | louis==3.10.0 29 | lxml==4.4.0 30 | markdown2==2.3.8 31 | menulibre==2.2.0 32 | msgpack==0.6.1 33 | mugshot==0.4.1 34 | npyscreen==4.10.5 35 | numpy==1.16.4 36 | objgraph==3.4.1 37 | packaging==19.0 38 | pacman-mirrors==4.14.2 39 | pbr==5.4.1 40 | pep517==0.5.0 41 | pexpect==4.7.0 42 | Pillow==6.2.0 43 | progress==1.5 44 | psutil==5.6.3 45 | ptyprocess==0.6.0 46 | pycairo==1.18.1 47 | pycodestyle==2.5.0 48 | pycups==1.9.74 49 | pycurl==7.43.0.3 50 | Pygments==2.4.2 51 | PyGObject==3.32.2 52 | PyInstaller==3.5 53 | pyparsing==2.4.0 54 | PyQt5==5.13.0 55 | PyQt5-sip==4.19.18 56 | PySide2==5.12.4a1.dev1563353628 57 | python-distutils-extra==2.39 58 | pytoml==0.1.21 59 | pyxdg==0.26 60 | PyYAML==5.1.1 61 | reportlab==3.5.23 62 | requests==2.22.0 63 | retrying==1.3.3 64 | rope==0.14.0 65 | shiboken2==5.12.4a1.dev1563353628 66 | six==1.12.0 67 | smmap2==2.0.5 68 | stevedore==1.30.1 69 | team==1.0 70 | udiskie==1.7.7 71 | ufw==0.36 72 | urllib3==1.25.3 73 | webencodings==0.5.1 74 | -------------------------------------------------------------------------------- /table.py: -------------------------------------------------------------------------------- 1 | from bitstream import * 2 | from copy import copy 3 | 4 | class Field: 5 | def __init__(self, name, bs): 6 | self.bs = bs 7 | self.name = name 8 | self.begin = BitPos(-1,-1) 9 | self.end = BitPos(-1,-1) 10 | self.value = None 11 | 12 | def __str__(self): 13 | return "%s\t%s=>%s"%(self.name, str(self.begin), str(self.end)) 14 | 15 | class Table(Field): 16 | def __init__(self, name, context, bs: BitStream): 17 | super(Table, self).__init__(name, bs) 18 | self.fields = [] 19 | self.value = self 20 | self.context = context 21 | 22 | def remove(self, name): 23 | delattr(self, name) 24 | for i in range(len(self.fields)): 25 | if self.fields[i].name == name: 26 | del self.fields[i] 27 | break 28 | 29 | def add_exist(self, field: Field): 30 | self.fields.append(field) 31 | setattr(self, field.name, field.value) 32 | 33 | def add_table(self, func, **kwargs): 34 | bs = self.bs 35 | if kwargs.__contains__('bs'): 36 | bs = kwargs['bs'] 37 | del kwargs['bs'] 38 | 39 | table = Table(func.__name__, self.context, bs) 40 | setattr(self, func.__name__, table) 41 | self.fields.append(table) 42 | 43 | table.begin = bs.pos() 44 | func(table, bs, **kwargs) 45 | table.end = bs.pos() 46 | 47 | 48 | def add_field(self, name, func, **kwargs): 49 | field = Field(name, self.bs) 50 | 51 | field.begin = self.bs.pos() 52 | field.value = func(**kwargs) 53 | field.end = self.bs.pos() 54 | 55 | setattr(self, name, field.value) 56 | self.fields.append(field) 57 | 58 | 59 | def add_fields(self, func, *names, **kwargs): 60 | fields = [] 61 | for name in names: 62 | field = Field(name, self.bs) 63 | field.begin = self.bs.pos() 64 | setattr(self, name, None) 65 | self.fields.append(field) 66 | fields.append(field) 67 | 68 | func(self, self.bs, **kwargs) 69 | 70 | for field in fields: 71 | field.end = self.bs.pos() 72 | 73 | def get_value(self, name, def_value): 74 | if hasattr(self, name): 75 | return getattr(self, name) 76 | else: 77 | return def_value 78 | 79 | def get_value_by_path(self, path, def_value='Unknown'): 80 | field = self 81 | names = path.split('.') 82 | for name in names: 83 | try: 84 | field = getattr(field, name) 85 | except AttributeError as e: 86 | field = def_value 87 | break; 88 | 89 | return field -------------------------------------------------------------------------------- /viewdata.py: -------------------------------------------------------------------------------- 1 | from PySide2 import QtCore, QtWidgets, QtGui 2 | from table import BitPos 3 | from bitstream import * 4 | import mmap 5 | import weakref 6 | import platform 7 | 8 | class DataViewer: 9 | def __init__(self, editor): 10 | self.editor = editor 11 | 12 | def to_data_pos(self, cursor_pos): 13 | pass 14 | 15 | def moveto(self, pos:BitPos): 16 | pass 17 | 18 | def highlight(self,begin: BitPos,end: BitPos): 19 | pass 20 | 21 | def clear_higlight(self): 22 | self.editor.setExtraSelections([]) 23 | 24 | class ByteViewer(DataViewer): 25 | def __init__(self, editor:QtWidgets.QPlainTextEdit): 26 | super(ByteViewer, self).__init__(editor) 27 | 28 | def __to_cursor_pos(self, pos:BitPos): 29 | return pos.byte * 3 + (1 if pos.bit >= 4 else 0) 30 | 31 | def to_data_pos(self, cursor_pos): 32 | byte = int(cursor_pos / 3) 33 | bit = cursor_pos % 3 34 | if bit == 1: 35 | bit = 4 36 | elif bit == 2: #空格处处理为下一个字节 37 | bit = 0 38 | byte += 1 39 | return BitPos(byte, bit) 40 | 41 | def moveto(self, pos:BitPos): 42 | cursor = self.editor.textCursor() 43 | cursor.setPosition(self.__to_cursor_pos(pos)) 44 | self.editor.setTextCursor(cursor) 45 | 46 | def highlight(self,begin: BitPos,end: BitPos): 47 | selection = QtWidgets.QTextEdit.ExtraSelection() 48 | selection.cursor = self.editor.textCursor() 49 | selection.cursor.setPosition(self.__to_cursor_pos(begin)) 50 | selection.cursor.setPosition(self.__to_cursor_pos(end) + (0 if end.bit in [0,4] else 1), QtGui.QTextCursor.KeepAnchor) 51 | selection.format.setBackground(QtGui.QColor(0,255,0)) 52 | self.editor.setExtraSelections([selection]) 53 | 54 | def load(self,data): 55 | self.editor.clear() 56 | bytestr = [] 57 | for b in data: 58 | bytestr.append(format(b, '02x')) 59 | self.editor.setPlainText(' '.join(bytestr)+' ') 60 | 61 | class BitViewer(DataViewer): 62 | def __init__(self, editor:QtWidgets.QPlainTextEdit): 63 | super(BitViewer, self).__init__(editor) 64 | 65 | def __to_cursor_pos(self, pos:BitPos): 66 | return pos.byte * 9 + pos.bit 67 | 68 | def to_data_pos(self, cursor_pos): 69 | byte = int(cursor_pos / 9) 70 | bit = cursor_pos % 9 71 | if bit == 8: #空格位置处理为下一个字节开始 72 | byte += 1 73 | bit = 0 74 | return BitPos(byte, bit) 75 | 76 | def moveto(self, pos:BitPos): 77 | cursor = self.editor.textCursor() 78 | cursor.setPosition(self.__to_cursor_pos(pos)) 79 | self.editor.setTextCursor(cursor) 80 | 81 | def highlight(self,begin: BitPos,end: BitPos): 82 | print("highlight %s=>%s "%(str(begin),str(end))) 83 | selection = QtWidgets.QTextEdit.ExtraSelection() 84 | selection.cursor = self.editor.textCursor() 85 | selection.cursor.setPosition(self.__to_cursor_pos(begin)) 86 | selection.cursor.setPosition(self.__to_cursor_pos(end), QtGui.QTextCursor.KeepAnchor) 87 | selection.format.setBackground(QtGui.QColor(0,255,0)) 88 | self.editor.setExtraSelections([selection]) 89 | 90 | def load(self,data): 91 | self.editor.clear() 92 | bitstr = [] 93 | for b in data: 94 | bitstr.append(format(b, '08b')) 95 | self.editor.setPlainText(' '.join(bitstr)+' ') 96 | 97 | class MemoryViewer(DataViewer): 98 | def __init__(self, mem): 99 | super(MemoryViewer, self).__init__(DataViewerEditor(self)) 100 | self._highlight = [] 101 | self._impl = ByteViewer(self.editor) 102 | self._bitstream = BitStream(mem) 103 | self.valid_range = [0, len(mem)] # end pos not include 104 | self.mem = mem 105 | self.limit_range(0, 1024) 106 | 107 | def get_pos(self): 108 | return self._from_impl_pos(self._impl.to_data_pos(self.editor.textCursor().position())) 109 | 110 | def is_bit_mode(self): 111 | return type(self._impl) == BitViewer 112 | 113 | def _to_impl_pos(self, pos): 114 | return BitPos(pos.byte - self.valid_range[0], pos.bit) 115 | 116 | def _from_impl_pos(self, pos): 117 | return BitPos(pos.byte + self.valid_range[0], pos.bit) 118 | 119 | def set_bit_mode(self,bit = False): 120 | if self.is_bit_mode() == bit: 121 | return 122 | 123 | pos = self.get_pos() 124 | self._impl = BitViewer(self.editor) if bit else ByteViewer(self.editor) 125 | 126 | self.__load_to_impl() 127 | self._impl.moveto(self._to_impl_pos(pos)) 128 | if len(self._highlight) > 0: 129 | self._impl.highlight(self._to_impl_pos(self._highlight[0]), self._to_impl_pos(self._highlight[1])) 130 | 131 | def moveto(self, pos:BitPos): 132 | self._impl.moveto(self._to_impl_pos(pos)) 133 | 134 | def highlight(self, begin:BitPos, end:BitPos): 135 | self._highlight = [begin, end] 136 | self._impl.highlight(self._to_impl_pos(begin), self._to_impl_pos(end)) 137 | 138 | def get_bitstream(self): 139 | cursor = self.editor.textCursor() 140 | begin = cursor.selectionStart() 141 | pos = self._from_impl_pos(self._impl.to_data_pos(begin)) 142 | self._bitstream.moveto(pos.byte, pos.bit) 143 | if cursor.hasSelection(): 144 | end = cursor.selectionEnd() 145 | pos = self._from_impl_pos(self._impl.to_data_pos(end)) 146 | self._bitstream.set_end_pos(pos.byte, pos.bit) 147 | else: 148 | self._bitstream.set_end_pos(self.valid_range[1], 0) 149 | 150 | return self._bitstream 151 | 152 | def __load_to_impl(self): 153 | self._impl.load(self.mem[self.valid_range[0]:self.valid_range[1]]) 154 | 155 | def limit_range(self, from_include, to_exclude): 156 | if to_exclude > len(self.mem): 157 | to_exclude = len(self.mem) 158 | if from_include < 0: 159 | from_include = 0 160 | 161 | self.valid_range = [from_include, to_exclude] 162 | self.__load_to_impl() 163 | 164 | class FileViewer(MemoryViewer): 165 | def __init__(self, path): 166 | with open(path, 'rb') as f: 167 | mem = mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ) 168 | super(FileViewer, self).__init__(mem) 169 | 170 | def __del__(self): 171 | self.mem.close() 172 | 173 | class DataNav(QtWidgets.QWidget): 174 | def __init__(self): 175 | super().__init__() 176 | self.viewer = None 177 | 178 | scope_layout = QtWidgets.QHBoxLayout() 179 | self.begin = QtWidgets.QLineEdit() 180 | scope_layout.addWidget(self.begin) 181 | scope_layout.addWidget(QtWidgets.QLabel('-')) 182 | self.end = QtWidgets.QLineEdit() 183 | scope_layout.addWidget(self.end) 184 | btn = QtWidgets.QPushButton('加载') 185 | btn.clicked.connect(self.__load) 186 | scope_layout.addWidget(btn) 187 | 188 | nav_layout = QtWidgets.QVBoxLayout() 189 | self.curpos = QtWidgets.QLabel('当前位置: ') 190 | nav_layout.addWidget(self.curpos) 191 | self.size = QtWidgets.QLabel('大小: ') 192 | nav_layout.addWidget(self.size) 193 | nav_layout.addLayout(scope_layout) 194 | 195 | self.setLayout(nav_layout) 196 | 197 | def __load(self): 198 | if self.viewer == None: 199 | return 200 | 201 | begin = int(self.begin.text()) 202 | if self.end.text() != '': 203 | end = int(self.end.text()) 204 | else: 205 | end = begin+1024 206 | 207 | self.viewer.limit_range(begin, end) 208 | 209 | self.__update_pos() 210 | self.__update_size() 211 | 212 | def __update_size(self): 213 | self.size.setText('大小: [%d => %d) / %d'%(self.viewer.valid_range[0], self.viewer.valid_range[1], len(self.viewer.mem))) 214 | 215 | def __update_pos(self): 216 | pos = self.viewer.get_pos() 217 | self.curpos.setText('当前位置: '+str(pos)) 218 | 219 | def set_data_viewer(self,viewer): 220 | self.viewer = viewer 221 | self.viewer.editor.cursorPositionChanged.connect(self.__update_pos) 222 | self.viewer.editor.cursorPositionChanged.connect(self.__update_size) 223 | self.__update_pos() 224 | self.__update_size() 225 | 226 | class DataViewerEditor(QtWidgets.QPlainTextEdit): 227 | analyse_requested = QtCore.Signal(BitStream) 228 | default_analyse_requested = QtCore.Signal(BitStream) 229 | 230 | def __init__(self, viewer: MemoryViewer): 231 | super(DataViewerEditor, self).__init__(); 232 | 233 | family = 'monospace' 234 | if platform.system() == 'Darwin': 235 | family = 'Menlo' 236 | 237 | self.setFont(QtGui.QFont(family)) 238 | self.__viewer = weakref.ref(viewer) 239 | self.setCursorWidth(6) 240 | 241 | self.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu) 242 | self.__actions = [] 243 | self.__add_action('执行默认解析器', 'Ctrl+D', self.default_analyse) 244 | self.__add_action('解析...', 'Ctrl+P', self.analyse) 245 | self.__add_action('查找...', 'Ctrl+F', self.search) 246 | self.__add_action('切换二进制/十六机制', 'Ctrl+M', self.switch_mode) 247 | 248 | def __add_action(self, name, key, proc): 249 | action = QtWidgets.QAction(name) 250 | action.setShortcut(QtGui.QKeySequence(key)) 251 | action.triggered.connect(proc) 252 | self.__actions.append(action) 253 | self.addAction(action) 254 | 255 | def search(self): 256 | (text, ok) = QtWidgets.QInputDialog.getText(self, '请输入', '要搜索的字符串') 257 | if ok: 258 | self.find(text) 259 | 260 | def set_default_parser(self,name): 261 | self.__actions[0].setText('执行'+name) 262 | 263 | def default_analyse(self): 264 | self.default_analyse_requested.emit(self.__viewer().get_bitstream()) 265 | 266 | def analyse(self): 267 | self.analyse_requested.emit(self.__viewer().get_bitstream()) 268 | 269 | def switch_mode(self): 270 | self.__viewer().set_bit_mode(not self.__viewer().is_bit_mode()) 271 | --------------------------------------------------------------------------------