optimization.txt 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. optimization Tips (for libavcodec):
  2. ===================================
  3. What to optimize:
  4. -----------------
  5. If you plan to do non-x86 architecture specific optimizations (SIMD normally),
  6. then take a look in the x86/ directory, as most important functions are
  7. already optimized for MMX.
  8. If you want to do x86 optimizations then you can either try to finetune the
  9. stuff in the x86 directory or find some other functions in the C source to
  10. optimize, but there aren't many left.
  11. Understanding these overoptimized functions:
  12. --------------------------------------------
  13. As many functions tend to be a bit difficult to understand because
  14. of optimizations, it can be hard to optimize them further, or write
  15. architecture-specific versions. It is recommended to look at older
  16. revisions of the interesting files (for a web frontend try ViewVC at
  17. http://svn.ffmpeg.org/ffmpeg/trunk/).
  18. Alternatively, look into the other architecture-specific versions in
  19. the x86/, ppc/, alpha/ subdirectories. Even if you don't exactly
  20. comprehend the instructions, it could help understanding the functions
  21. and how they can be optimized.
  22. NOTE: If you still don't understand some function, ask at our mailing list!!!
  23. (http://lists.mplayerhq.hu/mailman/listinfo/ffmpeg-devel)
  24. When is an optimization justified?
  25. ----------------------------------
  26. Normally, clean and simple optimizations for widely used codecs are
  27. justified even if they only achieve an overall speedup of 0.1%. These
  28. speedups accumulate and can make a big difference after awhile. Also, if
  29. none of the following factors get worse due to an optimization -- speed,
  30. binary code size, source size, source readability -- and at least one
  31. factor improves, then an optimization is always a good idea even if the
  32. overall gain is less than 0.1%. For obscure codecs that are not often
  33. used, the goal is more toward keeping the code clean, small, and
  34. readable instead of making it 1% faster.
  35. WTF is that function good for ....:
  36. -----------------------------------
  37. The primary purpose of this list is to avoid wasting time optimizing functions
  38. which are rarely used.
  39. put(_no_rnd)_pixels{,_x2,_y2,_xy2}
  40. Used in motion compensation (en/decoding).
  41. avg_pixels{,_x2,_y2,_xy2}
  42. Used in motion compensation of B-frames.
  43. These are less important than the put*pixels functions.
  44. avg_no_rnd_pixels*
  45. unused
  46. pix_abs16x16{,_x2,_y2,_xy2}
  47. Used in motion estimation (encoding) with SAD.
  48. pix_abs8x8{,_x2,_y2,_xy2}
  49. Used in motion estimation (encoding) with SAD of MPEG-4 4MV only.
  50. These are less important than the pix_abs16x16* functions.
  51. put_mspel8_mc* / wmv2_mspel8*
  52. Used only in WMV2.
  53. it is not recommended that you waste your time with these, as WMV2
  54. is an ugly and relatively useless codec.
  55. mpeg4_qpel* / *qpel_mc*
  56. Used in MPEG-4 qpel motion compensation (encoding & decoding).
  57. The qpel8 functions are used only for 4mv,
  58. the avg_* functions are used only for B-frames.
  59. Optimizing them should have a significant impact on qpel
  60. encoding & decoding.
  61. qpel{8,16}_mc??_old_c / *pixels{8,16}_l4
  62. Just used to work around a bug in an old libavcodec encoder version.
  63. Don't optimize them.
  64. tpel_mc_func {put,avg}_tpel_pixels_tab
  65. Used only for SVQ3, so only optimize them if you need fast SVQ3 decoding.
  66. add_bytes/diff_bytes
  67. For huffyuv only, optimize if you want a faster ffhuffyuv codec.
  68. get_pixels / diff_pixels
  69. Used for encoding, easy.
  70. clear_blocks
  71. easiest to optimize
  72. gmc
  73. Used for MPEG-4 gmc.
  74. Optimizing this should have a significant effect on the gmc decoding
  75. speed.
  76. gmc1
  77. Used for chroma blocks in MPEG-4 gmc with 1 warp point
  78. (there are 4 luma & 2 chroma blocks per macroblock, so
  79. only 1/3 of the gmc blocks use this, the other 2/3
  80. use the normal put_pixel* code, but only if there is
  81. just 1 warp point).
  82. Note: DivX5 gmc always uses just 1 warp point.
  83. pix_sum
  84. Used for encoding.
  85. hadamard8_diff / sse / sad == pix_norm1 / dct_sad / quant_psnr / rd / bit
  86. Specific compare functions used in encoding, it depends upon the
  87. command line switches which of these are used.
  88. Don't waste your time with dct_sad & quant_psnr, they aren't
  89. really useful.
  90. put_pixels_clamped / add_pixels_clamped
  91. Used for en/decoding in the IDCT, easy.
  92. Note, some optimized IDCTs have the add/put clamped code included and
  93. then put_pixels_clamped / add_pixels_clamped will be unused.
  94. idct/fdct
  95. idct (encoding & decoding)
  96. fdct (encoding)
  97. difficult to optimize
  98. dct_quantize_trellis
  99. Used for encoding with trellis quantization.
  100. difficult to optimize
  101. dct_quantize
  102. Used for encoding.
  103. dct_unquantize_mpeg1
  104. Used in MPEG-1 en/decoding.
  105. dct_unquantize_mpeg2
  106. Used in MPEG-2 en/decoding.
  107. dct_unquantize_h263
  108. Used in MPEG-4/H.263 en/decoding.
  109. FIXME remaining functions?
  110. BTW, most of these functions are in dsputil.c/.h, some are in mpegvideo.c/.h.
  111. Alignment:
  112. Some instructions on some architectures have strict alignment restrictions,
  113. for example most SSE/SSE2 instructions on x86.
  114. The minimum guaranteed alignment is written in the .h files, for example:
  115. void (*put_pixels_clamped)(const DCTELEM *block/*align 16*/, UINT8 *pixels/*align 8*/, int line_size);
  116. General Tips:
  117. -------------
  118. Use asm loops like:
  119. __asm__(
  120. "1: ....
  121. ...
  122. "jump_instruciton ....
  123. Do not use C loops:
  124. do{
  125. __asm__(
  126. ...
  127. }while()
  128. Use __asm__() instead of intrinsics. The latter requires a good optimizing compiler
  129. which gcc is not.
  130. Links:
  131. ======
  132. http://www.aggregate.org/MAGIC/
  133. x86-specific:
  134. -------------
  135. http://developer.intel.com/design/pentium4/manuals/248966.htm
  136. The IA-32 Intel Architecture Software Developer's Manual, Volume 2:
  137. Instruction Set Reference
  138. http://developer.intel.com/design/pentium4/manuals/245471.htm
  139. http://www.agner.org/assem/
  140. AMD Athlon Processor x86 Code Optimization Guide:
  141. http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/22007.pdf
  142. ARM-specific:
  143. -------------
  144. ARM Architecture Reference Manual (up to ARMv5TE):
  145. http://www.arm.com/community/university/eulaarmarm.html
  146. Procedure Call Standard for the ARM Architecture:
  147. http://www.arm.com/pdfs/aapcs.pdf
  148. Optimization guide for ARM9E (used in Nokia 770 Internet Tablet):
  149. http://infocenter.arm.com/help/topic/com.arm.doc.ddi0240b/DDI0240A.pdf
  150. Optimization guide for ARM11 (used in Nokia N800 Internet Tablet):
  151. http://infocenter.arm.com/help/topic/com.arm.doc.ddi0211j/DDI0211J_arm1136_r1p5_trm.pdf
  152. Optimization guide for Intel XScale (used in Sharp Zaurus PDA):
  153. http://download.intel.com/design/intelxscale/27347302.pdf
  154. Intel Wireless MMX2 Coprocessor: Programmers Reference Manual
  155. http://download.intel.com/design/intelxscale/31451001.pdf
  156. PowerPC-specific:
  157. -----------------
  158. PowerPC32/AltiVec PIM:
  159. www.freescale.com/files/32bit/doc/ref_manual/ALTIVECPEM.pdf
  160. PowerPC32/AltiVec PEM:
  161. www.freescale.com/files/32bit/doc/ref_manual/ALTIVECPIM.pdf
  162. CELL/SPU:
  163. http://www-01.ibm.com/chips/techlib/techlib.nsf/techdocs/30B3520C93F437AB87257060006FFE5E/$file/Language_Extensions_for_CBEA_2.4.pdf
  164. http://www-01.ibm.com/chips/techlib/techlib.nsf/techdocs/9F820A5FFA3ECE8C8725716A0062585F/$file/CBE_Handbook_v1.1_24APR2007_pub.pdf
  165. SPARC-specific:
  166. ---------------
  167. SPARC Joint Programming Specification (JPS1): Commonality
  168. http://www.fujitsu.com/downloads/PRMPWR/JPS1-R1.0.4-Common-pub.pdf
  169. UltraSPARC III Processor User's Manual (contains instruction timings)
  170. http://www.sun.com/processors/manuals/USIIIv2.pdf
  171. VIS Whitepaper (contains optimization guidelines)
  172. http://www.sun.com/processors/vis/download/vis/vis_whitepaper.pdf
  173. GCC asm links:
  174. --------------
  175. official doc but quite ugly
  176. http://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html
  177. a bit old (note "+" is valid for input-output, even though the next disagrees)
  178. http://www.cs.virginia.edu/~clc5q/gcc-inline-asm.pdf