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.

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