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.

541 lines
20KB

  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 LGPL 2.1, including an
  222. "or any later version" clause, or the MIT license. GPL 2 including
  223. an "or any later version" clause is also acceptable, but LGPL is
  224. preferred.
  225. @item
  226. All the patches MUST be reviewed in the mailing list before they are
  227. committed.
  228. @item
  229. The Libav coding style should remain consistent. Changes to
  230. conform will be suggested during the review or implemented on commit.
  231. @item
  232. Patches should be generated using @code{git format-patch} or directly sent
  233. using @code{git send-email}.
  234. Please make sure you give the proper credit by setting the correct author
  235. in the commit.
  236. @item
  237. The commit message should have a short first line in the form of
  238. a @samp{topic: short description} as a header, separated by a newline
  239. from the body consisting of an explanation of why the change is necessary.
  240. If the commit fixes a known bug on the bug tracker, the commit message
  241. should include its bug ID. Referring to the issue on the bug tracker does
  242. not exempt you from writing an excerpt of the bug in the commit message.
  243. If the patch is a bug fix which should be backported to stable releases,
  244. i.e. a non-API/ABI-breaking bug fix, add @code{CC: libav-stable@@libav.org}
  245. to the bottom of your commit message, and make sure to CC your patch to
  246. this address, too. Some git setups will do this automatically.
  247. @item
  248. Work in progress patches should be sent to the mailing list with the [WIP]
  249. or the [RFC] tag.
  250. @item
  251. Branches in public personal repos are advised as way to
  252. work on issues collaboratively.
  253. @item
  254. You do not have to over-test things. If it works for you and you think it
  255. should work for others, send it to the mailing list for review.
  256. If you have doubt about portability please state it in the submission so
  257. people with specific hardware could test it.
  258. @item
  259. Do not commit unrelated changes together, split them into self-contained
  260. pieces. Also do not forget that if part B depends on part A, but A does not
  261. depend on B, then A can and should be committed first and separate from B.
  262. Keeping changes well split into self-contained parts makes reviewing and
  263. understanding them on the commit log mailing list easier. This also helps
  264. in case of debugging later on.
  265. @item
  266. Patches that change behavior of the programs (renaming options etc) or
  267. public API or ABI should be discussed in depth and possible few days should
  268. pass between discussion and commit.
  269. Changes to the build system (Makefiles, configure script) which alter
  270. the expected behavior should be considered in the same regard.
  271. @item
  272. When applying patches that have been discussed (at length) on the mailing
  273. list, reference the thread in the log message.
  274. @item
  275. Subscribe to the
  276. @uref{https://lists.libav.org/mailman/listinfo/libav-devel, libav-devel} and
  277. @uref{https://lists.libav.org/mailman/listinfo/libav-commits, libav-commits}
  278. mailing lists.
  279. Bugs and possible improvements or general questions regarding commits
  280. are discussed on libav-devel. We expect you to react if problems with
  281. your code are uncovered.
  282. @item
  283. Update the documentation if you change behavior or add features. If you are
  284. unsure how best to do this, send an [RFC] patch to libav-devel.
  285. @item
  286. All discussions and decisions should be reported on the public developer
  287. mailing list, so that there is a reference to them.
  288. Other media (e.g. IRC) should be used for coordination and immediate
  289. collaboration.
  290. @item
  291. Never write to unallocated memory, never write over the end of arrays,
  292. always check values read from some untrusted source before using them
  293. as array index or other risky things. Always use valgrind to double-check.
  294. @item
  295. Remember to check if you need to bump versions for the specific libav
  296. parts (libavutil, libavcodec, libavformat) you are changing. You need
  297. to change the version integer.
  298. Incrementing the first component means no backward compatibility to
  299. previous versions (e.g. removal of a function from the public API).
  300. Incrementing the second component means backward compatible change
  301. (e.g. addition of a function to the public API or extension of an
  302. existing data structure).
  303. Incrementing the third component means a noteworthy binary compatible
  304. change (e.g. encoder bug fix that matters for the decoder).
  305. @item
  306. Compiler warnings indicate potential bugs or code with bad style.
  307. If it is a bug, the bug has to be fixed. If it is not, the code should
  308. be changed to not generate a warning unless that causes a slowdown
  309. or obfuscates the code.
  310. If a type of warning leads to too many false positives, that warning
  311. should be disabled, not the code changed.
  312. @item
  313. If you add a new file, give it a proper license header. Do not copy and
  314. paste it from a random place, use an existing file as template.
  315. @end enumerate
  316. We think our rules are not too hard. If you have comments, contact us.
  317. Note, some rules were borrowed from the MPlayer project.
  318. @section Submitting patches
  319. First, read the @ref{Coding Rules} above if you did not yet, in particular
  320. the rules regarding patch submission.
  321. As stated already, please do not submit a patch which contains several
  322. unrelated changes.
  323. Split it into separate, self-contained pieces. This does not mean splitting
  324. file by file. Instead, make the patch as small as possible while still
  325. keeping it as a logical unit that contains an individual change, even
  326. if it spans multiple files. This makes reviewing your patches much easier
  327. for us and greatly increases your chances of getting your patch applied.
  328. Use the patcheck tool of Libav to check your patch.
  329. The tool is located in the tools directory.
  330. Run the @ref{Regression Tests} before submitting a patch in order to verify
  331. it does not cause unexpected problems.
  332. It also helps quite a bit if you tell us what the patch does (for example
  333. 'replaces lrint by lrintf'), and why (for example '*BSD isn't C99 compliant
  334. and has no lrint()'). This kind of explanation should be the body of the
  335. commit message.
  336. Also please if you send several patches, send each patch as a separate mail,
  337. do not attach several unrelated patches to the same mail.
  338. Patches should be posted to the
  339. @uref{https://lists.libav.org/mailman/listinfo/libav-devel, libav-devel}
  340. mailing list. Use @code{git send-email} when possible since it will properly
  341. send patches without requiring extra care. If you cannot, then send patches
  342. as base64-encoded attachments, so your patch is not trashed during
  343. transmission.
  344. Your patch will be reviewed on the mailing list. You will likely be asked
  345. to make some changes and are expected to send in an improved version that
  346. incorporates the requests from the review. This process may go through
  347. several iterations. Once your patch is deemed good enough, it will be
  348. committed to the official Libav tree.
  349. Give us a few days to react. But if some time passes without reaction,
  350. send a reminder by email. Your patch should eventually be dealt with.
  351. @section New codecs or formats checklist
  352. @enumerate
  353. @item
  354. Did you use av_cold for codec initialization and close functions?
  355. @item
  356. Did you add a long_name under NULL_IF_CONFIG_SMALL to the AVCodec or
  357. AVInputFormat/AVOutputFormat struct?
  358. @item
  359. Did you bump the minor version number (and reset the micro version
  360. number) in @file{libavcodec/version.h} or @file{libavformat/version.h}?
  361. @item
  362. Did you register it in @file{allcodecs.c} or @file{allformats.c}?
  363. @item
  364. Did you add the AVCodecID to @file{avcodec.h}?
  365. When adding new codec IDs, also add an entry to the codec descriptor
  366. list in @file{libavcodec/codec_desc.c}.
  367. @item
  368. If it has a FourCC, did you add it to @file{libavformat/riff.c},
  369. even if it is only a decoder?
  370. @item
  371. Did you add a rule to compile the appropriate files in the Makefile?
  372. Remember to do this even if you are just adding a format to a file that
  373. is already being compiled by some other rule, like a raw demuxer.
  374. @item
  375. Did you add an entry to the table of supported formats or codecs in
  376. @file{doc/general.texi}?
  377. @item
  378. Did you add an entry in the Changelog?
  379. @item
  380. If it depends on a parser or a library, did you add that dependency in
  381. configure?
  382. @item
  383. Did you @code{git add} the appropriate files before committing?
  384. @item
  385. Did you make sure it compiles standalone, i.e. with
  386. @code{configure --disable-everything --enable-decoder=foo}
  387. (or @code{--enable-demuxer} or whatever your component is)?
  388. @end enumerate
  389. @section patch submission checklist
  390. @enumerate
  391. @item
  392. Does @code{make check} pass with the patch applied?
  393. @item
  394. Is the patch against latest Libav git master branch?
  395. @item
  396. Are you subscribed to the
  397. @uref{https://lists.libav.org/mailman/listinfo/libav-devel, libav-devel}
  398. mailing list? (Only list subscribers are allowed to post.)
  399. @item
  400. Have you checked that the changes are minimal, so that the same cannot be
  401. achieved with a smaller patch and/or simpler final code?
  402. @item
  403. If the change is to speed critical code, did you benchmark it?
  404. @item
  405. If you did any benchmarks, did you provide them in the mail?
  406. @item
  407. Have you checked that the patch does not introduce buffer overflows or
  408. other security issues?
  409. @item
  410. Did you test your decoder or demuxer against damaged data? If no, see
  411. tools/trasher, the noise bitstream filter, and
  412. @uref{http://caca.zoy.org/wiki/zzuf, zzuf}. Your decoder or demuxer
  413. should not crash, end in a (near) infinite loop, or allocate ridiculous
  414. amounts of memory when fed damaged data.
  415. @item
  416. Does the patch not mix functional and cosmetic changes?
  417. @item
  418. Did you add tabs or trailing whitespace to the code? Both are forbidden.
  419. @item
  420. Is the patch attached to the email you send?
  421. @item
  422. Is the mime type of the patch correct? It should be text/x-diff or
  423. text/x-patch or at least text/plain and not application/octet-stream.
  424. @item
  425. If the patch fixes a bug, did you provide a verbose analysis of the bug?
  426. @item
  427. If the patch fixes a bug, did you provide enough information, including
  428. a sample, so the bug can be reproduced and the fix can be verified?
  429. Note please do not attach samples >100k to mails but rather provide a
  430. URL, you can upload to ftp://upload.libav.org
  431. @item
  432. Did you provide a verbose summary about what the patch does change?
  433. @item
  434. Did you provide a verbose explanation why it changes things like it does?
  435. @item
  436. Did you provide a verbose summary of the user visible advantages and
  437. disadvantages if the patch is applied?
  438. @item
  439. Did you provide an example so we can verify the new feature added by the
  440. patch easily?
  441. @item
  442. If you added a new file, did you insert a license header? It should be
  443. taken from Libav, not randomly copied and pasted from somewhere else.
  444. @item
  445. You should maintain alphabetical order in alphabetically ordered lists as
  446. long as doing so does not break API/ABI compatibility.
  447. @item
  448. Lines with similar content should be aligned vertically when doing so
  449. improves readability.
  450. @item
  451. Make sure you check the return values of function and return appropriate
  452. error codes. Especially memory allocation functions like @code{malloc()}
  453. are notoriously left unchecked, which is a serious problem.
  454. @end enumerate
  455. @section Patch review process
  456. All patches posted to the
  457. @uref{https://lists.libav.org/mailman/listinfo/libav-devel, libav-devel}
  458. mailing list will be reviewed, unless they contain a
  459. clear note that the patch is not for the git master branch.
  460. Reviews and comments will be posted as replies to the patch on the
  461. mailing list. The patch submitter then has to take care of every comment,
  462. that can be by resubmitting a changed patch or by discussion. Resubmitted
  463. patches will themselves be reviewed like any other patch. If at some point
  464. a patch passes review with no comments then it is approved, that can for
  465. simple and small patches happen immediately while large patches will generally
  466. have to be changed and reviewed many times before they are approved.
  467. After a patch is approved it will be committed to the repository.
  468. We will review all submitted patches, but sometimes we are quite busy so
  469. especially for large patches this can take several weeks.
  470. When resubmitting patches, if their size grew or during the review different
  471. issues arisen please split the patch so each issue has a specific patch.
  472. @anchor{Regression Tests}
  473. @section Regression Tests
  474. Before submitting a patch (or committing to the repository), you should at
  475. least make sure that it does not break anything.
  476. If the code changed has already a test present in FATE you should run it,
  477. otherwise it is advised to add it.
  478. Improvements to codec or demuxer might change the FATE results. Make sure
  479. to commit the update reference with the change and to explain in the comment
  480. why the expected result changed.
  481. Please refer to @url{fate.html}.
  482. @bye