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.

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