mpegvideo.c 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656
  1. /*
  2. * The simplest mpeg encoder (well, it was the simplest!)
  3. * Copyright (c) 2000,2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /**
  25. * @file
  26. * The simplest mpeg encoder (well, it was the simplest!).
  27. */
  28. #include "libavutil/intmath.h"
  29. #include "libavutil/imgutils.h"
  30. #include "avcodec.h"
  31. #include "dsputil.h"
  32. #include "internal.h"
  33. #include "mpegvideo.h"
  34. #include "mpegvideo_common.h"
  35. #include "mjpegenc.h"
  36. #include "msmpeg4.h"
  37. #include "faandct.h"
  38. #include "xvmc_internal.h"
  39. #include "thread.h"
  40. #include <limits.h>
  41. //#undef NDEBUG
  42. //#include <assert.h>
  43. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  44. DCTELEM *block, int n, int qscale);
  45. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  46. DCTELEM *block, int n, int qscale);
  47. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  48. DCTELEM *block, int n, int qscale);
  49. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  50. DCTELEM *block, int n, int qscale);
  51. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  52. DCTELEM *block, int n, int qscale);
  53. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  54. DCTELEM *block, int n, int qscale);
  55. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  56. DCTELEM *block, int n, int qscale);
  57. /* enable all paranoid tests for rounding, overflows, etc... */
  58. //#define PARANOID
  59. //#define DEBUG
  60. static const uint8_t ff_default_chroma_qscale_table[32]={
  61. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
  62. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
  63. };
  64. const uint8_t ff_mpeg1_dc_scale_table[128]={
  65. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
  66. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  67. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  68. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  69. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  70. };
  71. static const uint8_t mpeg2_dc_scale_table1[128]={
  72. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
  73. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  74. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  75. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  76. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  77. };
  78. static const uint8_t mpeg2_dc_scale_table2[128]={
  79. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
  80. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  81. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  82. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  83. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  84. };
  85. static const uint8_t mpeg2_dc_scale_table3[128]={
  86. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
  87. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  88. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  89. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  90. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  91. };
  92. const uint8_t * const ff_mpeg2_dc_scale_table[4]={
  93. ff_mpeg1_dc_scale_table,
  94. mpeg2_dc_scale_table1,
  95. mpeg2_dc_scale_table2,
  96. mpeg2_dc_scale_table3,
  97. };
  98. const enum PixelFormat ff_pixfmt_list_420[] = {
  99. PIX_FMT_YUV420P,
  100. PIX_FMT_NONE
  101. };
  102. const enum PixelFormat ff_hwaccel_pixfmt_list_420[] = {
  103. PIX_FMT_DXVA2_VLD,
  104. PIX_FMT_VAAPI_VLD,
  105. PIX_FMT_YUV420P,
  106. PIX_FMT_NONE
  107. };
  108. const uint8_t *ff_find_start_code(const uint8_t * restrict p, const uint8_t *end, uint32_t * restrict state){
  109. int i;
  110. assert(p<=end);
  111. if(p>=end)
  112. return end;
  113. for(i=0; i<3; i++){
  114. uint32_t tmp= *state << 8;
  115. *state= tmp + *(p++);
  116. if(tmp == 0x100 || p==end)
  117. return p;
  118. }
  119. while(p<end){
  120. if (p[-1] > 1 ) p+= 3;
  121. else if(p[-2] ) p+= 2;
  122. else if(p[-3]|(p[-1]-1)) p++;
  123. else{
  124. p++;
  125. break;
  126. }
  127. }
  128. p= FFMIN(p, end)-4;
  129. *state= AV_RB32(p);
  130. return p+4;
  131. }
  132. /* init common dct for both encoder and decoder */
  133. av_cold int ff_dct_common_init(MpegEncContext *s)
  134. {
  135. s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
  136. s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
  137. s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
  138. s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
  139. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
  140. if(s->flags & CODEC_FLAG_BITEXACT)
  141. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
  142. s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
  143. #if HAVE_MMX
  144. MPV_common_init_mmx(s);
  145. #elif ARCH_ALPHA
  146. MPV_common_init_axp(s);
  147. #elif CONFIG_MLIB
  148. MPV_common_init_mlib(s);
  149. #elif HAVE_MMI
  150. MPV_common_init_mmi(s);
  151. #elif ARCH_ARM
  152. MPV_common_init_arm(s);
  153. #elif HAVE_ALTIVEC
  154. MPV_common_init_altivec(s);
  155. #elif ARCH_BFIN
  156. MPV_common_init_bfin(s);
  157. #endif
  158. /* load & permutate scantables
  159. note: only wmv uses different ones
  160. */
  161. if(s->alternate_scan){
  162. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
  163. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
  164. }else{
  165. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
  166. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
  167. }
  168. ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  169. ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
  170. return 0;
  171. }
  172. void ff_copy_picture(Picture *dst, Picture *src){
  173. *dst = *src;
  174. dst->f.type= FF_BUFFER_TYPE_COPY;
  175. }
  176. /**
  177. * Release a frame buffer
  178. */
  179. static void free_frame_buffer(MpegEncContext *s, Picture *pic)
  180. {
  181. ff_thread_release_buffer(s->avctx, (AVFrame*)pic);
  182. av_freep(&pic->f.hwaccel_picture_private);
  183. }
  184. /**
  185. * Allocate a frame buffer
  186. */
  187. static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
  188. {
  189. int r;
  190. if (s->avctx->hwaccel) {
  191. assert(!pic->hwaccel_picture_private);
  192. if (s->avctx->hwaccel->priv_data_size) {
  193. pic->f.hwaccel_picture_private = av_mallocz(s->avctx->hwaccel->priv_data_size);
  194. if (!pic->f.hwaccel_picture_private) {
  195. av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
  196. return -1;
  197. }
  198. }
  199. }
  200. r = ff_thread_get_buffer(s->avctx, (AVFrame*)pic);
  201. if (r < 0 || !pic->f.age || !pic->f.type || !pic->f.data[0]) {
  202. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %d %p)\n",
  203. r, pic->f.age, pic->f.type, pic->f.data[0]);
  204. av_freep(&pic->f.hwaccel_picture_private);
  205. return -1;
  206. }
  207. if (s->linesize && (s->linesize != pic->f.linesize[0] || s->uvlinesize != pic->f.linesize[1])) {
  208. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (stride changed)\n");
  209. free_frame_buffer(s, pic);
  210. return -1;
  211. }
  212. if (pic->f.linesize[1] != pic->f.linesize[2]) {
  213. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (uv stride mismatch)\n");
  214. free_frame_buffer(s, pic);
  215. return -1;
  216. }
  217. return 0;
  218. }
  219. /**
  220. * allocates a Picture
  221. * The pixels are allocated/set by calling get_buffer() if shared=0
  222. */
  223. int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared){
  224. const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1; //the +1 is needed so memset(,,stride*height) does not sig11
  225. const int mb_array_size= s->mb_stride*s->mb_height;
  226. const int b8_array_size= s->b8_stride*s->mb_height*2;
  227. const int b4_array_size= s->b4_stride*s->mb_height*4;
  228. int i;
  229. int r= -1;
  230. if(shared){
  231. assert(pic->f.data[0]);
  232. assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
  233. pic->f.type = FF_BUFFER_TYPE_SHARED;
  234. }else{
  235. assert(!pic->f.data[0]);
  236. if (alloc_frame_buffer(s, pic) < 0)
  237. return -1;
  238. s->linesize = pic->f.linesize[0];
  239. s->uvlinesize = pic->f.linesize[1];
  240. }
  241. if (pic->f.qscale_table == NULL) {
  242. if (s->encoding) {
  243. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_var , mb_array_size * sizeof(int16_t) , fail)
  244. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mc_mb_var, mb_array_size * sizeof(int16_t) , fail)
  245. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_mean , mb_array_size * sizeof(int8_t ) , fail)
  246. }
  247. FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.mbskip_table, mb_array_size * sizeof(uint8_t) + 2, fail) //the +2 is for the slice end check
  248. FF_ALLOCZ_OR_GOTO(s->avctx, pic->qscale_table_base , (big_mb_num + s->mb_stride) * sizeof(uint8_t) , fail)
  249. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_type_base , (big_mb_num + s->mb_stride) * sizeof(uint32_t), fail)
  250. pic->f.mb_type = pic->mb_type_base + 2*s->mb_stride + 1;
  251. pic->f.qscale_table = pic->qscale_table_base + 2*s->mb_stride + 1;
  252. if(s->out_format == FMT_H264){
  253. for(i=0; i<2; i++){
  254. FF_ALLOCZ_OR_GOTO(s->avctx, pic->motion_val_base[i], 2 * (b4_array_size+4) * sizeof(int16_t), fail)
  255. pic->f.motion_val[i] = pic->motion_val_base[i] + 4;
  256. FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.ref_index[i], 4*mb_array_size * sizeof(uint8_t), fail)
  257. }
  258. pic->f.motion_subsample_log2 = 2;
  259. }else if(s->out_format == FMT_H263 || s->encoding || (s->avctx->debug&FF_DEBUG_MV) || (s->avctx->debug_mv)){
  260. for(i=0; i<2; i++){
  261. FF_ALLOCZ_OR_GOTO(s->avctx, pic->motion_val_base[i], 2 * (b8_array_size+4) * sizeof(int16_t), fail)
  262. pic->f.motion_val[i] = pic->motion_val_base[i] + 4;
  263. FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.ref_index[i], 4*mb_array_size * sizeof(uint8_t), fail)
  264. }
  265. pic->f.motion_subsample_log2 = 3;
  266. }
  267. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  268. FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.dct_coeff, 64 * mb_array_size * sizeof(DCTELEM) * 6, fail)
  269. }
  270. pic->f.qstride = s->mb_stride;
  271. FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.pan_scan , 1 * sizeof(AVPanScan), fail)
  272. }
  273. /* It might be nicer if the application would keep track of these
  274. * but it would require an API change. */
  275. memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
  276. s->prev_pict_types[0]= s->dropable ? AV_PICTURE_TYPE_B : s->pict_type;
  277. if (pic->f.age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->f.age] == AV_PICTURE_TYPE_B)
  278. pic->f.age = INT_MAX; // Skipped MBs in B-frames are quite rare in MPEG-1/2 and it is a bit tricky to skip them anyway.
  279. pic->owner2 = NULL;
  280. return 0;
  281. fail: //for the FF_ALLOCZ_OR_GOTO macro
  282. if(r>=0)
  283. free_frame_buffer(s, pic);
  284. return -1;
  285. }
  286. /**
  287. * deallocates a picture
  288. */
  289. static void free_picture(MpegEncContext *s, Picture *pic){
  290. int i;
  291. if (pic->f.data[0] && pic->f.type != FF_BUFFER_TYPE_SHARED) {
  292. free_frame_buffer(s, pic);
  293. }
  294. av_freep(&pic->mb_var);
  295. av_freep(&pic->mc_mb_var);
  296. av_freep(&pic->mb_mean);
  297. av_freep(&pic->f.mbskip_table);
  298. av_freep(&pic->qscale_table_base);
  299. av_freep(&pic->mb_type_base);
  300. av_freep(&pic->f.dct_coeff);
  301. av_freep(&pic->f.pan_scan);
  302. pic->f.mb_type = NULL;
  303. for(i=0; i<2; i++){
  304. av_freep(&pic->motion_val_base[i]);
  305. av_freep(&pic->f.ref_index[i]);
  306. }
  307. if (pic->f.type == FF_BUFFER_TYPE_SHARED) {
  308. for(i=0; i<4; i++){
  309. pic->f.base[i] =
  310. pic->f.data[i] = NULL;
  311. }
  312. pic->f.type = 0;
  313. }
  314. }
  315. static int init_duplicate_context(MpegEncContext *s, MpegEncContext *base){
  316. int y_size = s->b8_stride * (2 * s->mb_height + 1);
  317. int c_size = s->mb_stride * (s->mb_height + 1);
  318. int yc_size = y_size + 2 * c_size;
  319. int i;
  320. // edge emu needs blocksize + filter length - 1 (=17x17 for halfpel / 21x21 for h264)
  321. FF_ALLOCZ_OR_GOTO(s->avctx, s->allocated_edge_emu_buffer, (s->width+64)*2*21*2, fail); //(width + edge + align)*interlaced*MBsize*tolerance
  322. s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*21;
  323. //FIXME should be linesize instead of s->width*2 but that is not known before get_buffer()
  324. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad, (s->width+64)*4*16*2*sizeof(uint8_t), fail)
  325. s->me.temp= s->me.scratchpad;
  326. s->rd_scratchpad= s->me.scratchpad;
  327. s->b_scratchpad= s->me.scratchpad;
  328. s->obmc_scratchpad= s->me.scratchpad + 16;
  329. if (s->encoding) {
  330. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map , ME_MAP_SIZE*sizeof(uint32_t), fail)
  331. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t), fail)
  332. if(s->avctx->noise_reduction){
  333. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum, 2 * 64 * sizeof(int), fail)
  334. }
  335. }
  336. FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64*12*2 * sizeof(DCTELEM), fail)
  337. s->block= s->blocks[0];
  338. for(i=0;i<12;i++){
  339. s->pblocks[i] = &s->block[i];
  340. }
  341. if (s->out_format == FMT_H263) {
  342. /* ac values */
  343. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base, yc_size * sizeof(int16_t) * 16, fail);
  344. s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
  345. s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
  346. s->ac_val[2] = s->ac_val[1] + c_size;
  347. }
  348. return 0;
  349. fail:
  350. return -1; //free() through MPV_common_end()
  351. }
  352. static void free_duplicate_context(MpegEncContext *s){
  353. if(s==NULL) return;
  354. av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
  355. av_freep(&s->me.scratchpad);
  356. s->me.temp=
  357. s->rd_scratchpad=
  358. s->b_scratchpad=
  359. s->obmc_scratchpad= NULL;
  360. av_freep(&s->dct_error_sum);
  361. av_freep(&s->me.map);
  362. av_freep(&s->me.score_map);
  363. av_freep(&s->blocks);
  364. av_freep(&s->ac_val_base);
  365. s->block= NULL;
  366. }
  367. static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src){
  368. #define COPY(a) bak->a= src->a
  369. COPY(allocated_edge_emu_buffer);
  370. COPY(edge_emu_buffer);
  371. COPY(me.scratchpad);
  372. COPY(me.temp);
  373. COPY(rd_scratchpad);
  374. COPY(b_scratchpad);
  375. COPY(obmc_scratchpad);
  376. COPY(me.map);
  377. COPY(me.score_map);
  378. COPY(blocks);
  379. COPY(block);
  380. COPY(start_mb_y);
  381. COPY(end_mb_y);
  382. COPY(me.map_generation);
  383. COPY(pb);
  384. COPY(dct_error_sum);
  385. COPY(dct_count[0]);
  386. COPY(dct_count[1]);
  387. COPY(ac_val_base);
  388. COPY(ac_val[0]);
  389. COPY(ac_val[1]);
  390. COPY(ac_val[2]);
  391. #undef COPY
  392. }
  393. void ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src){
  394. MpegEncContext bak;
  395. int i;
  396. //FIXME copy only needed parts
  397. //START_TIMER
  398. backup_duplicate_context(&bak, dst);
  399. memcpy(dst, src, sizeof(MpegEncContext));
  400. backup_duplicate_context(dst, &bak);
  401. for(i=0;i<12;i++){
  402. dst->pblocks[i] = &dst->block[i];
  403. }
  404. //STOP_TIMER("update_duplicate_context") //about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
  405. }
  406. int ff_mpeg_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
  407. {
  408. MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
  409. if(dst == src || !s1->context_initialized) return 0;
  410. //FIXME can parameters change on I-frames? in that case dst may need a reinit
  411. if(!s->context_initialized){
  412. memcpy(s, s1, sizeof(MpegEncContext));
  413. s->avctx = dst;
  414. s->picture_range_start += MAX_PICTURE_COUNT;
  415. s->picture_range_end += MAX_PICTURE_COUNT;
  416. s->bitstream_buffer = NULL;
  417. s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
  418. MPV_common_init(s);
  419. }
  420. s->avctx->coded_height = s1->avctx->coded_height;
  421. s->avctx->coded_width = s1->avctx->coded_width;
  422. s->avctx->width = s1->avctx->width;
  423. s->avctx->height = s1->avctx->height;
  424. s->coded_picture_number = s1->coded_picture_number;
  425. s->picture_number = s1->picture_number;
  426. s->input_picture_number = s1->input_picture_number;
  427. memcpy(s->picture, s1->picture, s1->picture_count * sizeof(Picture));
  428. memcpy(&s->last_picture, &s1->last_picture, (char*)&s1->last_picture_ptr - (char*)&s1->last_picture);
  429. s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
  430. s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
  431. s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
  432. memcpy(s->prev_pict_types, s1->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
  433. //Error/bug resilience
  434. s->next_p_frame_damaged = s1->next_p_frame_damaged;
  435. s->workaround_bugs = s1->workaround_bugs;
  436. //MPEG4 timing info
  437. memcpy(&s->time_increment_bits, &s1->time_increment_bits, (char*)&s1->shape - (char*)&s1->time_increment_bits);
  438. //B-frame info
  439. s->max_b_frames = s1->max_b_frames;
  440. s->low_delay = s1->low_delay;
  441. s->dropable = s1->dropable;
  442. //DivX handling (doesn't work)
  443. s->divx_packed = s1->divx_packed;
  444. if(s1->bitstream_buffer){
  445. if (s1->bitstream_buffer_size + FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
  446. av_fast_malloc(&s->bitstream_buffer, &s->allocated_bitstream_buffer_size, s1->allocated_bitstream_buffer_size);
  447. s->bitstream_buffer_size = s1->bitstream_buffer_size;
  448. memcpy(s->bitstream_buffer, s1->bitstream_buffer, s1->bitstream_buffer_size);
  449. memset(s->bitstream_buffer+s->bitstream_buffer_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  450. }
  451. //MPEG2/interlacing info
  452. memcpy(&s->progressive_sequence, &s1->progressive_sequence, (char*)&s1->rtp_mode - (char*)&s1->progressive_sequence);
  453. if(!s1->first_field){
  454. s->last_pict_type= s1->pict_type;
  455. if (s1->current_picture_ptr) s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f.quality;
  456. if(s1->pict_type!=FF_B_TYPE){
  457. s->last_non_b_pict_type= s1->pict_type;
  458. }
  459. }
  460. return 0;
  461. }
  462. /**
  463. * sets the given MpegEncContext to common defaults (same for encoding and decoding).
  464. * the changed fields will not depend upon the prior state of the MpegEncContext.
  465. */
  466. void MPV_common_defaults(MpegEncContext *s){
  467. s->y_dc_scale_table=
  468. s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
  469. s->chroma_qscale_table= ff_default_chroma_qscale_table;
  470. s->progressive_frame= 1;
  471. s->progressive_sequence= 1;
  472. s->picture_structure= PICT_FRAME;
  473. s->coded_picture_number = 0;
  474. s->picture_number = 0;
  475. s->input_picture_number = 0;
  476. s->picture_in_gop_number = 0;
  477. s->f_code = 1;
  478. s->b_code = 1;
  479. s->picture_range_start = 0;
  480. s->picture_range_end = MAX_PICTURE_COUNT;
  481. }
  482. /**
  483. * sets the given MpegEncContext to defaults for decoding.
  484. * the changed fields will not depend upon the prior state of the MpegEncContext.
  485. */
  486. void MPV_decode_defaults(MpegEncContext *s){
  487. MPV_common_defaults(s);
  488. }
  489. /**
  490. * init common structure for both encoder and decoder.
  491. * this assumes that some variables like width/height are already set
  492. */
  493. av_cold int MPV_common_init(MpegEncContext *s)
  494. {
  495. int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y,
  496. threads = (s->encoding ||
  497. (HAVE_THREADS &&
  498. s->avctx->active_thread_type & FF_THREAD_SLICE)) ?
  499. s->avctx->thread_count : 1;
  500. if(s->codec_id == CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  501. s->mb_height = (s->height + 31) / 32 * 2;
  502. else if (s->codec_id != CODEC_ID_H264)
  503. s->mb_height = (s->height + 15) / 16;
  504. if(s->avctx->pix_fmt == PIX_FMT_NONE){
  505. av_log(s->avctx, AV_LOG_ERROR, "decoding to PIX_FMT_NONE is not supported.\n");
  506. return -1;
  507. }
  508. if((s->encoding || (s->avctx->active_thread_type & FF_THREAD_SLICE)) &&
  509. (s->avctx->thread_count > MAX_THREADS || (s->avctx->thread_count > s->mb_height && s->mb_height))){
  510. int max_threads = FFMIN(MAX_THREADS, s->mb_height);
  511. av_log(s->avctx, AV_LOG_WARNING, "too many threads (%d), reducing to %d\n",
  512. s->avctx->thread_count, max_threads);
  513. threads = max_threads;
  514. }
  515. if((s->width || s->height) && av_image_check_size(s->width, s->height, 0, s->avctx))
  516. return -1;
  517. dsputil_init(&s->dsp, s->avctx);
  518. ff_dct_common_init(s);
  519. s->flags= s->avctx->flags;
  520. s->flags2= s->avctx->flags2;
  521. s->mb_width = (s->width + 15) / 16;
  522. s->mb_stride = s->mb_width + 1;
  523. s->b8_stride = s->mb_width*2 + 1;
  524. s->b4_stride = s->mb_width*4 + 1;
  525. mb_array_size= s->mb_height * s->mb_stride;
  526. mv_table_size= (s->mb_height+2) * s->mb_stride + 1;
  527. /* set chroma shifts */
  528. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,&(s->chroma_x_shift),
  529. &(s->chroma_y_shift) );
  530. /* set default edge pos, will be overriden in decode_header if needed */
  531. s->h_edge_pos= s->mb_width*16;
  532. s->v_edge_pos= s->mb_height*16;
  533. s->mb_num = s->mb_width * s->mb_height;
  534. s->block_wrap[0]=
  535. s->block_wrap[1]=
  536. s->block_wrap[2]=
  537. s->block_wrap[3]= s->b8_stride;
  538. s->block_wrap[4]=
  539. s->block_wrap[5]= s->mb_stride;
  540. y_size = s->b8_stride * (2 * s->mb_height + 1);
  541. c_size = s->mb_stride * (s->mb_height + 1);
  542. yc_size = y_size + 2 * c_size;
  543. /* convert fourcc to upper case */
  544. s->codec_tag = ff_toupper4(s->avctx->codec_tag);
  545. s->stream_codec_tag = ff_toupper4(s->avctx->stream_codec_tag);
  546. s->avctx->coded_frame= (AVFrame*)&s->current_picture;
  547. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num+1)*sizeof(int), fail) //error ressilience code looks cleaner with this
  548. for(y=0; y<s->mb_height; y++){
  549. for(x=0; x<s->mb_width; x++){
  550. s->mb_index2xy[ x + y*s->mb_width ] = x + y*s->mb_stride;
  551. }
  552. }
  553. s->mb_index2xy[ s->mb_height*s->mb_width ] = (s->mb_height-1)*s->mb_stride + s->mb_width; //FIXME really needed?
  554. if (s->encoding) {
  555. /* Allocate MV tables */
  556. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  557. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  558. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  559. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  560. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  561. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  562. s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
  563. s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
  564. s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
  565. s->b_bidir_forw_mv_table= s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
  566. s->b_bidir_back_mv_table= s->b_bidir_back_mv_table_base + s->mb_stride + 1;
  567. s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
  568. if(s->msmpeg4_version){
  569. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int), fail);
  570. }
  571. FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
  572. /* Allocate MB type table */
  573. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type , mb_array_size * sizeof(uint16_t), fail) //needed for encoding
  574. FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size * sizeof(int), fail)
  575. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix , 64*32 * sizeof(int), fail)
  576. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix , 64*32 * sizeof(int), fail)
  577. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16, 64*32*2 * sizeof(uint16_t), fail)
  578. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16, 64*32*2 * sizeof(uint16_t), fail)
  579. FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*), fail)
  580. FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*), fail)
  581. if(s->avctx->noise_reduction){
  582. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset, 2 * 64 * sizeof(uint16_t), fail)
  583. }
  584. }
  585. s->picture_count = MAX_PICTURE_COUNT * FFMAX(1, s->avctx->thread_count);
  586. FF_ALLOCZ_OR_GOTO(s->avctx, s->picture, s->picture_count * sizeof(Picture), fail)
  587. for(i = 0; i < s->picture_count; i++) {
  588. avcodec_get_frame_defaults((AVFrame *)&s->picture[i]);
  589. }
  590. FF_ALLOCZ_OR_GOTO(s->avctx, s->error_status_table, mb_array_size*sizeof(uint8_t), fail)
  591. if(s->codec_id==CODEC_ID_MPEG4 || (s->flags & CODEC_FLAG_INTERLACED_ME)){
  592. /* interlaced direct mode decoding tables */
  593. for(i=0; i<2; i++){
  594. int j, k;
  595. for(j=0; j<2; j++){
  596. for(k=0; k<2; k++){
  597. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_mv_table_base[i][j][k], mv_table_size * 2 * sizeof(int16_t), fail)
  598. s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] + s->mb_stride + 1;
  599. }
  600. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j], mb_array_size * 2 * sizeof(uint8_t), fail)
  601. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], mv_table_size * 2 * sizeof(int16_t), fail)
  602. s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]+ s->mb_stride + 1;
  603. }
  604. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], mb_array_size * 2 * sizeof(uint8_t), fail)
  605. }
  606. }
  607. if (s->out_format == FMT_H263) {
  608. /* cbp values */
  609. FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
  610. s->coded_block= s->coded_block_base + s->b8_stride + 1;
  611. /* cbp, ac_pred, pred_dir */
  612. FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table , mb_array_size * sizeof(uint8_t), fail)
  613. FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, mb_array_size * sizeof(uint8_t), fail)
  614. }
  615. if (s->h263_pred || s->h263_plus || !s->encoding) {
  616. /* dc values */
  617. //MN: we need these for error resilience of intra-frames
  618. FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, yc_size * sizeof(int16_t), fail);
  619. s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
  620. s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
  621. s->dc_val[2] = s->dc_val[1] + c_size;
  622. for(i=0;i<yc_size;i++)
  623. s->dc_val_base[i] = 1024;
  624. }
  625. /* which mb is a intra block */
  626. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
  627. memset(s->mbintra_table, 1, mb_array_size);
  628. /* init macroblock skip table */
  629. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size+2, fail);
  630. //Note the +1 is for a quicker mpeg4 slice_end detection
  631. FF_ALLOCZ_OR_GOTO(s->avctx, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE, fail);
  632. s->parse_context.state= -1;
  633. if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
  634. s->visualization_buffer[0] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
  635. s->visualization_buffer[1] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
  636. s->visualization_buffer[2] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
  637. }
  638. s->context_initialized = 1;
  639. s->thread_context[0]= s;
  640. if (s->encoding || (HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_SLICE)) {
  641. for(i=1; i<threads; i++){
  642. s->thread_context[i]= av_malloc(sizeof(MpegEncContext));
  643. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  644. }
  645. for(i=0; i<threads; i++){
  646. if(init_duplicate_context(s->thread_context[i], s) < 0)
  647. goto fail;
  648. s->thread_context[i]->start_mb_y= (s->mb_height*(i ) + s->avctx->thread_count/2) / s->avctx->thread_count;
  649. s->thread_context[i]->end_mb_y = (s->mb_height*(i+1) + s->avctx->thread_count/2) / s->avctx->thread_count;
  650. }
  651. } else {
  652. if(init_duplicate_context(s, s) < 0) goto fail;
  653. s->start_mb_y = 0;
  654. s->end_mb_y = s->mb_height;
  655. }
  656. return 0;
  657. fail:
  658. MPV_common_end(s);
  659. return -1;
  660. }
  661. /* init common structure for both encoder and decoder */
  662. void MPV_common_end(MpegEncContext *s)
  663. {
  664. int i, j, k;
  665. if (s->encoding || (HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_SLICE)) {
  666. for(i=0; i<s->avctx->thread_count; i++){
  667. free_duplicate_context(s->thread_context[i]);
  668. }
  669. for(i=1; i<s->avctx->thread_count; i++){
  670. av_freep(&s->thread_context[i]);
  671. }
  672. } else free_duplicate_context(s);
  673. av_freep(&s->parse_context.buffer);
  674. s->parse_context.buffer_size=0;
  675. av_freep(&s->mb_type);
  676. av_freep(&s->p_mv_table_base);
  677. av_freep(&s->b_forw_mv_table_base);
  678. av_freep(&s->b_back_mv_table_base);
  679. av_freep(&s->b_bidir_forw_mv_table_base);
  680. av_freep(&s->b_bidir_back_mv_table_base);
  681. av_freep(&s->b_direct_mv_table_base);
  682. s->p_mv_table= NULL;
  683. s->b_forw_mv_table= NULL;
  684. s->b_back_mv_table= NULL;
  685. s->b_bidir_forw_mv_table= NULL;
  686. s->b_bidir_back_mv_table= NULL;
  687. s->b_direct_mv_table= NULL;
  688. for(i=0; i<2; i++){
  689. for(j=0; j<2; j++){
  690. for(k=0; k<2; k++){
  691. av_freep(&s->b_field_mv_table_base[i][j][k]);
  692. s->b_field_mv_table[i][j][k]=NULL;
  693. }
  694. av_freep(&s->b_field_select_table[i][j]);
  695. av_freep(&s->p_field_mv_table_base[i][j]);
  696. s->p_field_mv_table[i][j]=NULL;
  697. }
  698. av_freep(&s->p_field_select_table[i]);
  699. }
  700. av_freep(&s->dc_val_base);
  701. av_freep(&s->coded_block_base);
  702. av_freep(&s->mbintra_table);
  703. av_freep(&s->cbp_table);
  704. av_freep(&s->pred_dir_table);
  705. av_freep(&s->mbskip_table);
  706. av_freep(&s->prev_pict_types);
  707. av_freep(&s->bitstream_buffer);
  708. s->allocated_bitstream_buffer_size=0;
  709. av_freep(&s->avctx->stats_out);
  710. av_freep(&s->ac_stats);
  711. av_freep(&s->error_status_table);
  712. av_freep(&s->mb_index2xy);
  713. av_freep(&s->lambda_table);
  714. av_freep(&s->q_intra_matrix);
  715. av_freep(&s->q_inter_matrix);
  716. av_freep(&s->q_intra_matrix16);
  717. av_freep(&s->q_inter_matrix16);
  718. av_freep(&s->input_picture);
  719. av_freep(&s->reordered_input_picture);
  720. av_freep(&s->dct_offset);
  721. if(s->picture && !s->avctx->is_copy){
  722. for(i=0; i<s->picture_count; i++){
  723. free_picture(s, &s->picture[i]);
  724. }
  725. }
  726. av_freep(&s->picture);
  727. s->context_initialized = 0;
  728. s->last_picture_ptr=
  729. s->next_picture_ptr=
  730. s->current_picture_ptr= NULL;
  731. s->linesize= s->uvlinesize= 0;
  732. for(i=0; i<3; i++)
  733. av_freep(&s->visualization_buffer[i]);
  734. if(!(s->avctx->active_thread_type&FF_THREAD_FRAME))
  735. avcodec_default_free_buffers(s->avctx);
  736. }
  737. void init_rl(RLTable *rl, uint8_t static_store[2][2*MAX_RUN + MAX_LEVEL + 3])
  738. {
  739. int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
  740. uint8_t index_run[MAX_RUN+1];
  741. int last, run, level, start, end, i;
  742. /* If table is static, we can quit if rl->max_level[0] is not NULL */
  743. if(static_store && rl->max_level[0])
  744. return;
  745. /* compute max_level[], max_run[] and index_run[] */
  746. for(last=0;last<2;last++) {
  747. if (last == 0) {
  748. start = 0;
  749. end = rl->last;
  750. } else {
  751. start = rl->last;
  752. end = rl->n;
  753. }
  754. memset(max_level, 0, MAX_RUN + 1);
  755. memset(max_run, 0, MAX_LEVEL + 1);
  756. memset(index_run, rl->n, MAX_RUN + 1);
  757. for(i=start;i<end;i++) {
  758. run = rl->table_run[i];
  759. level = rl->table_level[i];
  760. if (index_run[run] == rl->n)
  761. index_run[run] = i;
  762. if (level > max_level[run])
  763. max_level[run] = level;
  764. if (run > max_run[level])
  765. max_run[level] = run;
  766. }
  767. if(static_store)
  768. rl->max_level[last] = static_store[last];
  769. else
  770. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  771. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  772. if(static_store)
  773. rl->max_run[last] = static_store[last] + MAX_RUN + 1;
  774. else
  775. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  776. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  777. if(static_store)
  778. rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
  779. else
  780. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  781. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  782. }
  783. }
  784. void init_vlc_rl(RLTable *rl)
  785. {
  786. int i, q;
  787. for(q=0; q<32; q++){
  788. int qmul= q*2;
  789. int qadd= (q-1)|1;
  790. if(q==0){
  791. qmul=1;
  792. qadd=0;
  793. }
  794. for(i=0; i<rl->vlc.table_size; i++){
  795. int code= rl->vlc.table[i][0];
  796. int len = rl->vlc.table[i][1];
  797. int level, run;
  798. if(len==0){ // illegal code
  799. run= 66;
  800. level= MAX_LEVEL;
  801. }else if(len<0){ //more bits needed
  802. run= 0;
  803. level= code;
  804. }else{
  805. if(code==rl->n){ //esc
  806. run= 66;
  807. level= 0;
  808. }else{
  809. run= rl->table_run [code] + 1;
  810. level= rl->table_level[code] * qmul + qadd;
  811. if(code >= rl->last) run+=192;
  812. }
  813. }
  814. rl->rl_vlc[q][i].len= len;
  815. rl->rl_vlc[q][i].level= level;
  816. rl->rl_vlc[q][i].run= run;
  817. }
  818. }
  819. }
  820. void ff_release_unused_pictures(MpegEncContext *s, int remove_current)
  821. {
  822. int i;
  823. /* release non reference frames */
  824. for(i=0; i<s->picture_count; i++){
  825. if (s->picture[i].f.data[0] && !s->picture[i].f.reference
  826. && (!s->picture[i].owner2 || s->picture[i].owner2 == s)
  827. && (remove_current || &s->picture[i] != s->current_picture_ptr)
  828. /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
  829. free_frame_buffer(s, &s->picture[i]);
  830. }
  831. }
  832. }
  833. int ff_find_unused_picture(MpegEncContext *s, int shared){
  834. int i;
  835. if(shared){
  836. for(i=s->picture_range_start; i<s->picture_range_end; i++){
  837. if (s->picture[i].f.data[0] == NULL && s->picture[i].f.type == 0)
  838. return i;
  839. }
  840. }else{
  841. for(i=s->picture_range_start; i<s->picture_range_end; i++){
  842. if (s->picture[i].f.data[0] == NULL && s->picture[i].f.type != 0)
  843. return i; //FIXME
  844. }
  845. for(i=s->picture_range_start; i<s->picture_range_end; i++){
  846. if (s->picture[i].f.data[0] == NULL)
  847. return i;
  848. }
  849. }
  850. av_log(s->avctx, AV_LOG_FATAL, "Internal error, picture buffer overflow\n");
  851. /* We could return -1, but the codec would crash trying to draw into a
  852. * non-existing frame anyway. This is safer than waiting for a random crash.
  853. * Also the return of this is never useful, an encoder must only allocate
  854. * as much as allowed in the specification. This has no relationship to how
  855. * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
  856. * enough for such valid streams).
  857. * Plus, a decoder has to check stream validity and remove frames if too
  858. * many reference frames are around. Waiting for "OOM" is not correct at
  859. * all. Similarly, missing reference frames have to be replaced by
  860. * interpolated/MC frames, anything else is a bug in the codec ...
  861. */
  862. abort();
  863. return -1;
  864. }
  865. static void update_noise_reduction(MpegEncContext *s){
  866. int intra, i;
  867. for(intra=0; intra<2; intra++){
  868. if(s->dct_count[intra] > (1<<16)){
  869. for(i=0; i<64; i++){
  870. s->dct_error_sum[intra][i] >>=1;
  871. }
  872. s->dct_count[intra] >>= 1;
  873. }
  874. for(i=0; i<64; i++){
  875. s->dct_offset[intra][i]= (s->avctx->noise_reduction * s->dct_count[intra] + s->dct_error_sum[intra][i]/2) / (s->dct_error_sum[intra][i]+1);
  876. }
  877. }
  878. }
  879. /**
  880. * generic function for encode/decode called after coding/decoding the header and before a frame is coded/decoded
  881. */
  882. int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  883. {
  884. int i;
  885. Picture *pic;
  886. s->mb_skipped = 0;
  887. assert(s->last_picture_ptr==NULL || s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3);
  888. /* mark&release old frames */
  889. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr && s->last_picture_ptr != s->next_picture_ptr && s->last_picture_ptr->f.data[0]) {
  890. if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){
  891. free_frame_buffer(s, s->last_picture_ptr);
  892. /* release forgotten pictures */
  893. /* if(mpeg124/h263) */
  894. if(!s->encoding){
  895. for(i=0; i<s->picture_count; i++){
  896. if (s->picture[i].f.data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].f.reference) {
  897. av_log(avctx, AV_LOG_ERROR, "releasing zombie picture\n");
  898. free_frame_buffer(s, &s->picture[i]);
  899. }
  900. }
  901. }
  902. }
  903. }
  904. if(!s->encoding){
  905. ff_release_unused_pictures(s, 1);
  906. if (s->current_picture_ptr && s->current_picture_ptr->f.data[0] == NULL)
  907. pic= s->current_picture_ptr; //we already have a unused image (maybe it was set before reading the header)
  908. else{
  909. i= ff_find_unused_picture(s, 0);
  910. pic= &s->picture[i];
  911. }
  912. pic->f.reference = 0;
  913. if (!s->dropable){
  914. if (s->codec_id == CODEC_ID_H264)
  915. pic->f.reference = s->picture_structure;
  916. else if (s->pict_type != AV_PICTURE_TYPE_B)
  917. pic->f.reference = 3;
  918. }
  919. pic->f.coded_picture_number = s->coded_picture_number++;
  920. if(ff_alloc_picture(s, pic, 0) < 0)
  921. return -1;
  922. s->current_picture_ptr= pic;
  923. //FIXME use only the vars from current_pic
  924. s->current_picture_ptr->f.top_field_first = s->top_field_first;
  925. if(s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO) {
  926. if(s->picture_structure != PICT_FRAME)
  927. s->current_picture_ptr->f.top_field_first = (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
  928. }
  929. s->current_picture_ptr->f.interlaced_frame = !s->progressive_frame && !s->progressive_sequence;
  930. s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
  931. }
  932. s->current_picture_ptr->f.pict_type = s->pict_type;
  933. // if(s->flags && CODEC_FLAG_QSCALE)
  934. // s->current_picture_ptr->quality= s->new_picture_ptr->quality;
  935. s->current_picture_ptr->f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  936. ff_copy_picture(&s->current_picture, s->current_picture_ptr);
  937. if (s->pict_type != AV_PICTURE_TYPE_B) {
  938. s->last_picture_ptr= s->next_picture_ptr;
  939. if(!s->dropable)
  940. s->next_picture_ptr= s->current_picture_ptr;
  941. }
  942. /* av_log(s->avctx, AV_LOG_DEBUG, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n", s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  943. s->last_picture_ptr ? s->last_picture_ptr->f.data[0] : NULL,
  944. s->next_picture_ptr ? s->next_picture_ptr->f.data[0] : NULL,
  945. s->current_picture_ptr ? s->current_picture_ptr->f.data[0] : NULL,
  946. s->pict_type, s->dropable);*/
  947. if(s->codec_id != CODEC_ID_H264){
  948. if ((s->last_picture_ptr == NULL || s->last_picture_ptr->f.data[0] == NULL) &&
  949. (s->pict_type!=AV_PICTURE_TYPE_I || s->picture_structure != PICT_FRAME)){
  950. if (s->pict_type != AV_PICTURE_TYPE_I)
  951. av_log(avctx, AV_LOG_ERROR, "warning: first frame is no keyframe\n");
  952. else if (s->picture_structure != PICT_FRAME)
  953. av_log(avctx, AV_LOG_INFO, "allocate dummy last picture for field based first keyframe\n");
  954. /* Allocate a dummy frame */
  955. i= ff_find_unused_picture(s, 0);
  956. s->last_picture_ptr= &s->picture[i];
  957. s->last_picture_ptr->f.key_frame = 0;
  958. if(ff_alloc_picture(s, s->last_picture_ptr, 0) < 0)
  959. return -1;
  960. ff_thread_report_progress((AVFrame*)s->last_picture_ptr, INT_MAX, 0);
  961. ff_thread_report_progress((AVFrame*)s->last_picture_ptr, INT_MAX, 1);
  962. }
  963. if ((s->next_picture_ptr == NULL || s->next_picture_ptr->f.data[0] == NULL) && s->pict_type == AV_PICTURE_TYPE_B) {
  964. /* Allocate a dummy frame */
  965. i= ff_find_unused_picture(s, 0);
  966. s->next_picture_ptr= &s->picture[i];
  967. s->next_picture_ptr->f.key_frame = 0;
  968. if(ff_alloc_picture(s, s->next_picture_ptr, 0) < 0)
  969. return -1;
  970. ff_thread_report_progress((AVFrame*)s->next_picture_ptr, INT_MAX, 0);
  971. ff_thread_report_progress((AVFrame*)s->next_picture_ptr, INT_MAX, 1);
  972. }
  973. }
  974. if(s->last_picture_ptr) ff_copy_picture(&s->last_picture, s->last_picture_ptr);
  975. if(s->next_picture_ptr) ff_copy_picture(&s->next_picture, s->next_picture_ptr);
  976. assert(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr && s->last_picture_ptr->f.data[0]));
  977. if(s->picture_structure!=PICT_FRAME && s->out_format != FMT_H264){
  978. int i;
  979. for(i=0; i<4; i++){
  980. if(s->picture_structure == PICT_BOTTOM_FIELD){
  981. s->current_picture.f.data[i] += s->current_picture.f.linesize[i];
  982. }
  983. s->current_picture.f.linesize[i] *= 2;
  984. s->last_picture.f.linesize[i] *= 2;
  985. s->next_picture.f.linesize[i] *= 2;
  986. }
  987. }
  988. s->error_recognition= avctx->error_recognition;
  989. /* set dequantizer, we can't do it during init as it might change for mpeg4
  990. and we can't do it in the header decode as init is not called for mpeg4 there yet */
  991. if(s->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO){
  992. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  993. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  994. }else if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
  995. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  996. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  997. }else{
  998. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  999. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1000. }
  1001. if(s->dct_error_sum){
  1002. assert(s->avctx->noise_reduction && s->encoding);
  1003. update_noise_reduction(s);
  1004. }
  1005. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1006. return ff_xvmc_field_start(s, avctx);
  1007. return 0;
  1008. }
  1009. /* generic function for encode/decode called after a frame has been coded/decoded */
  1010. void MPV_frame_end(MpegEncContext *s)
  1011. {
  1012. int i;
  1013. /* redraw edges for the frame if decoding didn't complete */
  1014. //just to make sure that all data is rendered.
  1015. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
  1016. ff_xvmc_field_end(s);
  1017. }else if((s->error_count || s->encoding || !(s->avctx->codec->capabilities&CODEC_CAP_DRAW_HORIZ_BAND))
  1018. && !s->avctx->hwaccel
  1019. && !(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  1020. && s->unrestricted_mv
  1021. && s->current_picture.f.reference
  1022. && !s->intra_only
  1023. && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  1024. int hshift = av_pix_fmt_descriptors[s->avctx->pix_fmt].log2_chroma_w;
  1025. int vshift = av_pix_fmt_descriptors[s->avctx->pix_fmt].log2_chroma_h;
  1026. s->dsp.draw_edges(s->current_picture.f.data[0], s->linesize,
  1027. s->h_edge_pos , s->v_edge_pos,
  1028. EDGE_WIDTH , EDGE_WIDTH , EDGE_TOP | EDGE_BOTTOM);
  1029. s->dsp.draw_edges(s->current_picture.f.data[1], s->uvlinesize,
  1030. s->h_edge_pos>>hshift, s->v_edge_pos>>vshift,
  1031. EDGE_WIDTH>>hshift, EDGE_WIDTH>>vshift, EDGE_TOP | EDGE_BOTTOM);
  1032. s->dsp.draw_edges(s->current_picture.f.data[2], s->uvlinesize,
  1033. s->h_edge_pos>>hshift, s->v_edge_pos>>vshift,
  1034. EDGE_WIDTH>>hshift, EDGE_WIDTH>>vshift, EDGE_TOP | EDGE_BOTTOM);
  1035. }
  1036. emms_c();
  1037. s->last_pict_type = s->pict_type;
  1038. s->last_lambda_for[s->pict_type] = s->current_picture_ptr->f.quality;
  1039. if(s->pict_type!=AV_PICTURE_TYPE_B){
  1040. s->last_non_b_pict_type= s->pict_type;
  1041. }
  1042. #if 0
  1043. /* copy back current_picture variables */
  1044. for(i=0; i<MAX_PICTURE_COUNT; i++){
  1045. if(s->picture[i].f.data[0] == s->current_picture.f.data[0]){
  1046. s->picture[i]= s->current_picture;
  1047. break;
  1048. }
  1049. }
  1050. assert(i<MAX_PICTURE_COUNT);
  1051. #endif
  1052. if(s->encoding){
  1053. /* release non-reference frames */
  1054. for(i=0; i<s->picture_count; i++){
  1055. if (s->picture[i].f.data[0] && !s->picture[i].f.reference /*&& s->picture[i].type != FF_BUFFER_TYPE_SHARED*/) {
  1056. free_frame_buffer(s, &s->picture[i]);
  1057. }
  1058. }
  1059. }
  1060. // clear copies, to avoid confusion
  1061. #if 0
  1062. memset(&s->last_picture, 0, sizeof(Picture));
  1063. memset(&s->next_picture, 0, sizeof(Picture));
  1064. memset(&s->current_picture, 0, sizeof(Picture));
  1065. #endif
  1066. s->avctx->coded_frame= (AVFrame*)s->current_picture_ptr;
  1067. if (s->codec_id != CODEC_ID_H264 && s->current_picture.f.reference) {
  1068. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, s->mb_height-1, 0);
  1069. }
  1070. }
  1071. /**
  1072. * draws an line from (ex, ey) -> (sx, sy).
  1073. * @param w width of the image
  1074. * @param h height of the image
  1075. * @param stride stride/linesize of the image
  1076. * @param color color of the arrow
  1077. */
  1078. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  1079. int x, y, fr, f;
  1080. sx= av_clip(sx, 0, w-1);
  1081. sy= av_clip(sy, 0, h-1);
  1082. ex= av_clip(ex, 0, w-1);
  1083. ey= av_clip(ey, 0, h-1);
  1084. buf[sy*stride + sx]+= color;
  1085. if(FFABS(ex - sx) > FFABS(ey - sy)){
  1086. if(sx > ex){
  1087. FFSWAP(int, sx, ex);
  1088. FFSWAP(int, sy, ey);
  1089. }
  1090. buf+= sx + sy*stride;
  1091. ex-= sx;
  1092. f= ((ey-sy)<<16)/ex;
  1093. for(x= 0; x <= ex; x++){
  1094. y = (x*f)>>16;
  1095. fr= (x*f)&0xFFFF;
  1096. buf[ y *stride + x]+= (color*(0x10000-fr))>>16;
  1097. buf[(y+1)*stride + x]+= (color* fr )>>16;
  1098. }
  1099. }else{
  1100. if(sy > ey){
  1101. FFSWAP(int, sx, ex);
  1102. FFSWAP(int, sy, ey);
  1103. }
  1104. buf+= sx + sy*stride;
  1105. ey-= sy;
  1106. if(ey) f= ((ex-sx)<<16)/ey;
  1107. else f= 0;
  1108. for(y= 0; y <= ey; y++){
  1109. x = (y*f)>>16;
  1110. fr= (y*f)&0xFFFF;
  1111. buf[y*stride + x ]+= (color*(0x10000-fr))>>16;
  1112. buf[y*stride + x+1]+= (color* fr )>>16;
  1113. }
  1114. }
  1115. }
  1116. /**
  1117. * draws an arrow from (ex, ey) -> (sx, sy).
  1118. * @param w width of the image
  1119. * @param h height of the image
  1120. * @param stride stride/linesize of the image
  1121. * @param color color of the arrow
  1122. */
  1123. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  1124. int dx,dy;
  1125. sx= av_clip(sx, -100, w+100);
  1126. sy= av_clip(sy, -100, h+100);
  1127. ex= av_clip(ex, -100, w+100);
  1128. ey= av_clip(ey, -100, h+100);
  1129. dx= ex - sx;
  1130. dy= ey - sy;
  1131. if(dx*dx + dy*dy > 3*3){
  1132. int rx= dx + dy;
  1133. int ry= -dx + dy;
  1134. int length= ff_sqrt((rx*rx + ry*ry)<<8);
  1135. //FIXME subpixel accuracy
  1136. rx= ROUNDED_DIV(rx*3<<4, length);
  1137. ry= ROUNDED_DIV(ry*3<<4, length);
  1138. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  1139. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  1140. }
  1141. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  1142. }
  1143. /**
  1144. * prints debuging info for the given picture.
  1145. */
  1146. void ff_print_debug_info(MpegEncContext *s, AVFrame *pict){
  1147. if(s->avctx->hwaccel || !pict || !pict->mb_type) return;
  1148. if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){
  1149. int x,y;
  1150. av_log(s->avctx, AV_LOG_DEBUG, "New frame, type: %c\n",
  1151. av_get_picture_type_char(pict->pict_type));
  1152. for(y=0; y<s->mb_height; y++){
  1153. for(x=0; x<s->mb_width; x++){
  1154. if(s->avctx->debug&FF_DEBUG_SKIP){
  1155. int count= s->mbskip_table[x + y*s->mb_stride];
  1156. if(count>9) count=9;
  1157. av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
  1158. }
  1159. if(s->avctx->debug&FF_DEBUG_QP){
  1160. av_log(s->avctx, AV_LOG_DEBUG, "%2d", pict->qscale_table[x + y*s->mb_stride]);
  1161. }
  1162. if(s->avctx->debug&FF_DEBUG_MB_TYPE){
  1163. int mb_type= pict->mb_type[x + y*s->mb_stride];
  1164. //Type & MV direction
  1165. if(IS_PCM(mb_type))
  1166. av_log(s->avctx, AV_LOG_DEBUG, "P");
  1167. else if(IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1168. av_log(s->avctx, AV_LOG_DEBUG, "A");
  1169. else if(IS_INTRA4x4(mb_type))
  1170. av_log(s->avctx, AV_LOG_DEBUG, "i");
  1171. else if(IS_INTRA16x16(mb_type))
  1172. av_log(s->avctx, AV_LOG_DEBUG, "I");
  1173. else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1174. av_log(s->avctx, AV_LOG_DEBUG, "d");
  1175. else if(IS_DIRECT(mb_type))
  1176. av_log(s->avctx, AV_LOG_DEBUG, "D");
  1177. else if(IS_GMC(mb_type) && IS_SKIP(mb_type))
  1178. av_log(s->avctx, AV_LOG_DEBUG, "g");
  1179. else if(IS_GMC(mb_type))
  1180. av_log(s->avctx, AV_LOG_DEBUG, "G");
  1181. else if(IS_SKIP(mb_type))
  1182. av_log(s->avctx, AV_LOG_DEBUG, "S");
  1183. else if(!USES_LIST(mb_type, 1))
  1184. av_log(s->avctx, AV_LOG_DEBUG, ">");
  1185. else if(!USES_LIST(mb_type, 0))
  1186. av_log(s->avctx, AV_LOG_DEBUG, "<");
  1187. else{
  1188. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1189. av_log(s->avctx, AV_LOG_DEBUG, "X");
  1190. }
  1191. //segmentation
  1192. if(IS_8X8(mb_type))
  1193. av_log(s->avctx, AV_LOG_DEBUG, "+");
  1194. else if(IS_16X8(mb_type))
  1195. av_log(s->avctx, AV_LOG_DEBUG, "-");
  1196. else if(IS_8X16(mb_type))
  1197. av_log(s->avctx, AV_LOG_DEBUG, "|");
  1198. else if(IS_INTRA(mb_type) || IS_16X16(mb_type))
  1199. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1200. else
  1201. av_log(s->avctx, AV_LOG_DEBUG, "?");
  1202. if(IS_INTERLACED(mb_type))
  1203. av_log(s->avctx, AV_LOG_DEBUG, "=");
  1204. else
  1205. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1206. }
  1207. // av_log(s->avctx, AV_LOG_DEBUG, " ");
  1208. }
  1209. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1210. }
  1211. }
  1212. if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
  1213. const int shift= 1 + s->quarter_sample;
  1214. int mb_y;
  1215. uint8_t *ptr;
  1216. int i;
  1217. int h_chroma_shift, v_chroma_shift, block_height;
  1218. const int width = s->avctx->width;
  1219. const int height= s->avctx->height;
  1220. const int mv_sample_log2= 4 - pict->motion_subsample_log2;
  1221. const int mv_stride= (s->mb_width << mv_sample_log2) + (s->codec_id == CODEC_ID_H264 ? 0 : 1);
  1222. s->low_delay=0; //needed to see the vectors without trashing the buffers
  1223. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1224. for(i=0; i<3; i++){
  1225. memcpy(s->visualization_buffer[i], pict->data[i], (i==0) ? pict->linesize[i]*height:pict->linesize[i]*height >> v_chroma_shift);
  1226. pict->data[i]= s->visualization_buffer[i];
  1227. }
  1228. pict->type= FF_BUFFER_TYPE_COPY;
  1229. pict->opaque= NULL;
  1230. ptr= pict->data[0];
  1231. block_height = 16>>v_chroma_shift;
  1232. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  1233. int mb_x;
  1234. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  1235. const int mb_index= mb_x + mb_y*s->mb_stride;
  1236. if((s->avctx->debug_mv) && pict->motion_val){
  1237. int type;
  1238. for(type=0; type<3; type++){
  1239. int direction = 0;
  1240. switch (type) {
  1241. case 0: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_P_FOR)) || (pict->pict_type!=AV_PICTURE_TYPE_P))
  1242. continue;
  1243. direction = 0;
  1244. break;
  1245. case 1: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_FOR)) || (pict->pict_type!=AV_PICTURE_TYPE_B))
  1246. continue;
  1247. direction = 0;
  1248. break;
  1249. case 2: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_BACK)) || (pict->pict_type!=AV_PICTURE_TYPE_B))
  1250. continue;
  1251. direction = 1;
  1252. break;
  1253. }
  1254. if(!USES_LIST(pict->mb_type[mb_index], direction))
  1255. continue;
  1256. if(IS_8X8(pict->mb_type[mb_index])){
  1257. int i;
  1258. for(i=0; i<4; i++){
  1259. int sx= mb_x*16 + 4 + 8*(i&1);
  1260. int sy= mb_y*16 + 4 + 8*(i>>1);
  1261. int xy= (mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*mv_stride) << (mv_sample_log2-1);
  1262. int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
  1263. int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
  1264. draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
  1265. }
  1266. }else if(IS_16X8(pict->mb_type[mb_index])){
  1267. int i;
  1268. for(i=0; i<2; i++){
  1269. int sx=mb_x*16 + 8;
  1270. int sy=mb_y*16 + 4 + 8*i;
  1271. int xy= (mb_x*2 + (mb_y*2 + i)*mv_stride) << (mv_sample_log2-1);
  1272. int mx=(pict->motion_val[direction][xy][0]>>shift);
  1273. int my=(pict->motion_val[direction][xy][1]>>shift);
  1274. if(IS_INTERLACED(pict->mb_type[mb_index]))
  1275. my*=2;
  1276. draw_arrow(ptr, sx, sy, mx+sx, my+sy, width, height, s->linesize, 100);
  1277. }
  1278. }else if(IS_8X16(pict->mb_type[mb_index])){
  1279. int i;
  1280. for(i=0; i<2; i++){
  1281. int sx=mb_x*16 + 4 + 8*i;
  1282. int sy=mb_y*16 + 8;
  1283. int xy= (mb_x*2 + i + mb_y*2*mv_stride) << (mv_sample_log2-1);
  1284. int mx=(pict->motion_val[direction][xy][0]>>shift);
  1285. int my=(pict->motion_val[direction][xy][1]>>shift);
  1286. if(IS_INTERLACED(pict->mb_type[mb_index]))
  1287. my*=2;
  1288. draw_arrow(ptr, sx, sy, mx+sx, my+sy, width, height, s->linesize, 100);
  1289. }
  1290. }else{
  1291. int sx= mb_x*16 + 8;
  1292. int sy= mb_y*16 + 8;
  1293. int xy= (mb_x + mb_y*mv_stride) << mv_sample_log2;
  1294. int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
  1295. int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
  1296. draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
  1297. }
  1298. }
  1299. }
  1300. if((s->avctx->debug&FF_DEBUG_VIS_QP) && pict->motion_val){
  1301. uint64_t c= (pict->qscale_table[mb_index]*128/31) * 0x0101010101010101ULL;
  1302. int y;
  1303. for(y=0; y<block_height; y++){
  1304. *(uint64_t*)(pict->data[1] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[1])= c;
  1305. *(uint64_t*)(pict->data[2] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[2])= c;
  1306. }
  1307. }
  1308. if((s->avctx->debug&FF_DEBUG_VIS_MB_TYPE) && pict->motion_val){
  1309. int mb_type= pict->mb_type[mb_index];
  1310. uint64_t u,v;
  1311. int y;
  1312. #define COLOR(theta, r)\
  1313. u= (int)(128 + r*cos(theta*3.141592/180));\
  1314. v= (int)(128 + r*sin(theta*3.141592/180));
  1315. u=v=128;
  1316. if(IS_PCM(mb_type)){
  1317. COLOR(120,48)
  1318. }else if((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) || IS_INTRA16x16(mb_type)){
  1319. COLOR(30,48)
  1320. }else if(IS_INTRA4x4(mb_type)){
  1321. COLOR(90,48)
  1322. }else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type)){
  1323. // COLOR(120,48)
  1324. }else if(IS_DIRECT(mb_type)){
  1325. COLOR(150,48)
  1326. }else if(IS_GMC(mb_type) && IS_SKIP(mb_type)){
  1327. COLOR(170,48)
  1328. }else if(IS_GMC(mb_type)){
  1329. COLOR(190,48)
  1330. }else if(IS_SKIP(mb_type)){
  1331. // COLOR(180,48)
  1332. }else if(!USES_LIST(mb_type, 1)){
  1333. COLOR(240,48)
  1334. }else if(!USES_LIST(mb_type, 0)){
  1335. COLOR(0,48)
  1336. }else{
  1337. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1338. COLOR(300,48)
  1339. }
  1340. u*= 0x0101010101010101ULL;
  1341. v*= 0x0101010101010101ULL;
  1342. for(y=0; y<block_height; y++){
  1343. *(uint64_t*)(pict->data[1] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[1])= u;
  1344. *(uint64_t*)(pict->data[2] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[2])= v;
  1345. }
  1346. //segmentation
  1347. if(IS_8X8(mb_type) || IS_16X8(mb_type)){
  1348. *(uint64_t*)(pict->data[0] + 16*mb_x + 0 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
  1349. *(uint64_t*)(pict->data[0] + 16*mb_x + 8 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
  1350. }
  1351. if(IS_8X8(mb_type) || IS_8X16(mb_type)){
  1352. for(y=0; y<16; y++)
  1353. pict->data[0][16*mb_x + 8 + (16*mb_y + y)*pict->linesize[0]]^= 0x80;
  1354. }
  1355. if(IS_8X8(mb_type) && mv_sample_log2 >= 2){
  1356. int dm= 1 << (mv_sample_log2-2);
  1357. for(i=0; i<4; i++){
  1358. int sx= mb_x*16 + 8*(i&1);
  1359. int sy= mb_y*16 + 8*(i>>1);
  1360. int xy= (mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*mv_stride) << (mv_sample_log2-1);
  1361. //FIXME bidir
  1362. int32_t *mv = (int32_t*)&pict->motion_val[0][xy];
  1363. if(mv[0] != mv[dm] || mv[dm*mv_stride] != mv[dm*(mv_stride+1)])
  1364. for(y=0; y<8; y++)
  1365. pict->data[0][sx + 4 + (sy + y)*pict->linesize[0]]^= 0x80;
  1366. if(mv[0] != mv[dm*mv_stride] || mv[dm] != mv[dm*(mv_stride+1)])
  1367. *(uint64_t*)(pict->data[0] + sx + (sy + 4)*pict->linesize[0])^= 0x8080808080808080ULL;
  1368. }
  1369. }
  1370. if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264){
  1371. // hmm
  1372. }
  1373. }
  1374. s->mbskip_table[mb_index]=0;
  1375. }
  1376. }
  1377. }
  1378. }
  1379. static inline int hpel_motion_lowres(MpegEncContext *s,
  1380. uint8_t *dest, uint8_t *src,
  1381. int field_based, int field_select,
  1382. int src_x, int src_y,
  1383. int width, int height, int stride,
  1384. int h_edge_pos, int v_edge_pos,
  1385. int w, int h, h264_chroma_mc_func *pix_op,
  1386. int motion_x, int motion_y)
  1387. {
  1388. const int lowres= s->avctx->lowres;
  1389. const int op_index= FFMIN(lowres, 2);
  1390. const int s_mask= (2<<lowres)-1;
  1391. int emu=0;
  1392. int sx, sy;
  1393. if(s->quarter_sample){
  1394. motion_x/=2;
  1395. motion_y/=2;
  1396. }
  1397. sx= motion_x & s_mask;
  1398. sy= motion_y & s_mask;
  1399. src_x += motion_x >> (lowres+1);
  1400. src_y += motion_y >> (lowres+1);
  1401. src += src_y * stride + src_x;
  1402. if( (unsigned)src_x > h_edge_pos - (!!sx) - w
  1403. || (unsigned)src_y >(v_edge_pos >> field_based) - (!!sy) - h){
  1404. s->dsp.emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w+1, (h+1)<<field_based,
  1405. src_x, src_y<<field_based, h_edge_pos, v_edge_pos);
  1406. src= s->edge_emu_buffer;
  1407. emu=1;
  1408. }
  1409. sx= (sx << 2) >> lowres;
  1410. sy= (sy << 2) >> lowres;
  1411. if(field_select)
  1412. src += s->linesize;
  1413. pix_op[op_index](dest, src, stride, h, sx, sy);
  1414. return emu;
  1415. }
  1416. /* apply one mpeg motion vector to the three components */
  1417. static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
  1418. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1419. int field_based, int bottom_field, int field_select,
  1420. uint8_t **ref_picture, h264_chroma_mc_func *pix_op,
  1421. int motion_x, int motion_y, int h, int mb_y)
  1422. {
  1423. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  1424. int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, uvlinesize, linesize, sx, sy, uvsx, uvsy;
  1425. const int lowres= s->avctx->lowres;
  1426. const int op_index= FFMIN(lowres-1+s->chroma_x_shift, 2);
  1427. const int block_s= 8>>lowres;
  1428. const int s_mask= (2<<lowres)-1;
  1429. const int h_edge_pos = s->h_edge_pos >> lowres;
  1430. const int v_edge_pos = s->v_edge_pos >> lowres;
  1431. linesize = s->current_picture.f.linesize[0] << field_based;
  1432. uvlinesize = s->current_picture.f.linesize[1] << field_based;
  1433. if(s->quarter_sample){ //FIXME obviously not perfect but qpel will not work in lowres anyway
  1434. motion_x/=2;
  1435. motion_y/=2;
  1436. }
  1437. if(field_based){
  1438. motion_y += (bottom_field - field_select)*((1<<lowres)-1);
  1439. }
  1440. sx= motion_x & s_mask;
  1441. sy= motion_y & s_mask;
  1442. src_x = s->mb_x*2*block_s + (motion_x >> (lowres+1));
  1443. src_y =( mb_y*2*block_s>>field_based) + (motion_y >> (lowres+1));
  1444. if (s->out_format == FMT_H263) {
  1445. uvsx = ((motion_x>>1) & s_mask) | (sx&1);
  1446. uvsy = ((motion_y>>1) & s_mask) | (sy&1);
  1447. uvsrc_x = src_x>>1;
  1448. uvsrc_y = src_y>>1;
  1449. }else if(s->out_format == FMT_H261){//even chroma mv's are full pel in H261
  1450. mx = motion_x / 4;
  1451. my = motion_y / 4;
  1452. uvsx = (2*mx) & s_mask;
  1453. uvsy = (2*my) & s_mask;
  1454. uvsrc_x = s->mb_x*block_s + (mx >> lowres);
  1455. uvsrc_y = mb_y*block_s + (my >> lowres);
  1456. } else {
  1457. if(s->chroma_y_shift){
  1458. mx = motion_x / 2;
  1459. my = motion_y / 2;
  1460. uvsx = mx & s_mask;
  1461. uvsy = my & s_mask;
  1462. uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
  1463. uvsrc_y =( mb_y*block_s>>field_based) + (my >> (lowres+1));
  1464. } else {
  1465. if(s->chroma_x_shift){
  1466. //Chroma422
  1467. mx = motion_x / 2;
  1468. uvsx = mx & s_mask;
  1469. uvsy = motion_y & s_mask;
  1470. uvsrc_y = src_y;
  1471. uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
  1472. } else {
  1473. //Chroma444
  1474. uvsx = motion_x & s_mask;
  1475. uvsy = motion_y & s_mask;
  1476. uvsrc_x = src_x;
  1477. uvsrc_y = src_y;
  1478. }
  1479. }
  1480. }
  1481. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  1482. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  1483. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  1484. if( (unsigned)src_x > h_edge_pos - (!!sx) - 2*block_s
  1485. || (unsigned)src_y >(v_edge_pos >> field_based) - (!!sy) - h){
  1486. s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based,
  1487. src_x, src_y<<field_based, h_edge_pos, v_edge_pos);
  1488. ptr_y = s->edge_emu_buffer;
  1489. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1490. uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
  1491. s->dsp.emulated_edge_mc(uvbuf , ptr_cb, s->uvlinesize, 9, 9+field_based,
  1492. uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1);
  1493. s->dsp.emulated_edge_mc(uvbuf+16, ptr_cr, s->uvlinesize, 9, 9+field_based,
  1494. uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1);
  1495. ptr_cb= uvbuf;
  1496. ptr_cr= uvbuf+16;
  1497. }
  1498. }
  1499. if(bottom_field){ //FIXME use this for field pix too instead of the obnoxious hack which changes picture.f.data
  1500. dest_y += s->linesize;
  1501. dest_cb+= s->uvlinesize;
  1502. dest_cr+= s->uvlinesize;
  1503. }
  1504. if(field_select){
  1505. ptr_y += s->linesize;
  1506. ptr_cb+= s->uvlinesize;
  1507. ptr_cr+= s->uvlinesize;
  1508. }
  1509. sx= (sx << 2) >> lowres;
  1510. sy= (sy << 2) >> lowres;
  1511. pix_op[lowres-1](dest_y, ptr_y, linesize, h, sx, sy);
  1512. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1513. uvsx= (uvsx << 2) >> lowres;
  1514. uvsy= (uvsy << 2) >> lowres;
  1515. if(h >> s->chroma_y_shift){
  1516. pix_op[op_index](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
  1517. pix_op[op_index](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
  1518. }
  1519. }
  1520. //FIXME h261 lowres loop filter
  1521. }
  1522. static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
  1523. uint8_t *dest_cb, uint8_t *dest_cr,
  1524. uint8_t **ref_picture,
  1525. h264_chroma_mc_func *pix_op,
  1526. int mx, int my){
  1527. const int lowres= s->avctx->lowres;
  1528. const int op_index= FFMIN(lowres, 2);
  1529. const int block_s= 8>>lowres;
  1530. const int s_mask= (2<<lowres)-1;
  1531. const int h_edge_pos = s->h_edge_pos >> (lowres+1);
  1532. const int v_edge_pos = s->v_edge_pos >> (lowres+1);
  1533. int emu=0, src_x, src_y, offset, sx, sy;
  1534. uint8_t *ptr;
  1535. if(s->quarter_sample){
  1536. mx/=2;
  1537. my/=2;
  1538. }
  1539. /* In case of 8X8, we construct a single chroma motion vector
  1540. with a special rounding */
  1541. mx= ff_h263_round_chroma(mx);
  1542. my= ff_h263_round_chroma(my);
  1543. sx= mx & s_mask;
  1544. sy= my & s_mask;
  1545. src_x = s->mb_x*block_s + (mx >> (lowres+1));
  1546. src_y = s->mb_y*block_s + (my >> (lowres+1));
  1547. offset = src_y * s->uvlinesize + src_x;
  1548. ptr = ref_picture[1] + offset;
  1549. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1550. if( (unsigned)src_x > h_edge_pos - (!!sx) - block_s
  1551. || (unsigned)src_y > v_edge_pos - (!!sy) - block_s){
  1552. s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
  1553. ptr= s->edge_emu_buffer;
  1554. emu=1;
  1555. }
  1556. }
  1557. sx= (sx << 2) >> lowres;
  1558. sy= (sy << 2) >> lowres;
  1559. pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
  1560. ptr = ref_picture[2] + offset;
  1561. if(emu){
  1562. s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
  1563. ptr= s->edge_emu_buffer;
  1564. }
  1565. pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
  1566. }
  1567. /**
  1568. * motion compensation of a single macroblock
  1569. * @param s context
  1570. * @param dest_y luma destination pointer
  1571. * @param dest_cb chroma cb/u destination pointer
  1572. * @param dest_cr chroma cr/v destination pointer
  1573. * @param dir direction (0->forward, 1->backward)
  1574. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  1575. * @param pix_op halfpel motion compensation function (average or put normally)
  1576. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  1577. */
  1578. static inline void MPV_motion_lowres(MpegEncContext *s,
  1579. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1580. int dir, uint8_t **ref_picture,
  1581. h264_chroma_mc_func *pix_op)
  1582. {
  1583. int mx, my;
  1584. int mb_x, mb_y, i;
  1585. const int lowres= s->avctx->lowres;
  1586. const int block_s= 8>>lowres;
  1587. mb_x = s->mb_x;
  1588. mb_y = s->mb_y;
  1589. switch(s->mv_type) {
  1590. case MV_TYPE_16X16:
  1591. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1592. 0, 0, 0,
  1593. ref_picture, pix_op,
  1594. s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s, mb_y);
  1595. break;
  1596. case MV_TYPE_8X8:
  1597. mx = 0;
  1598. my = 0;
  1599. for(i=0;i<4;i++) {
  1600. hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) * s->linesize)*block_s,
  1601. ref_picture[0], 0, 0,
  1602. (2*mb_x + (i & 1))*block_s, (2*mb_y + (i >>1))*block_s,
  1603. s->width, s->height, s->linesize,
  1604. s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
  1605. block_s, block_s, pix_op,
  1606. s->mv[dir][i][0], s->mv[dir][i][1]);
  1607. mx += s->mv[dir][i][0];
  1608. my += s->mv[dir][i][1];
  1609. }
  1610. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY))
  1611. chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture, pix_op, mx, my);
  1612. break;
  1613. case MV_TYPE_FIELD:
  1614. if (s->picture_structure == PICT_FRAME) {
  1615. /* top field */
  1616. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1617. 1, 0, s->field_select[dir][0],
  1618. ref_picture, pix_op,
  1619. s->mv[dir][0][0], s->mv[dir][0][1], block_s, mb_y);
  1620. /* bottom field */
  1621. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1622. 1, 1, s->field_select[dir][1],
  1623. ref_picture, pix_op,
  1624. s->mv[dir][1][0], s->mv[dir][1][1], block_s, mb_y);
  1625. } else {
  1626. if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != AV_PICTURE_TYPE_B && !s->first_field){
  1627. ref_picture = s->current_picture_ptr->f.data;
  1628. }
  1629. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1630. 0, 0, s->field_select[dir][0],
  1631. ref_picture, pix_op,
  1632. s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s, mb_y>>1);
  1633. }
  1634. break;
  1635. case MV_TYPE_16X8:
  1636. for(i=0; i<2; i++){
  1637. uint8_t ** ref2picture;
  1638. if(s->picture_structure == s->field_select[dir][i] + 1 || s->pict_type == AV_PICTURE_TYPE_B || s->first_field){
  1639. ref2picture= ref_picture;
  1640. }else{
  1641. ref2picture = s->current_picture_ptr->f.data;
  1642. }
  1643. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1644. 0, 0, s->field_select[dir][i],
  1645. ref2picture, pix_op,
  1646. s->mv[dir][i][0], s->mv[dir][i][1] + 2*block_s*i, block_s, mb_y>>1);
  1647. dest_y += 2*block_s*s->linesize;
  1648. dest_cb+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize;
  1649. dest_cr+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize;
  1650. }
  1651. break;
  1652. case MV_TYPE_DMV:
  1653. if(s->picture_structure == PICT_FRAME){
  1654. for(i=0; i<2; i++){
  1655. int j;
  1656. for(j=0; j<2; j++){
  1657. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1658. 1, j, j^i,
  1659. ref_picture, pix_op,
  1660. s->mv[dir][2*i + j][0], s->mv[dir][2*i + j][1], block_s, mb_y);
  1661. }
  1662. pix_op = s->dsp.avg_h264_chroma_pixels_tab;
  1663. }
  1664. }else{
  1665. for(i=0; i<2; i++){
  1666. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1667. 0, 0, s->picture_structure != i+1,
  1668. ref_picture, pix_op,
  1669. s->mv[dir][2*i][0],s->mv[dir][2*i][1],2*block_s, mb_y>>1);
  1670. // after put we make avg of the same block
  1671. pix_op = s->dsp.avg_h264_chroma_pixels_tab;
  1672. //opposite parity is always in the same frame if this is second field
  1673. if(!s->first_field){
  1674. ref_picture = s->current_picture_ptr->f.data;
  1675. }
  1676. }
  1677. }
  1678. break;
  1679. default: assert(0);
  1680. }
  1681. }
  1682. /**
  1683. * find the lowest MB row referenced in the MVs
  1684. */
  1685. int MPV_lowest_referenced_row(MpegEncContext *s, int dir)
  1686. {
  1687. int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
  1688. int my, off, i, mvs;
  1689. if (s->picture_structure != PICT_FRAME) goto unhandled;
  1690. switch (s->mv_type) {
  1691. case MV_TYPE_16X16:
  1692. mvs = 1;
  1693. break;
  1694. case MV_TYPE_16X8:
  1695. mvs = 2;
  1696. break;
  1697. case MV_TYPE_8X8:
  1698. mvs = 4;
  1699. break;
  1700. default:
  1701. goto unhandled;
  1702. }
  1703. for (i = 0; i < mvs; i++) {
  1704. my = s->mv[dir][i][1]<<qpel_shift;
  1705. my_max = FFMAX(my_max, my);
  1706. my_min = FFMIN(my_min, my);
  1707. }
  1708. off = (FFMAX(-my_min, my_max) + 63) >> 6;
  1709. return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
  1710. unhandled:
  1711. return s->mb_height-1;
  1712. }
  1713. /* put block[] to dest[] */
  1714. static inline void put_dct(MpegEncContext *s,
  1715. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  1716. {
  1717. s->dct_unquantize_intra(s, block, i, qscale);
  1718. s->dsp.idct_put (dest, line_size, block);
  1719. }
  1720. /* add block[] to dest[] */
  1721. static inline void add_dct(MpegEncContext *s,
  1722. DCTELEM *block, int i, uint8_t *dest, int line_size)
  1723. {
  1724. if (s->block_last_index[i] >= 0) {
  1725. s->dsp.idct_add (dest, line_size, block);
  1726. }
  1727. }
  1728. static inline void add_dequant_dct(MpegEncContext *s,
  1729. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  1730. {
  1731. if (s->block_last_index[i] >= 0) {
  1732. s->dct_unquantize_inter(s, block, i, qscale);
  1733. s->dsp.idct_add (dest, line_size, block);
  1734. }
  1735. }
  1736. /**
  1737. * cleans dc, ac, coded_block for the current non intra MB
  1738. */
  1739. void ff_clean_intra_table_entries(MpegEncContext *s)
  1740. {
  1741. int wrap = s->b8_stride;
  1742. int xy = s->block_index[0];
  1743. s->dc_val[0][xy ] =
  1744. s->dc_val[0][xy + 1 ] =
  1745. s->dc_val[0][xy + wrap] =
  1746. s->dc_val[0][xy + 1 + wrap] = 1024;
  1747. /* ac pred */
  1748. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  1749. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  1750. if (s->msmpeg4_version>=3) {
  1751. s->coded_block[xy ] =
  1752. s->coded_block[xy + 1 ] =
  1753. s->coded_block[xy + wrap] =
  1754. s->coded_block[xy + 1 + wrap] = 0;
  1755. }
  1756. /* chroma */
  1757. wrap = s->mb_stride;
  1758. xy = s->mb_x + s->mb_y * wrap;
  1759. s->dc_val[1][xy] =
  1760. s->dc_val[2][xy] = 1024;
  1761. /* ac pred */
  1762. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  1763. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  1764. s->mbintra_table[xy]= 0;
  1765. }
  1766. /* generic function called after a macroblock has been parsed by the
  1767. decoder or after it has been encoded by the encoder.
  1768. Important variables used:
  1769. s->mb_intra : true if intra macroblock
  1770. s->mv_dir : motion vector direction
  1771. s->mv_type : motion vector type
  1772. s->mv : motion vector
  1773. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1774. */
  1775. static av_always_inline
  1776. void MPV_decode_mb_internal(MpegEncContext *s, DCTELEM block[12][64],
  1777. int lowres_flag, int is_mpeg12)
  1778. {
  1779. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  1780. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
  1781. ff_xvmc_decode_mb(s);//xvmc uses pblocks
  1782. return;
  1783. }
  1784. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  1785. /* save DCT coefficients */
  1786. int i,j;
  1787. DCTELEM *dct = &s->current_picture.f.dct_coeff[mb_xy * 64 * 6];
  1788. av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
  1789. for(i=0; i<6; i++){
  1790. for(j=0; j<64; j++){
  1791. *dct++ = block[i][s->dsp.idct_permutation[j]];
  1792. av_log(s->avctx, AV_LOG_DEBUG, "%5d", dct[-1]);
  1793. }
  1794. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1795. }
  1796. }
  1797. s->current_picture.f.qscale_table[mb_xy] = s->qscale;
  1798. /* update DC predictors for P macroblocks */
  1799. if (!s->mb_intra) {
  1800. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  1801. if(s->mbintra_table[mb_xy])
  1802. ff_clean_intra_table_entries(s);
  1803. } else {
  1804. s->last_dc[0] =
  1805. s->last_dc[1] =
  1806. s->last_dc[2] = 128 << s->intra_dc_precision;
  1807. }
  1808. }
  1809. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  1810. s->mbintra_table[mb_xy]=1;
  1811. if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==AV_PICTURE_TYPE_B) && s->avctx->mb_decision != FF_MB_DECISION_RD)) { //FIXME precalc
  1812. uint8_t *dest_y, *dest_cb, *dest_cr;
  1813. int dct_linesize, dct_offset;
  1814. op_pixels_func (*op_pix)[4];
  1815. qpel_mc_func (*op_qpix)[16];
  1816. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  1817. const int uvlinesize = s->current_picture.f.linesize[1];
  1818. const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
  1819. const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
  1820. /* avoid copy if macroblock skipped in last frame too */
  1821. /* skip only during decoding as we might trash the buffers during encoding a bit */
  1822. if(!s->encoding){
  1823. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  1824. const int age = s->current_picture.f.age;
  1825. assert(age);
  1826. if (s->mb_skipped) {
  1827. s->mb_skipped= 0;
  1828. assert(s->pict_type!=AV_PICTURE_TYPE_I);
  1829. (*mbskip_ptr) ++; /* indicate that this time we skipped it */
  1830. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  1831. /* if previous was skipped too, then nothing to do ! */
  1832. if (*mbskip_ptr >= age && s->current_picture.f.reference){
  1833. return;
  1834. }
  1835. } else if(!s->current_picture.f.reference) {
  1836. (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
  1837. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  1838. } else{
  1839. *mbskip_ptr = 0; /* not skipped */
  1840. }
  1841. }
  1842. dct_linesize = linesize << s->interlaced_dct;
  1843. dct_offset =(s->interlaced_dct)? linesize : linesize*block_size;
  1844. if(readable){
  1845. dest_y= s->dest[0];
  1846. dest_cb= s->dest[1];
  1847. dest_cr= s->dest[2];
  1848. }else{
  1849. dest_y = s->b_scratchpad;
  1850. dest_cb= s->b_scratchpad+16*linesize;
  1851. dest_cr= s->b_scratchpad+32*linesize;
  1852. }
  1853. if (!s->mb_intra) {
  1854. /* motion handling */
  1855. /* decoding or more than one mb_type (MC was already done otherwise) */
  1856. if(!s->encoding){
  1857. if(HAVE_PTHREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  1858. if (s->mv_dir & MV_DIR_FORWARD) {
  1859. ff_thread_await_progress((AVFrame*)s->last_picture_ptr, MPV_lowest_referenced_row(s, 0), 0);
  1860. }
  1861. if (s->mv_dir & MV_DIR_BACKWARD) {
  1862. ff_thread_await_progress((AVFrame*)s->next_picture_ptr, MPV_lowest_referenced_row(s, 1), 0);
  1863. }
  1864. }
  1865. if(lowres_flag){
  1866. h264_chroma_mc_func *op_pix = s->dsp.put_h264_chroma_pixels_tab;
  1867. if (s->mv_dir & MV_DIR_FORWARD) {
  1868. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix);
  1869. op_pix = s->dsp.avg_h264_chroma_pixels_tab;
  1870. }
  1871. if (s->mv_dir & MV_DIR_BACKWARD) {
  1872. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix);
  1873. }
  1874. }else{
  1875. op_qpix= s->me.qpel_put;
  1876. if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  1877. op_pix = s->dsp.put_pixels_tab;
  1878. }else{
  1879. op_pix = s->dsp.put_no_rnd_pixels_tab;
  1880. }
  1881. if (s->mv_dir & MV_DIR_FORWARD) {
  1882. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix);
  1883. op_pix = s->dsp.avg_pixels_tab;
  1884. op_qpix= s->me.qpel_avg;
  1885. }
  1886. if (s->mv_dir & MV_DIR_BACKWARD) {
  1887. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix);
  1888. }
  1889. }
  1890. }
  1891. /* skip dequant / idct if we are really late ;) */
  1892. if(s->avctx->skip_idct){
  1893. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
  1894. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
  1895. || s->avctx->skip_idct >= AVDISCARD_ALL)
  1896. goto skip_idct;
  1897. }
  1898. /* add dct residue */
  1899. if(s->encoding || !( s->msmpeg4_version || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO
  1900. || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
  1901. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1902. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1903. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1904. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1905. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1906. if (s->chroma_y_shift){
  1907. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1908. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1909. }else{
  1910. dct_linesize >>= 1;
  1911. dct_offset >>=1;
  1912. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1913. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1914. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1915. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1916. }
  1917. }
  1918. } else if(is_mpeg12 || (s->codec_id != CODEC_ID_WMV2)){
  1919. add_dct(s, block[0], 0, dest_y , dct_linesize);
  1920. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  1921. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  1922. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  1923. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1924. if(s->chroma_y_shift){//Chroma420
  1925. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  1926. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  1927. }else{
  1928. //chroma422
  1929. dct_linesize = uvlinesize << s->interlaced_dct;
  1930. dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*block_size;
  1931. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  1932. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  1933. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  1934. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  1935. if(!s->chroma_x_shift){//Chroma444
  1936. add_dct(s, block[8], 8, dest_cb+block_size, dct_linesize);
  1937. add_dct(s, block[9], 9, dest_cr+block_size, dct_linesize);
  1938. add_dct(s, block[10], 10, dest_cb+block_size+dct_offset, dct_linesize);
  1939. add_dct(s, block[11], 11, dest_cr+block_size+dct_offset, dct_linesize);
  1940. }
  1941. }
  1942. }//fi gray
  1943. }
  1944. else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
  1945. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  1946. }
  1947. } else {
  1948. /* dct only in intra block */
  1949. if(s->encoding || !(s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO)){
  1950. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1951. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1952. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1953. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1954. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1955. if(s->chroma_y_shift){
  1956. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1957. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1958. }else{
  1959. dct_offset >>=1;
  1960. dct_linesize >>=1;
  1961. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1962. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1963. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1964. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1965. }
  1966. }
  1967. }else{
  1968. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  1969. s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  1970. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  1971. s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  1972. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1973. if(s->chroma_y_shift){
  1974. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  1975. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  1976. }else{
  1977. dct_linesize = uvlinesize << s->interlaced_dct;
  1978. dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*block_size;
  1979. s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
  1980. s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
  1981. s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  1982. s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  1983. if(!s->chroma_x_shift){//Chroma444
  1984. s->dsp.idct_put(dest_cb + block_size, dct_linesize, block[8]);
  1985. s->dsp.idct_put(dest_cr + block_size, dct_linesize, block[9]);
  1986. s->dsp.idct_put(dest_cb + block_size + dct_offset, dct_linesize, block[10]);
  1987. s->dsp.idct_put(dest_cr + block_size + dct_offset, dct_linesize, block[11]);
  1988. }
  1989. }
  1990. }//gray
  1991. }
  1992. }
  1993. skip_idct:
  1994. if(!readable){
  1995. s->dsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  1996. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  1997. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  1998. }
  1999. }
  2000. }
  2001. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[12][64]){
  2002. #if !CONFIG_SMALL
  2003. if(s->out_format == FMT_MPEG1) {
  2004. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 1);
  2005. else MPV_decode_mb_internal(s, block, 0, 1);
  2006. } else
  2007. #endif
  2008. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 0);
  2009. else MPV_decode_mb_internal(s, block, 0, 0);
  2010. }
  2011. /**
  2012. *
  2013. * @param h is the normal height, this will be reduced automatically if needed for the last row
  2014. */
  2015. void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
  2016. const int field_pic= s->picture_structure != PICT_FRAME;
  2017. if(field_pic){
  2018. h <<= 1;
  2019. y <<= 1;
  2020. }
  2021. if (!s->avctx->hwaccel
  2022. && !(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  2023. && s->unrestricted_mv
  2024. && s->current_picture.f.reference
  2025. && !s->intra_only
  2026. && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  2027. int sides = 0, edge_h;
  2028. int hshift = av_pix_fmt_descriptors[s->avctx->pix_fmt].log2_chroma_w;
  2029. int vshift = av_pix_fmt_descriptors[s->avctx->pix_fmt].log2_chroma_h;
  2030. if (y==0) sides |= EDGE_TOP;
  2031. if (y + h >= s->v_edge_pos) sides |= EDGE_BOTTOM;
  2032. edge_h= FFMIN(h, s->v_edge_pos - y);
  2033. s->dsp.draw_edges(s->current_picture_ptr->f.data[0] + y *s->linesize , s->linesize,
  2034. s->h_edge_pos , edge_h , EDGE_WIDTH , EDGE_WIDTH , sides);
  2035. s->dsp.draw_edges(s->current_picture_ptr->f.data[1] + (y>>vshift)*s->uvlinesize, s->uvlinesize,
  2036. s->h_edge_pos>>hshift, edge_h>>hshift, EDGE_WIDTH>>hshift, EDGE_WIDTH>>vshift, sides);
  2037. s->dsp.draw_edges(s->current_picture_ptr->f.data[2] + (y>>vshift)*s->uvlinesize, s->uvlinesize,
  2038. s->h_edge_pos>>hshift, edge_h>>hshift, EDGE_WIDTH>>hshift, EDGE_WIDTH>>vshift, sides);
  2039. }
  2040. h= FFMIN(h, s->avctx->height - y);
  2041. if(field_pic && s->first_field && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  2042. if (s->avctx->draw_horiz_band) {
  2043. AVFrame *src;
  2044. int offset[4];
  2045. if(s->pict_type==AV_PICTURE_TYPE_B || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
  2046. src= (AVFrame*)s->current_picture_ptr;
  2047. else if(s->last_picture_ptr)
  2048. src= (AVFrame*)s->last_picture_ptr;
  2049. else
  2050. return;
  2051. if(s->pict_type==AV_PICTURE_TYPE_B && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){
  2052. offset[0]=
  2053. offset[1]=
  2054. offset[2]=
  2055. offset[3]= 0;
  2056. }else{
  2057. offset[0]= y * s->linesize;
  2058. offset[1]=
  2059. offset[2]= (y >> s->chroma_y_shift) * s->uvlinesize;
  2060. offset[3]= 0;
  2061. }
  2062. emms_c();
  2063. s->avctx->draw_horiz_band(s->avctx, src, offset,
  2064. y, s->picture_structure, h);
  2065. }
  2066. }
  2067. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  2068. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  2069. const int uvlinesize = s->current_picture.f.linesize[1];
  2070. const int mb_size= 4 - s->avctx->lowres;
  2071. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  2072. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  2073. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  2074. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  2075. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2076. s->block_index[5]= s->mb_stride*(s->mb_y + s->mb_height + 2) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2077. //block_index is not used by mpeg2, so it is not affected by chroma_format
  2078. s->dest[0] = s->current_picture.f.data[0] + ((s->mb_x - 1) << mb_size);
  2079. s->dest[1] = s->current_picture.f.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2080. s->dest[2] = s->current_picture.f.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2081. if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  2082. {
  2083. if(s->picture_structure==PICT_FRAME){
  2084. s->dest[0] += s->mb_y * linesize << mb_size;
  2085. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2086. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2087. }else{
  2088. s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
  2089. s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2090. s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2091. assert((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  2092. }
  2093. }
  2094. }
  2095. void ff_mpeg_flush(AVCodecContext *avctx){
  2096. int i;
  2097. MpegEncContext *s = avctx->priv_data;
  2098. if(s==NULL || s->picture==NULL)
  2099. return;
  2100. for(i=0; i<s->picture_count; i++){
  2101. if (s->picture[i].f.data[0] &&
  2102. (s->picture[i].f.type == FF_BUFFER_TYPE_INTERNAL ||
  2103. s->picture[i].f.type == FF_BUFFER_TYPE_USER))
  2104. free_frame_buffer(s, &s->picture[i]);
  2105. }
  2106. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  2107. s->mb_x= s->mb_y= 0;
  2108. s->closed_gop= 0;
  2109. s->parse_context.state= -1;
  2110. s->parse_context.frame_start_found= 0;
  2111. s->parse_context.overread= 0;
  2112. s->parse_context.overread_index= 0;
  2113. s->parse_context.index= 0;
  2114. s->parse_context.last_index= 0;
  2115. s->bitstream_buffer_size=0;
  2116. s->pp_time=0;
  2117. }
  2118. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  2119. DCTELEM *block, int n, int qscale)
  2120. {
  2121. int i, level, nCoeffs;
  2122. const uint16_t *quant_matrix;
  2123. nCoeffs= s->block_last_index[n];
  2124. if (n < 4)
  2125. block[0] = block[0] * s->y_dc_scale;
  2126. else
  2127. block[0] = block[0] * s->c_dc_scale;
  2128. /* XXX: only mpeg1 */
  2129. quant_matrix = s->intra_matrix;
  2130. for(i=1;i<=nCoeffs;i++) {
  2131. int j= s->intra_scantable.permutated[i];
  2132. level = block[j];
  2133. if (level) {
  2134. if (level < 0) {
  2135. level = -level;
  2136. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2137. level = (level - 1) | 1;
  2138. level = -level;
  2139. } else {
  2140. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2141. level = (level - 1) | 1;
  2142. }
  2143. block[j] = level;
  2144. }
  2145. }
  2146. }
  2147. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  2148. DCTELEM *block, int n, int qscale)
  2149. {
  2150. int i, level, nCoeffs;
  2151. const uint16_t *quant_matrix;
  2152. nCoeffs= s->block_last_index[n];
  2153. quant_matrix = s->inter_matrix;
  2154. for(i=0; i<=nCoeffs; i++) {
  2155. int j= s->intra_scantable.permutated[i];
  2156. level = block[j];
  2157. if (level) {
  2158. if (level < 0) {
  2159. level = -level;
  2160. level = (((level << 1) + 1) * qscale *
  2161. ((int) (quant_matrix[j]))) >> 4;
  2162. level = (level - 1) | 1;
  2163. level = -level;
  2164. } else {
  2165. level = (((level << 1) + 1) * qscale *
  2166. ((int) (quant_matrix[j]))) >> 4;
  2167. level = (level - 1) | 1;
  2168. }
  2169. block[j] = level;
  2170. }
  2171. }
  2172. }
  2173. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  2174. DCTELEM *block, int n, int qscale)
  2175. {
  2176. int i, level, nCoeffs;
  2177. const uint16_t *quant_matrix;
  2178. if(s->alternate_scan) nCoeffs= 63;
  2179. else nCoeffs= s->block_last_index[n];
  2180. if (n < 4)
  2181. block[0] = block[0] * s->y_dc_scale;
  2182. else
  2183. block[0] = block[0] * s->c_dc_scale;
  2184. quant_matrix = s->intra_matrix;
  2185. for(i=1;i<=nCoeffs;i++) {
  2186. int j= s->intra_scantable.permutated[i];
  2187. level = block[j];
  2188. if (level) {
  2189. if (level < 0) {
  2190. level = -level;
  2191. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2192. level = -level;
  2193. } else {
  2194. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2195. }
  2196. block[j] = level;
  2197. }
  2198. }
  2199. }
  2200. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  2201. DCTELEM *block, int n, int qscale)
  2202. {
  2203. int i, level, nCoeffs;
  2204. const uint16_t *quant_matrix;
  2205. int sum=-1;
  2206. if(s->alternate_scan) nCoeffs= 63;
  2207. else nCoeffs= s->block_last_index[n];
  2208. if (n < 4)
  2209. block[0] = block[0] * s->y_dc_scale;
  2210. else
  2211. block[0] = block[0] * s->c_dc_scale;
  2212. quant_matrix = s->intra_matrix;
  2213. for(i=1;i<=nCoeffs;i++) {
  2214. int j= s->intra_scantable.permutated[i];
  2215. level = block[j];
  2216. if (level) {
  2217. if (level < 0) {
  2218. level = -level;
  2219. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2220. level = -level;
  2221. } else {
  2222. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2223. }
  2224. block[j] = level;
  2225. sum+=level;
  2226. }
  2227. }
  2228. block[63]^=sum&1;
  2229. }
  2230. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  2231. DCTELEM *block, int n, int qscale)
  2232. {
  2233. int i, level, nCoeffs;
  2234. const uint16_t *quant_matrix;
  2235. int sum=-1;
  2236. if(s->alternate_scan) nCoeffs= 63;
  2237. else nCoeffs= s->block_last_index[n];
  2238. quant_matrix = s->inter_matrix;
  2239. for(i=0; i<=nCoeffs; i++) {
  2240. int j= s->intra_scantable.permutated[i];
  2241. level = block[j];
  2242. if (level) {
  2243. if (level < 0) {
  2244. level = -level;
  2245. level = (((level << 1) + 1) * qscale *
  2246. ((int) (quant_matrix[j]))) >> 4;
  2247. level = -level;
  2248. } else {
  2249. level = (((level << 1) + 1) * qscale *
  2250. ((int) (quant_matrix[j]))) >> 4;
  2251. }
  2252. block[j] = level;
  2253. sum+=level;
  2254. }
  2255. }
  2256. block[63]^=sum&1;
  2257. }
  2258. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  2259. DCTELEM *block, int n, int qscale)
  2260. {
  2261. int i, level, qmul, qadd;
  2262. int nCoeffs;
  2263. assert(s->block_last_index[n]>=0);
  2264. qmul = qscale << 1;
  2265. if (!s->h263_aic) {
  2266. if (n < 4)
  2267. block[0] = block[0] * s->y_dc_scale;
  2268. else
  2269. block[0] = block[0] * s->c_dc_scale;
  2270. qadd = (qscale - 1) | 1;
  2271. }else{
  2272. qadd = 0;
  2273. }
  2274. if(s->ac_pred)
  2275. nCoeffs=63;
  2276. else
  2277. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2278. for(i=1; i<=nCoeffs; i++) {
  2279. level = block[i];
  2280. if (level) {
  2281. if (level < 0) {
  2282. level = level * qmul - qadd;
  2283. } else {
  2284. level = level * qmul + qadd;
  2285. }
  2286. block[i] = level;
  2287. }
  2288. }
  2289. }
  2290. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  2291. DCTELEM *block, int n, int qscale)
  2292. {
  2293. int i, level, qmul, qadd;
  2294. int nCoeffs;
  2295. assert(s->block_last_index[n]>=0);
  2296. qadd = (qscale - 1) | 1;
  2297. qmul = qscale << 1;
  2298. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2299. for(i=0; i<=nCoeffs; i++) {
  2300. level = block[i];
  2301. if (level) {
  2302. if (level < 0) {
  2303. level = level * qmul - qadd;
  2304. } else {
  2305. level = level * qmul + qadd;
  2306. }
  2307. block[i] = level;
  2308. }
  2309. }
  2310. }
  2311. /**
  2312. * set qscale and update qscale dependent variables.
  2313. */
  2314. void ff_set_qscale(MpegEncContext * s, int qscale)
  2315. {
  2316. if (qscale < 1)
  2317. qscale = 1;
  2318. else if (qscale > 31)
  2319. qscale = 31;
  2320. s->qscale = qscale;
  2321. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2322. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2323. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2324. }
  2325. void MPV_report_decode_progress(MpegEncContext *s)
  2326. {
  2327. if (s->pict_type != FF_B_TYPE && !s->partitioned_frame && !s->error_occurred)
  2328. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, s->mb_y, 0);
  2329. }