You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

544 lines
21KB

  1. \input texinfo @c -*- texinfo -*-
  2. @settitle Developer Documentation
  3. @titlepage
  4. @center @titlefont{Developer Documentation}
  5. @end titlepage
  6. @top
  7. @contents
  8. @chapter Developers Guide
  9. @section API
  10. @itemize @bullet
  11. @item libavcodec is the library containing the codecs (both encoding and
  12. decoding). Look at @file{libavcodec/apiexample.c} to see how to use it.
  13. @item libavformat is the library containing the file format handling (mux and
  14. demux code for several formats). Look at @file{avplay.c} to use it in a
  15. player. See @file{libavformat/output-example.c} to use it to generate
  16. audio or video streams.
  17. @end itemize
  18. @section Integrating libav in your program
  19. Shared libraries should be used whenever is possible in order to reduce
  20. the effort distributors have to pour to support programs and to ensure
  21. only the public API is used.
  22. You can use Libav in your commercial program, but you must abide to the
  23. license, LGPL or GPL depending on the specific features used, please refer
  24. to @uref{http://libav.org/legal.html, our legal page} for a quick checklist and to
  25. the following links for the exact text of each license:
  26. @uref{http://git.libav.org/?p=libav.git;a=blob;f=COPYING.GPLv2, GPL version 2},
  27. @uref{http://git.libav.org/?p=libav.git;a=blob;f=COPYING.GPLv3, GPL version 3},
  28. @uref{http://git.libav.org/?p=libav.git;a=blob;f=COPYING.LGPLv2.1, LGPL version 2.1},
  29. @uref{http://git.libav.org/?p=libav.git;a=blob;f=COPYING.LGPLv3, LGPL version 3}.
  30. Any modification to the source code can be suggested for inclusion.
  31. The best way to proceed is to send your patches to the
  32. @uref{https://lists.libav.org/mailman/listinfo/libav-devel, libav-devel}
  33. mailing list.
  34. @anchor{Coding Rules}
  35. @section Coding Rules
  36. @subsection Code formatting conventions
  37. The code is written in K&R C style. That means the following:
  38. @itemize @bullet
  39. @item
  40. The control statements are formatted by putting space between the statement
  41. and parenthesis in the following way:
  42. @example
  43. for (i = 0; i < filter->input_count; i++) @{
  44. @end example
  45. @item
  46. The case statement is always located at the same level as the switch itself:
  47. @example
  48. switch (link->init_state) @{
  49. case AVLINK_INIT:
  50. continue;
  51. case AVLINK_STARTINIT:
  52. av_log(filter, AV_LOG_INFO, "circular filter chain detected");
  53. return 0;
  54. @end example
  55. @item
  56. Braces in function declarations are written on the new line:
  57. @example
  58. const char *avfilter_configuration(void)
  59. @{
  60. return LIBAV_CONFIGURATION;
  61. @}
  62. @end example
  63. @item
  64. Do not check for NULL values by comparison, @samp{if (p)} and
  65. @samp{if (!p)} are correct; @samp{if (p == NULL)} and @samp{if (p != NULL)}
  66. are not.
  67. @item
  68. In case of a single-statement if, no curly braces are required:
  69. @example
  70. if (!pic || !picref)
  71. goto fail;
  72. @end example
  73. @item
  74. Do not put spaces immediately inside parentheses. @samp{if (ret)} is
  75. a valid style; @samp{if ( ret )} is not.
  76. @end itemize
  77. There are the following guidelines regarding the indentation in files:
  78. @itemize @bullet
  79. @item
  80. Indent size is 4.
  81. @item
  82. The TAB character is forbidden outside of Makefiles as is any
  83. form of trailing whitespace. Commits containing either will be
  84. rejected by the git repository.
  85. @item
  86. You should try to limit your code lines to 80 characters; however, do so if
  87. and only if this improves readability.
  88. @end itemize
  89. The presentation is one inspired by 'indent -i4 -kr -nut'.
  90. The main priority in Libav is simplicity and small code size in order to
  91. minimize the bug count.
  92. @subsection Comments
  93. Use the JavaDoc/Doxygen format (see examples below) so that code documentation
  94. can be generated automatically. All nontrivial functions should have a comment
  95. above them explaining what the function does, even if it is just one sentence.
  96. All structures and their member variables should be documented, too.
  97. Avoid Qt-style and similar Doxygen syntax with @code{!} in it, i.e. replace
  98. @code{//!} with @code{///} and similar. Also @@ syntax should be employed
  99. for markup commands, i.e. use @code{@@param} and not @code{\param}.
  100. @example
  101. /**
  102. * @@file
  103. * MPEG codec.
  104. * @@author ...
  105. */
  106. /**
  107. * Summary sentence.
  108. * more text ...
  109. * ...
  110. */
  111. typedef struct Foobar@{
  112. int var1; /**< var1 description */
  113. int var2; ///< var2 description
  114. /** var3 description */
  115. int var3;
  116. @} Foobar;
  117. /**
  118. * Summary sentence.
  119. * more text ...
  120. * ...
  121. * @@param my_parameter description of my_parameter
  122. * @@return return value description
  123. */
  124. int myfunc(int my_parameter)
  125. ...
  126. @end example
  127. @subsection C language features
  128. Libav is programmed in the ISO C90 language with a few additional
  129. features from ISO C99, namely:
  130. @itemize @bullet
  131. @item
  132. the @samp{inline} keyword;
  133. @item
  134. @samp{//} comments;
  135. @item
  136. designated struct initializers (@samp{struct s x = @{ .i = 17 @};})
  137. @item
  138. compound literals (@samp{x = (struct s) @{ 17, 23 @};})
  139. @end itemize
  140. These features are supported by all compilers we care about, so we will not
  141. accept patches to remove their use unless they absolutely do not impair
  142. clarity and performance.
  143. All code must compile with recent versions of GCC and a number of other
  144. currently supported compilers. To ensure compatibility, please do not use
  145. additional C99 features or GCC extensions. Especially watch out for:
  146. @itemize @bullet
  147. @item
  148. mixing statements and declarations;
  149. @item
  150. @samp{long long} (use @samp{int64_t} instead);
  151. @item
  152. @samp{__attribute__} not protected by @samp{#ifdef __GNUC__} or similar;
  153. @item
  154. GCC statement expressions (@samp{(x = (@{ int y = 4; y; @})}).
  155. @end itemize
  156. @subsection Naming conventions
  157. All names are using underscores (_), not CamelCase. For example,
  158. @samp{avfilter_get_video_buffer} is a valid function name and
  159. @samp{AVFilterGetVideo} is not. The only exception from this are structure
  160. names; they should always be in the CamelCase
  161. There are following conventions for naming variables and functions:
  162. @itemize @bullet
  163. @item
  164. For local variables no prefix is required.
  165. @item
  166. For variables and functions declared as @code{static} no prefixes are required.
  167. @item
  168. For variables and functions used internally by the library, @code{ff_} prefix
  169. should be used.
  170. For example, @samp{ff_w64_demuxer}.
  171. @item
  172. For variables and functions used internally across multiple libraries, use
  173. @code{avpriv_}. For example, @samp{avpriv_aac_parse_header}.
  174. @item
  175. For exported names, each library has its own prefixes. Just check the existing
  176. code and name accordingly.
  177. @end itemize
  178. @subsection Miscellaneous conventions
  179. @itemize @bullet
  180. @item
  181. fprintf and printf are forbidden in libavformat and libavcodec,
  182. please use av_log() instead.
  183. @item
  184. Casts should be used only when necessary. Unneeded parentheses
  185. should also be avoided if they don't make the code easier to understand.
  186. @end itemize
  187. @subsection Editor configuration
  188. In order to configure Vim to follow Libav formatting conventions, paste
  189. the following snippet into your @file{.vimrc}:
  190. @example
  191. " Indentation rules for Libav: 4 spaces, no tabs.
  192. set expandtab
  193. set shiftwidth=4
  194. set softtabstop=4
  195. set cindent
  196. set cinoptions=(0
  197. " Allow tabs in Makefiles.
  198. autocmd FileType make set noexpandtab shiftwidth=8 softtabstop=8
  199. " Trailing whitespace and tabs are forbidden, so highlight them.
  200. highlight ForbiddenWhitespace ctermbg=red guibg=red
  201. match ForbiddenWhitespace /\s\+$\|\t/
  202. " Do not highlight spaces at the end of line while typing on that line.
  203. autocmd InsertEnter * match ForbiddenWhitespace /\t\|\s\+\%#\@@<!$/
  204. @end example
  205. For Emacs, add these roughly equivalent lines to your @file{.emacs.d/init.el}:
  206. @example
  207. (c-add-style "libav"
  208. '("k&r"
  209. (c-basic-offset . 4)
  210. (indent-tabs-mode . nil)
  211. (show-trailing-whitespace . t)
  212. (c-offsets-alist
  213. (statement-cont . (c-lineup-assignments +)))
  214. )
  215. )
  216. (setq c-default-style "libav")
  217. @end example
  218. @section Development Policy
  219. @enumerate
  220. @item
  221. Contributions should be licensed under the
  222. @uref{http://www.gnu.org/licenses/lgpl-2.1.html, LGPL 2.1},
  223. including an "or any later version" clause, or, if you prefer
  224. a gift-style license, the
  225. @uref{http://www.isc.org/software/license/, ISC} or
  226. @uref{http://mit-license.org/, MIT} license.
  227. @uref{http://www.gnu.org/licenses/gpl-2.0.html, GPL 2} including
  228. an "or any later version" clause is also acceptable, but LGPL is
  229. preferred.
  230. @item
  231. All the patches MUST be reviewed in the mailing list before they are
  232. committed.
  233. @item
  234. The Libav coding style should remain consistent. Changes to
  235. conform will be suggested during the review or implemented on commit.
  236. @item
  237. Patches should be generated using @code{git format-patch} or directly sent
  238. using @code{git send-email}.
  239. Please make sure you give the proper credit by setting the correct author
  240. in the commit.
  241. @item
  242. The commit message should have a short first line in the form of
  243. a @samp{topic: short description} as a header, separated by a newline
  244. from the body consisting of an explanation of why the change is necessary.
  245. If the commit fixes a known bug on the bug tracker, the commit message
  246. should include its bug ID. Referring to the issue on the bug tracker does
  247. not exempt you from writing an excerpt of the bug in the commit message.
  248. If the patch is a bug fix which should be backported to stable releases,
  249. i.e. a non-API/ABI-breaking bug fix, add @code{CC: libav-stable@@libav.org}
  250. to the bottom of your commit message, and make sure to CC your patch to
  251. this address, too. Some git setups will do this automatically.
  252. @item
  253. Work in progress patches should be sent to the mailing list with the [WIP]
  254. or the [RFC] tag.
  255. @item
  256. Branches in public personal repos are advised as way to
  257. work on issues collaboratively.
  258. @item
  259. You do not have to over-test things. If it works for you and you think it
  260. should work for others, send it to the mailing list for review.
  261. If you have doubt about portability please state it in the submission so
  262. people with specific hardware could test it.
  263. @item
  264. Do not commit unrelated changes together, split them into self-contained
  265. pieces. Also do not forget that if part B depends on part A, but A does not
  266. depend on B, then A can and should be committed first and separate from B.
  267. Keeping changes well split into self-contained parts makes reviewing and
  268. understanding them on the commit log mailing list easier. This also helps
  269. in case of debugging later on.
  270. @item
  271. Patches that change behavior of the programs (renaming options etc) or
  272. public API or ABI should be discussed in depth and possible few days should
  273. pass between discussion and commit.
  274. Changes to the build system (Makefiles, configure script) which alter
  275. the expected behavior should be considered in the same regard.
  276. @item
  277. When applying patches that have been discussed (at length) on the mailing
  278. list, reference the thread in the log message.
  279. @item
  280. Subscribe to the
  281. @uref{https://lists.libav.org/mailman/listinfo/libav-devel, libav-devel} and
  282. @uref{https://lists.libav.org/mailman/listinfo/libav-commits, libav-commits}
  283. mailing lists.
  284. Bugs and possible improvements or general questions regarding commits
  285. are discussed on libav-devel. We expect you to react if problems with
  286. your code are uncovered.
  287. @item
  288. Update the documentation if you change behavior or add features. If you are
  289. unsure how best to do this, send an [RFC] patch to libav-devel.
  290. @item
  291. All discussions and decisions should be reported on the public developer
  292. mailing list, so that there is a reference to them.
  293. Other media (e.g. IRC) should be used for coordination and immediate
  294. collaboration.
  295. @item
  296. Never write to unallocated memory, never write over the end of arrays,
  297. always check values read from some untrusted source before using them
  298. as array index or other risky things. Always use valgrind to double-check.
  299. @item
  300. Remember to check if you need to bump versions for the specific libav
  301. parts (libavutil, libavcodec, libavformat) you are changing. You need
  302. to change the version integer.
  303. Incrementing the first component means no backward compatibility to
  304. previous versions (e.g. removal of a function from the public API).
  305. Incrementing the second component means backward compatible change
  306. (e.g. addition of a function to the public API or extension of an
  307. existing data structure).
  308. Incrementing the third component means a noteworthy binary compatible
  309. change (e.g. encoder bug fix that matters for the decoder).
  310. @item
  311. Compiler warnings indicate potential bugs or code with bad style.
  312. If it is a bug, the bug has to be fixed. If it is not, the code should
  313. be changed to not generate a warning unless that causes a slowdown
  314. or obfuscates the code.
  315. If a type of warning leads to too many false positives, that warning
  316. should be disabled, not the code changed.
  317. @item
  318. If you add a new file, give it a proper license header. Do not copy and
  319. paste it from a random place, use an existing file as template.
  320. @end enumerate
  321. We think our rules are not too hard. If you have comments, contact us.
  322. @section Submitting patches
  323. First, read the @ref{Coding Rules} above if you did not yet, in particular
  324. the rules regarding patch submission.
  325. As stated already, please do not submit a patch which contains several
  326. unrelated changes.
  327. Split it into separate, self-contained pieces. This does not mean splitting
  328. file by file. Instead, make the patch as small as possible while still
  329. keeping it as a logical unit that contains an individual change, even
  330. if it spans multiple files. This makes reviewing your patches much easier
  331. for us and greatly increases your chances of getting your patch applied.
  332. Use the patcheck tool of Libav to check your patch.
  333. The tool is located in the tools directory.
  334. Run the @ref{Regression Tests} before submitting a patch in order to verify
  335. it does not cause unexpected problems.
  336. It also helps quite a bit if you tell us what the patch does (for example
  337. 'replaces lrint by lrintf'), and why (for example '*BSD isn't C99 compliant
  338. and has no lrint()'). This kind of explanation should be the body of the
  339. commit message.
  340. Also please if you send several patches, send each patch as a separate mail,
  341. do not attach several unrelated patches to the same mail.
  342. Patches should be posted to the
  343. @uref{https://lists.libav.org/mailman/listinfo/libav-devel, libav-devel}
  344. mailing list. Use @code{git send-email} when possible since it will properly
  345. send patches without requiring extra care. If you cannot, then send patches
  346. as base64-encoded attachments, so your patch is not trashed during
  347. transmission.
  348. Your patch will be reviewed on the mailing list. You will likely be asked
  349. to make some changes and are expected to send in an improved version that
  350. incorporates the requests from the review. This process may go through
  351. several iterations. Once your patch is deemed good enough, it will be
  352. committed to the official Libav tree.
  353. Give us a few days to react. But if some time passes without reaction,
  354. send a reminder by email. Your patch should eventually be dealt with.
  355. @section New codecs or formats checklist
  356. @enumerate
  357. @item
  358. Did you use av_cold for codec initialization and close functions?
  359. @item
  360. Did you add a long_name under NULL_IF_CONFIG_SMALL to the AVCodec or
  361. AVInputFormat/AVOutputFormat struct?
  362. @item
  363. Did you bump the minor version number (and reset the micro version
  364. number) in @file{libavcodec/version.h} or @file{libavformat/version.h}?
  365. @item
  366. Did you register it in @file{allcodecs.c} or @file{allformats.c}?
  367. @item
  368. Did you add the AVCodecID to @file{avcodec.h}?
  369. When adding new codec IDs, also add an entry to the codec descriptor
  370. list in @file{libavcodec/codec_desc.c}.
  371. @item
  372. If it has a FourCC, did you add it to @file{libavformat/riff.c},
  373. even if it is only a decoder?
  374. @item
  375. Did you add a rule to compile the appropriate files in the Makefile?
  376. Remember to do this even if you are just adding a format to a file that
  377. is already being compiled by some other rule, like a raw demuxer.
  378. @item
  379. Did you add an entry to the table of supported formats or codecs in
  380. @file{doc/general.texi}?
  381. @item
  382. Did you add an entry in the Changelog?
  383. @item
  384. If it depends on a parser or a library, did you add that dependency in
  385. configure?
  386. @item
  387. Did you @code{git add} the appropriate files before committing?
  388. @item
  389. Did you make sure it compiles standalone, i.e. with
  390. @code{configure --disable-everything --enable-decoder=foo}
  391. (or @code{--enable-demuxer} or whatever your component is)?
  392. @end enumerate
  393. @section patch submission checklist
  394. @enumerate
  395. @item
  396. Does @code{make check} pass with the patch applied?
  397. @item
  398. Is the patch against latest Libav git master branch?
  399. @item
  400. Are you subscribed to the
  401. @uref{https://lists.libav.org/mailman/listinfo/libav-devel, libav-devel}
  402. mailing list? (Only list subscribers are allowed to post.)
  403. @item
  404. Have you checked that the changes are minimal, so that the same cannot be
  405. achieved with a smaller patch and/or simpler final code?
  406. @item
  407. If the change is to speed critical code, did you benchmark it?
  408. @item
  409. If you did any benchmarks, did you provide them in the mail?
  410. @item
  411. Have you checked that the patch does not introduce buffer overflows or
  412. other security issues?
  413. @item
  414. Did you test your decoder or demuxer against damaged data? If no, see
  415. tools/trasher, the noise bitstream filter, and
  416. @uref{http://caca.zoy.org/wiki/zzuf, zzuf}. Your decoder or demuxer
  417. should not crash, end in a (near) infinite loop, or allocate ridiculous
  418. amounts of memory when fed damaged data.
  419. @item
  420. Does the patch not mix functional and cosmetic changes?
  421. @item
  422. Did you add tabs or trailing whitespace to the code? Both are forbidden.
  423. @item
  424. Is the patch attached to the email you send?
  425. @item
  426. Is the mime type of the patch correct? It should be text/x-diff or
  427. text/x-patch or at least text/plain and not application/octet-stream.
  428. @item
  429. If the patch fixes a bug, did you provide a verbose analysis of the bug?
  430. @item
  431. If the patch fixes a bug, did you provide enough information, including
  432. a sample, so the bug can be reproduced and the fix can be verified?
  433. Note please do not attach samples >100k to mails but rather provide a
  434. URL, you can upload to ftp://upload.libav.org
  435. @item
  436. Did you provide a verbose summary about what the patch does change?
  437. @item
  438. Did you provide a verbose explanation why it changes things like it does?
  439. @item
  440. Did you provide a verbose summary of the user visible advantages and
  441. disadvantages if the patch is applied?
  442. @item
  443. Did you provide an example so we can verify the new feature added by the
  444. patch easily?
  445. @item
  446. If you added a new file, did you insert a license header? It should be
  447. taken from Libav, not randomly copied and pasted from somewhere else.
  448. @item
  449. You should maintain alphabetical order in alphabetically ordered lists as
  450. long as doing so does not break API/ABI compatibility.
  451. @item
  452. Lines with similar content should be aligned vertically when doing so
  453. improves readability.
  454. @item
  455. Make sure you check the return values of function and return appropriate
  456. error codes. Especially memory allocation functions like @code{malloc()}
  457. are notoriously left unchecked, which is a serious problem.
  458. @end enumerate
  459. @section Patch review process
  460. All patches posted to the
  461. @uref{https://lists.libav.org/mailman/listinfo/libav-devel, libav-devel}
  462. mailing list will be reviewed, unless they contain a
  463. clear note that the patch is not for the git master branch.
  464. Reviews and comments will be posted as replies to the patch on the
  465. mailing list. The patch submitter then has to take care of every comment,
  466. that can be by resubmitting a changed patch or by discussion. Resubmitted
  467. patches will themselves be reviewed like any other patch. If at some point
  468. a patch passes review with no comments then it is approved, that can for
  469. simple and small patches happen immediately while large patches will generally
  470. have to be changed and reviewed many times before they are approved.
  471. After a patch is approved it will be committed to the repository.
  472. We will review all submitted patches, but sometimes we are quite busy so
  473. especially for large patches this can take several weeks.
  474. When resubmitting patches, if their size grew or during the review different
  475. issues arisen please split the patch so each issue has a specific patch.
  476. @anchor{Regression Tests}
  477. @section Regression Tests
  478. Before submitting a patch (or committing to the repository), you should at
  479. least make sure that it does not break anything.
  480. If the code changed has already a test present in FATE you should run it,
  481. otherwise it is advised to add it.
  482. Improvements to codec or demuxer might change the FATE results. Make sure
  483. to commit the update reference with the change and to explain in the comment
  484. why the expected result changed.
  485. Please refer to @url{fate.html}.
  486. @bye