dvb_demux.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517
  1. /*
  2. * dvb_demux.c - DVB kernel demux API
  3. *
  4. * Copyright (C) 2000-2001 Ralph Metzler <[email protected]>
  5. * & Marcus Metzler <[email protected]>
  6. * for convergence integrated media GmbH
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * as published by the Free Software Foundation; either version 2.1
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. *
  22. */
  23. #include <linux/sched.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/slab.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/module.h>
  28. #include <linux/poll.h>
  29. #include <linux/string.h>
  30. #include <linux/crc32.h>
  31. #include <asm/uaccess.h>
  32. #include <asm/div64.h>
  33. #include "dvb_demux.h"
  34. #define NOBUFS
  35. /*
  36. ** #define DVB_DEMUX_SECTION_LOSS_LOG to monitor payload loss in the syslog
  37. */
  38. // #define DVB_DEMUX_SECTION_LOSS_LOG
  39. static int dvb_demux_tscheck;
  40. module_param(dvb_demux_tscheck, int, 0644);
  41. MODULE_PARM_DESC(dvb_demux_tscheck,
  42. "enable transport stream continuity and TEI check");
  43. static int dvb_demux_speedcheck;
  44. module_param(dvb_demux_speedcheck, int, 0644);
  45. MODULE_PARM_DESC(dvb_demux_speedcheck,
  46. "enable transport stream speed check");
  47. static int dvb_demux_feed_err_pkts = 1;
  48. module_param(dvb_demux_feed_err_pkts, int, 0644);
  49. MODULE_PARM_DESC(dvb_demux_feed_err_pkts,
  50. "when set to 0, drop packets with the TEI bit set (1 by default)");
  51. /* counter advancing for each new dvb-demux device */
  52. static int dvb_demux_index;
  53. static int dvb_demux_performancecheck;
  54. module_param(dvb_demux_performancecheck, int, 0644);
  55. MODULE_PARM_DESC(dvb_demux_performancecheck,
  56. "enable transport stream performance check, reported through debugfs");
  57. #define dprintk_tscheck(x...) do { \
  58. if (dvb_demux_tscheck && printk_ratelimit()) \
  59. printk(x); \
  60. } while (0)
  61. static const struct dvb_dmx_video_patterns mpeg2_seq_hdr = {
  62. {0x00, 0x00, 0x01, 0xB3},
  63. {0xFF, 0xFF, 0xFF, 0xFF},
  64. 4,
  65. DMX_IDX_MPEG_SEQ_HEADER
  66. };
  67. static const struct dvb_dmx_video_patterns mpeg2_gop = {
  68. {0x00, 0x00, 0x01, 0xB8},
  69. {0xFF, 0xFF, 0xFF, 0xFF},
  70. 4,
  71. DMX_IDX_MPEG_GOP
  72. };
  73. static const struct dvb_dmx_video_patterns mpeg2_iframe = {
  74. {0x00, 0x00, 0x01, 0x00, 0x00, 0x08},
  75. {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x38},
  76. 6,
  77. DMX_IDX_MPEG_I_FRAME_START
  78. };
  79. static const struct dvb_dmx_video_patterns mpeg2_pframe = {
  80. {0x00, 0x00, 0x01, 0x00, 0x00, 0x10},
  81. {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x38},
  82. 6,
  83. DMX_IDX_MPEG_P_FRAME_START
  84. };
  85. static const struct dvb_dmx_video_patterns mpeg2_bframe = {
  86. {0x00, 0x00, 0x01, 0x00, 0x00, 0x18},
  87. {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x38},
  88. 6,
  89. DMX_IDX_MPEG_B_FRAME_START
  90. };
  91. static const struct dvb_dmx_video_patterns h264_sps = {
  92. {0x00, 0x00, 0x01, 0x07},
  93. {0xFF, 0xFF, 0xFF, 0x1F},
  94. 4,
  95. DMX_IDX_H264_SPS
  96. };
  97. static const struct dvb_dmx_video_patterns h264_pps = {
  98. {0x00, 0x00, 0x01, 0x08},
  99. {0xFF, 0xFF, 0xFF, 0x1F},
  100. 4,
  101. DMX_IDX_H264_PPS
  102. };
  103. static const struct dvb_dmx_video_patterns h264_idr = {
  104. {0x00, 0x00, 0x01, 0x05, 0x80},
  105. {0xFF, 0xFF, 0xFF, 0x1F, 0x80},
  106. 5,
  107. DMX_IDX_H264_IDR_START
  108. };
  109. static const struct dvb_dmx_video_patterns h264_non_idr = {
  110. {0x00, 0x00, 0x01, 0x01, 0x80},
  111. {0xFF, 0xFF, 0xFF, 0x1F, 0x80},
  112. 5,
  113. DMX_IDX_H264_NON_IDR_START
  114. };
  115. /*
  116. * Forbidden (1 bit) + NAL idc (2 bits) + NAL type (5 bits)
  117. * I-Slice NAL idc = 3, NAL type = 5, 01100101 mask 0x7F
  118. */
  119. static const struct dvb_dmx_video_patterns h264_idr_islice = {
  120. {0x00, 0x00, 0x01, 0x65, 0x80},
  121. {0xFF, 0xFF, 0xFF, 0x7F, 0x80},
  122. 5,
  123. DMX_IDX_H264_IDR_ISLICE_START
  124. };
  125. /*
  126. * Forbidden (1 bit) + NAL idc (2 bits) + NAL type (5 bits)
  127. * P-Slice NAL idc = 2, NAL type = 1, 01000001 mask 0x7F
  128. */
  129. static const struct dvb_dmx_video_patterns h264_non_idr_pslice = {
  130. {0x00, 0x00, 0x01, 0x41, 0x80},
  131. {0xFF, 0xFF, 0xFF, 0x7F, 0x80},
  132. 5,
  133. DMX_IDX_H264_NON_IDR_PSLICE_START
  134. };
  135. /*
  136. * Forbidden (1 bit) + NAL idc (2 bits) + NAL type (5 bits)
  137. * B-Slice NAL idc = 0, NAL type = 1, 00000001 mask 0x7F
  138. */
  139. static const struct dvb_dmx_video_patterns h264_non_idr_bslice = {
  140. {0x00, 0x00, 0x01, 0x01, 0x80},
  141. {0xFF, 0xFF, 0xFF, 0x7F, 0x80},
  142. 5,
  143. DMX_IDX_H264_NON_IDR_BSLICE_START
  144. };
  145. static const struct dvb_dmx_video_patterns h264_non_access_unit_del = {
  146. {0x00, 0x00, 0x01, 0x09},
  147. {0xFF, 0xFF, 0xFF, 0x1F},
  148. 4,
  149. DMX_IDX_H264_ACCESS_UNIT_DEL
  150. };
  151. static const struct dvb_dmx_video_patterns h264_non_sei = {
  152. {0x00, 0x00, 0x01, 0x06},
  153. {0xFF, 0xFF, 0xFF, 0x1F},
  154. 4,
  155. DMX_IDX_H264_SEI
  156. };
  157. static const struct dvb_dmx_video_patterns vc1_seq_hdr = {
  158. {0x00, 0x00, 0x01, 0x0F},
  159. {0xFF, 0xFF, 0xFF, 0xFF},
  160. 4,
  161. DMX_IDX_VC1_SEQ_HEADER
  162. };
  163. static const struct dvb_dmx_video_patterns vc1_entry_point = {
  164. {0x00, 0x00, 0x01, 0x0E},
  165. {0xFF, 0xFF, 0xFF, 0xFF},
  166. 4,
  167. DMX_IDX_VC1_ENTRY_POINT
  168. };
  169. static const struct dvb_dmx_video_patterns vc1_frame = {
  170. {0x00, 0x00, 0x01, 0x0D},
  171. {0xFF, 0xFF, 0xFF, 0xFF},
  172. 4,
  173. DMX_IDX_VC1_FRAME_START
  174. };
  175. /******************************************************************************
  176. * static inlined helper functions
  177. ******************************************************************************/
  178. static inline u16 section_length(const u8 *buf)
  179. {
  180. return 3 + ((buf[1] & 0x0f) << 8) + buf[2];
  181. }
  182. static inline u8 ts_scrambling_ctrl(const u8 *buf)
  183. {
  184. return (buf[3] >> 6) & 0x3;
  185. }
  186. static inline u8 payload(const u8 *tsp)
  187. {
  188. if (!(tsp[3] & 0x10)) // no payload?
  189. return 0;
  190. if (tsp[3] & 0x20) { // adaptation field?
  191. if (tsp[4] > 183) // corrupted data?
  192. return 0;
  193. else
  194. return 184 - 1 - tsp[4];
  195. }
  196. return 184;
  197. }
  198. static u32 dvb_dmx_crc32(struct dvb_demux_feed *f, const u8 *src, size_t len)
  199. {
  200. return (f->feed.sec.crc_val = crc32_be(f->feed.sec.crc_val, src, len));
  201. }
  202. static void dvb_dmx_memcopy(struct dvb_demux_feed *f, u8 *d, const u8 *s,
  203. size_t len)
  204. {
  205. memcpy(d, s, len);
  206. }
  207. static u32 dvb_dmx_calc_time_delta(ktime_t past_time)
  208. {
  209. ktime_t curr_time = ktime_get();
  210. s64 delta_time_us = ktime_us_delta(curr_time, past_time);
  211. return (u32)delta_time_us;
  212. }
  213. /******************************************************************************
  214. * Software filter functions
  215. ******************************************************************************/
  216. /*
  217. * Check if two patterns are identical, taking mask into consideration.
  218. * @pattern1: the first byte pattern to compare.
  219. * @pattern2: the second byte pattern to compare.
  220. * @mask: the bit mask to use.
  221. * @pattern_size: the length of both patterns and the mask, in bytes.
  222. *
  223. * Return: 1 if patterns match, 0 otherwise.
  224. */
  225. static inline int dvb_dmx_patterns_match(const u8 *pattern1, const u8 *pattern2,
  226. const u8 *mask, size_t pattern_size)
  227. {
  228. int i;
  229. /*
  230. * Assumption: it is OK to access pattern1, pattern2 and mask.
  231. * This function performs no sanity checks to keep things fast.
  232. */
  233. for (i = 0; i < pattern_size; i++)
  234. if ((pattern1[i] & mask[i]) != (pattern2[i] & mask[i]))
  235. return 0;
  236. return 1;
  237. }
  238. /*
  239. * dvb_dmx_video_pattern_search -
  240. * search for framing patterns in a given buffer.
  241. *
  242. * Optimized version: first search for a common substring, e.g. 0x00 0x00 0x01.
  243. * If this string is found, go over all the given patterns (all must start
  244. * with this string) and search for their ending in the buffer.
  245. *
  246. * Assumption: the patterns we look for do not spread over more than two
  247. * buffers.
  248. *
  249. * @paterns: the full patterns information to look for.
  250. * @patterns_num: the number of patterns to look for.
  251. * @buf: the buffer to search.
  252. * @buf_size: the size of the buffer to search. we search the entire buffer.
  253. * @prefix_size_masks: a bit mask (per pattern) of possible prefix sizes to use
  254. * when searching for a pattern that started at the last buffer.
  255. * Updated in this function for use in the next lookup.
  256. * @results: lookup results (offset, type, used_prefix_size) per found pattern,
  257. * up to DVB_DMX_MAX_FOUND_PATTERNS.
  258. *
  259. * Return:
  260. * Number of patterns found (up to DVB_DMX_MAX_FOUND_PATTERNS).
  261. * 0 if pattern was not found.
  262. * error value on failure.
  263. */
  264. int dvb_dmx_video_pattern_search(
  265. const struct dvb_dmx_video_patterns
  266. *patterns[DVB_DMX_MAX_SEARCH_PATTERN_NUM],
  267. int patterns_num,
  268. const u8 *buf,
  269. size_t buf_size,
  270. struct dvb_dmx_video_prefix_size_masks *prefix_size_masks,
  271. struct dvb_dmx_video_patterns_results *results)
  272. {
  273. int i, j;
  274. unsigned int current_size;
  275. u32 prefix;
  276. int found = 0;
  277. int start_offset = 0;
  278. /* the starting common substring to look for */
  279. u8 string[] = {0x00, 0x00, 0x01};
  280. /* the mask for the starting string */
  281. u8 string_mask[] = {0xFF, 0xFF, 0xFF};
  282. /* the size of the starting string (in bytes) */
  283. size_t string_size = 3;
  284. if ((patterns == NULL) || (patterns_num <= 0) || (buf == NULL))
  285. return -EINVAL;
  286. memset(results, 0, sizeof(struct dvb_dmx_video_patterns_results));
  287. /*
  288. * handle prefix - disregard string, simply check all patterns,
  289. * looking for a matching suffix at the very beginning of the buffer.
  290. */
  291. for (j = 0; (j < patterns_num) && !found; j++) {
  292. prefix = prefix_size_masks->size_mask[j];
  293. current_size = 32;
  294. while (prefix) {
  295. if (prefix & (0x1 << (current_size - 1))) {
  296. /*
  297. * check that we don't look further
  298. * than buf_size boundary
  299. */
  300. if ((int)(patterns[j]->size - current_size) >
  301. buf_size)
  302. break;
  303. if (current_size >= DVB_DMX_MAX_PATTERN_LEN)
  304. break;
  305. if (dvb_dmx_patterns_match(
  306. (patterns[j]->pattern + current_size),
  307. buf, (patterns[j]->mask + current_size),
  308. (patterns[j]->size - current_size))) {
  309. /*
  310. * pattern found using prefix at the
  311. * very beginning of the buffer, so
  312. * offset is 0, but we already zeroed
  313. * everything in the beginning of the
  314. * function. that's why the next line
  315. * is commented.
  316. */
  317. /* results->info[found].offset = 0; */
  318. results->info[found].type =
  319. patterns[j]->type;
  320. results->info[found].used_prefix_size =
  321. current_size;
  322. found++;
  323. /*
  324. * save offset to start looking from
  325. * in the buffer, to avoid reusing the
  326. * data of a pattern we already found.
  327. */
  328. start_offset = (patterns[j]->size -
  329. current_size);
  330. if (found >= DVB_DMX_MAX_FOUND_PATTERNS)
  331. goto next_prefix_lookup;
  332. /*
  333. * we don't want to search for the same
  334. * pattern with several possible prefix
  335. * sizes if we have already found it,
  336. * so we break from the inner loop.
  337. * since we incremented 'found', we
  338. * will not search for additional
  339. * patterns using a prefix - that would
  340. * imply ambiguous patterns where one
  341. * pattern can be included in another.
  342. * the for loop will exit.
  343. */
  344. break;
  345. }
  346. }
  347. prefix &= ~(0x1 << (current_size - 1));
  348. current_size--;
  349. }
  350. }
  351. /*
  352. * Search buffer for entire pattern, starting with the string.
  353. * Note the external for loop does not execute if buf_size is
  354. * smaller than string_size (the cast to int is required, since
  355. * size_t is unsigned).
  356. */
  357. for (i = start_offset; i < (int)(buf_size - string_size + 1); i++) {
  358. if (dvb_dmx_patterns_match(string, (buf + i), string_mask,
  359. string_size)) {
  360. /* now search for patterns: */
  361. for (j = 0; j < patterns_num; j++) {
  362. /* avoid overflow to next buffer */
  363. if ((i + patterns[j]->size) > buf_size)
  364. continue;
  365. if (dvb_dmx_patterns_match(
  366. (patterns[j]->pattern + string_size),
  367. (buf + i + string_size),
  368. (patterns[j]->mask + string_size),
  369. (patterns[j]->size - string_size))) {
  370. results->info[found].offset = i;
  371. results->info[found].type =
  372. patterns[j]->type;
  373. /*
  374. * save offset to start next prefix
  375. * lookup, to avoid reusing the data
  376. * of any pattern we already found.
  377. */
  378. if ((i + patterns[j]->size) >
  379. start_offset)
  380. start_offset = (i +
  381. patterns[j]->size);
  382. /*
  383. * did not use a prefix to find this
  384. * pattern, but we zeroed everything
  385. * in the beginning of the function.
  386. * So no need to zero used_prefix_size
  387. * for results->info[found]
  388. */
  389. found++;
  390. if (found >= DVB_DMX_MAX_FOUND_PATTERNS)
  391. goto next_prefix_lookup;
  392. /*
  393. * theoretically we don't have to break
  394. * here, but we don't want to search
  395. * for the other matching patterns on
  396. * the very same same place in the
  397. * buffer. That would mean the
  398. * (pattern & mask) combinations are
  399. * not unique. So we break from inner
  400. * loop and move on to the next place
  401. * in the buffer.
  402. */
  403. break;
  404. }
  405. }
  406. }
  407. }
  408. next_prefix_lookup:
  409. /* check for possible prefix sizes for the next buffer */
  410. for (j = 0; j < patterns_num; j++) {
  411. prefix_size_masks->size_mask[j] = 0;
  412. for (i = 1; i < patterns[j]->size; i++) {
  413. /*
  414. * avoid looking outside of the buffer
  415. * or reusing previously used data.
  416. */
  417. if (i > (buf_size - start_offset))
  418. break;
  419. if (dvb_dmx_patterns_match(patterns[j]->pattern,
  420. (buf + buf_size - i),
  421. patterns[j]->mask, i)) {
  422. prefix_size_masks->size_mask[j] |=
  423. (1 << (i - 1));
  424. }
  425. }
  426. }
  427. return found;
  428. }
  429. EXPORT_SYMBOL(dvb_dmx_video_pattern_search);
  430. /**
  431. * dvb_dmx_notify_section_event() - Notify demux event for all filters of a
  432. * specified section feed.
  433. *
  434. * @feed: dvb_demux_feed object
  435. * @event: demux event to notify
  436. * @should_lock: specifies whether the function should lock the demux
  437. *
  438. * Caller is responsible for locking the demux properly, either by doing the
  439. * locking itself and setting 'should_lock' to 0, or have the function do it
  440. * by setting 'should_lock' to 1.
  441. */
  442. int dvb_dmx_notify_section_event(struct dvb_demux_feed *feed,
  443. struct dmx_data_ready *event, int should_lock)
  444. {
  445. struct dvb_demux_filter *f;
  446. if (feed == NULL || event == NULL || feed->type != DMX_TYPE_SEC)
  447. return -EINVAL;
  448. if (!should_lock && !spin_is_locked(&feed->demux->lock))
  449. return -EINVAL;
  450. if (should_lock)
  451. spin_lock(&feed->demux->lock);
  452. f = feed->filter;
  453. while (f && feed->feed.sec.is_filtering) {
  454. feed->data_ready_cb.sec(&f->filter, event);
  455. f = f->next;
  456. }
  457. if (should_lock)
  458. spin_unlock(&feed->demux->lock);
  459. return 0;
  460. }
  461. EXPORT_SYMBOL(dvb_dmx_notify_section_event);
  462. static int dvb_dmx_check_pes_end(struct dvb_demux_feed *feed)
  463. {
  464. struct dmx_data_ready data;
  465. if (!feed->pusi_seen)
  466. return 0;
  467. data.status = DMX_OK_PES_END;
  468. data.data_length = 0;
  469. data.pes_end.start_gap = 0;
  470. data.pes_end.actual_length = feed->peslen;
  471. data.pes_end.disc_indicator_set = 0;
  472. data.pes_end.pes_length_mismatch = 0;
  473. data.pes_end.stc = 0;
  474. data.pes_end.tei_counter = feed->pes_tei_counter;
  475. data.pes_end.cont_err_counter = feed->pes_cont_err_counter;
  476. data.pes_end.ts_packets_num = feed->pes_ts_packets_num;
  477. return feed->data_ready_cb.ts(&feed->feed.ts, &data);
  478. }
  479. static inline int dvb_dmx_swfilter_payload(struct dvb_demux_feed *feed,
  480. const u8 *buf)
  481. {
  482. int count = payload(buf);
  483. int p;
  484. int ccok;
  485. u8 cc;
  486. int ret;
  487. if (count == 0)
  488. return -1;
  489. p = 188 - count;
  490. cc = buf[3] & 0x0f;
  491. if (feed->first_cc)
  492. ccok = 1;
  493. else
  494. ccok = ((feed->cc + 1) & 0x0f) == cc;
  495. feed->first_cc = 0;
  496. feed->cc = cc;
  497. /* PUSI ? */
  498. if (buf[1] & 0x40) {
  499. dvb_dmx_check_pes_end(feed);
  500. feed->pusi_seen = 1;
  501. feed->peslen = 0;
  502. feed->pes_tei_counter = 0;
  503. feed->pes_cont_err_counter = 0;
  504. feed->pes_ts_packets_num = 0;
  505. }
  506. if (feed->pusi_seen == 0)
  507. return 0;
  508. ret = feed->cb.ts(&buf[p], count, NULL, 0, &feed->feed.ts);
  509. /* Verify TS packet was copied successfully */
  510. if (!ret) {
  511. feed->pes_cont_err_counter += !ccok;
  512. feed->pes_tei_counter += (buf[1] & 0x80) ? 1 : 0;
  513. feed->pes_ts_packets_num++;
  514. feed->peslen += count;
  515. }
  516. return ret;
  517. }
  518. static int dvb_dmx_swfilter_sectionfilter(struct dvb_demux_feed *feed,
  519. struct dvb_demux_filter *f)
  520. {
  521. u8 neq = 0;
  522. int i;
  523. for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
  524. u8 xor = f->filter.filter_value[i] ^ feed->feed.sec.secbuf[i];
  525. if (f->maskandmode[i] & xor)
  526. return 0;
  527. neq |= f->maskandnotmode[i] & xor;
  528. }
  529. if (f->doneq && !neq)
  530. return 0;
  531. return feed->cb.sec(feed->feed.sec.secbuf, feed->feed.sec.seclen,
  532. NULL, 0, &f->filter);
  533. }
  534. static inline int dvb_dmx_swfilter_section_feed(struct dvb_demux_feed *feed)
  535. {
  536. struct dvb_demux *demux = feed->demux;
  537. struct dvb_demux_filter *f = feed->filter;
  538. struct dmx_section_feed *sec = &feed->feed.sec;
  539. int section_syntax_indicator;
  540. if (!sec->is_filtering)
  541. return 0;
  542. if (!f)
  543. return 0;
  544. if (sec->check_crc) {
  545. ktime_t pre_crc_time = ktime_set(0, 0);
  546. if (dvb_demux_performancecheck)
  547. pre_crc_time = ktime_get();
  548. section_syntax_indicator = ((sec->secbuf[1] & 0x80) != 0);
  549. if (section_syntax_indicator &&
  550. demux->check_crc32(feed, sec->secbuf, sec->seclen)) {
  551. if (dvb_demux_performancecheck)
  552. demux->total_crc_time +=
  553. dvb_dmx_calc_time_delta(pre_crc_time);
  554. /* Notify on CRC error */
  555. feed->cb.sec(NULL, 0, NULL, 0,
  556. &f->filter);
  557. return -1;
  558. }
  559. if (dvb_demux_performancecheck)
  560. demux->total_crc_time +=
  561. dvb_dmx_calc_time_delta(pre_crc_time);
  562. }
  563. do {
  564. if (dvb_dmx_swfilter_sectionfilter(feed, f) < 0)
  565. return -1;
  566. } while ((f = f->next) && sec->is_filtering);
  567. sec->seclen = 0;
  568. return 0;
  569. }
  570. static void dvb_dmx_swfilter_section_new(struct dvb_demux_feed *feed)
  571. {
  572. struct dmx_section_feed *sec = &feed->feed.sec;
  573. #ifdef DVB_DEMUX_SECTION_LOSS_LOG
  574. if (sec->secbufp < sec->tsfeedp) {
  575. int i, n = sec->tsfeedp - sec->secbufp;
  576. /*
  577. * Section padding is done with 0xff bytes entirely.
  578. * Due to speed reasons, we won't check all of them
  579. * but just first and last.
  580. */
  581. if (sec->secbuf[0] != 0xff || sec->secbuf[n - 1] != 0xff) {
  582. printk("dvb_demux.c section ts padding loss: %d/%d\n",
  583. n, sec->tsfeedp);
  584. printk("dvb_demux.c pad data:");
  585. for (i = 0; i < n; i++)
  586. printk(" %02x", sec->secbuf[i]);
  587. printk("\n");
  588. }
  589. }
  590. #endif
  591. sec->tsfeedp = sec->secbufp = sec->seclen = 0;
  592. sec->secbuf = sec->secbuf_base;
  593. }
  594. /*
  595. * Losless Section Demux 1.4.1 by Emard
  596. * Valsecchi Patrick:
  597. * - middle of section A (no PUSI)
  598. * - end of section A and start of section B
  599. * (with PUSI pointing to the start of the second section)
  600. *
  601. * In this case, without feed->pusi_seen you'll receive a garbage section
  602. * consisting of the end of section A. Basically because tsfeedp
  603. * is incemented and the use=0 condition is not raised
  604. * when the second packet arrives.
  605. *
  606. * Fix:
  607. * when demux is started, let feed->pusi_seen = 0 to
  608. * prevent initial feeding of garbage from the end of
  609. * previous section. When you for the first time see PUSI=1
  610. * then set feed->pusi_seen = 1
  611. */
  612. static int dvb_dmx_swfilter_section_copy_dump(struct dvb_demux_feed *feed,
  613. const u8 *buf, u8 len)
  614. {
  615. struct dvb_demux *demux = feed->demux;
  616. struct dmx_section_feed *sec = &feed->feed.sec;
  617. u16 limit, seclen, n;
  618. if (sec->tsfeedp >= DMX_MAX_SECFEED_SIZE)
  619. return 0;
  620. if (sec->tsfeedp + len > DMX_MAX_SECFEED_SIZE) {
  621. #ifdef DVB_DEMUX_SECTION_LOSS_LOG
  622. printk("dvb_demux.c section buffer full loss: %d/%d\n",
  623. sec->tsfeedp + len - DMX_MAX_SECFEED_SIZE,
  624. DMX_MAX_SECFEED_SIZE);
  625. #endif
  626. len = DMX_MAX_SECFEED_SIZE - sec->tsfeedp;
  627. }
  628. if (len <= 0)
  629. return 0;
  630. demux->memcopy(feed, sec->secbuf_base + sec->tsfeedp, buf, len);
  631. sec->tsfeedp += len;
  632. /*
  633. * Dump all the sections we can find in the data (Emard)
  634. */
  635. limit = sec->tsfeedp;
  636. if (limit > DMX_MAX_SECFEED_SIZE)
  637. return -1; /* internal error should never happen */
  638. /* to be sure always set secbuf */
  639. sec->secbuf = sec->secbuf_base + sec->secbufp;
  640. for (n = 0; sec->secbufp + 2 < limit; n++) {
  641. seclen = section_length(sec->secbuf);
  642. if (seclen <= 0 || seclen > DMX_MAX_SECTION_SIZE
  643. || seclen + sec->secbufp > limit)
  644. return 0;
  645. sec->seclen = seclen;
  646. sec->crc_val = ~0;
  647. /* dump [secbuf .. secbuf+seclen) */
  648. if (feed->pusi_seen)
  649. dvb_dmx_swfilter_section_feed(feed);
  650. #ifdef DVB_DEMUX_SECTION_LOSS_LOG
  651. else
  652. printk("dvb_demux.c pusi not seen, discarding section data\n");
  653. #endif
  654. sec->secbufp += seclen; /* secbufp and secbuf moving together is */
  655. sec->secbuf += seclen; /* redundant but saves pointer arithmetic */
  656. }
  657. return 0;
  658. }
  659. static int dvb_dmx_swfilter_section_one_packet(struct dvb_demux_feed *feed,
  660. const u8 *buf)
  661. {
  662. u8 p, count;
  663. int ccok, dc_i = 0;
  664. u8 cc;
  665. count = payload(buf);
  666. if (count == 0) /* count == 0 if no payload or out of range */
  667. return -1;
  668. p = 188 - count; /* payload start */
  669. cc = buf[3] & 0x0f;
  670. if (feed->first_cc)
  671. ccok = 1;
  672. else
  673. ccok = ((feed->cc + 1) & 0x0f) == cc;
  674. /* discard TS packets holding sections with TEI bit set */
  675. if (buf[1] & 0x80)
  676. return -EINVAL;
  677. feed->first_cc = 0;
  678. feed->cc = cc;
  679. if (buf[3] & 0x20) {
  680. /* adaption field present, check for discontinuity_indicator */
  681. if ((buf[4] > 0) && (buf[5] & 0x80))
  682. dc_i = 1;
  683. }
  684. if (!ccok || dc_i) {
  685. #ifdef DVB_DEMUX_SECTION_LOSS_LOG
  686. printk("dvb_demux.c discontinuity detected %d bytes lost\n",
  687. count);
  688. /*
  689. * those bytes under sume circumstances will again be reported
  690. * in the following dvb_dmx_swfilter_section_new
  691. */
  692. #endif
  693. /*
  694. * Discontinuity detected. Reset pusi_seen = 0 to
  695. * stop feeding of suspicious data until next PUSI=1 arrives
  696. */
  697. feed->pusi_seen = 0;
  698. dvb_dmx_swfilter_section_new(feed);
  699. }
  700. if (buf[1] & 0x40) {
  701. /* PUSI=1 (is set), section boundary is here */
  702. if (count > 1 && buf[p] < count) {
  703. const u8 *before = &buf[p + 1];
  704. u8 before_len = buf[p];
  705. const u8 *after = &before[before_len];
  706. u8 after_len = count - 1 - before_len;
  707. dvb_dmx_swfilter_section_copy_dump(feed, before,
  708. before_len);
  709. /* before start of new section, set pusi_seen = 1 */
  710. feed->pusi_seen = 1;
  711. dvb_dmx_swfilter_section_new(feed);
  712. dvb_dmx_swfilter_section_copy_dump(feed, after,
  713. after_len);
  714. }
  715. #ifdef DVB_DEMUX_SECTION_LOSS_LOG
  716. else if (count > 0)
  717. printk("dvb_demux.c PUSI=1 but %d bytes lost\n", count);
  718. #endif
  719. } else {
  720. /* PUSI=0 (is not set), no section boundary */
  721. dvb_dmx_swfilter_section_copy_dump(feed, &buf[p], count);
  722. }
  723. return 0;
  724. }
  725. /*
  726. * dvb_dmx_swfilter_section_packet - wrapper for section filtering of single
  727. * TS packet.
  728. *
  729. * @feed: dvb demux feed
  730. * @buf: buffer containing the TS packet
  731. * @should_lock: specifies demux locking semantics: if not set, proper demux
  732. * locking is expected to have been done by the caller.
  733. *
  734. * Return error status
  735. */
  736. int dvb_dmx_swfilter_section_packet(struct dvb_demux_feed *feed,
  737. const u8 *buf, int should_lock)
  738. {
  739. int ret;
  740. if (!should_lock && !spin_is_locked(&feed->demux->lock)) {
  741. pr_err("%s: demux spinlock should have been locked\n",
  742. __func__);
  743. return -EINVAL;
  744. }
  745. if (should_lock)
  746. spin_lock(&feed->demux->lock);
  747. ret = dvb_dmx_swfilter_section_one_packet(feed, buf);
  748. if (should_lock)
  749. spin_unlock(&feed->demux->lock);
  750. return ret;
  751. }
  752. EXPORT_SYMBOL(dvb_dmx_swfilter_section_packet);
  753. static int dvb_demux_idx_event_sort(struct dmx_index_event_info *curr,
  754. struct dmx_index_event_info *new)
  755. {
  756. if (curr->match_tsp_num > new->match_tsp_num)
  757. return 0;
  758. if (curr->match_tsp_num < new->match_tsp_num)
  759. return 1;
  760. /*
  761. * In case TSP numbers are equal, sort according to event type giving
  762. * priority to PUSI events first, then RAI and finally framing events.
  763. */
  764. if ((curr->type & DMX_IDX_RAI && new->type & DMX_IDX_PUSI) ||
  765. (!(curr->type & DMX_IDX_PUSI) && !(curr->type & DMX_IDX_RAI) &&
  766. new->type & (DMX_IDX_PUSI | DMX_IDX_RAI)))
  767. return 0;
  768. return 1;
  769. }
  770. static int dvb_demux_save_idx_event(struct dvb_demux_feed *feed,
  771. struct dmx_index_event_info *idx_event,
  772. int traverse_from_tail)
  773. {
  774. struct dmx_index_entry *idx_entry;
  775. struct dmx_index_entry *curr_entry;
  776. struct list_head *pos;
  777. /* get entry from free list */
  778. if (list_empty(&feed->rec_info->idx_info.free_list)) {
  779. pr_err("%s: index free list is empty\n", __func__);
  780. return -ENOMEM;
  781. }
  782. idx_entry = list_first_entry(&feed->rec_info->idx_info.free_list,
  783. struct dmx_index_entry, next);
  784. list_del(&idx_entry->next);
  785. idx_entry->event = *idx_event;
  786. pos = &feed->rec_info->idx_info.ready_list;
  787. if (traverse_from_tail) {
  788. list_for_each_entry_reverse(curr_entry,
  789. &feed->rec_info->idx_info.ready_list, next) {
  790. if (dvb_demux_idx_event_sort(&curr_entry->event,
  791. idx_event)) {
  792. pos = &curr_entry->next;
  793. break;
  794. }
  795. }
  796. } else {
  797. list_for_each_entry(curr_entry,
  798. &feed->rec_info->idx_info.ready_list, next) {
  799. if (!dvb_demux_idx_event_sort(&curr_entry->event,
  800. idx_event)) {
  801. pos = &curr_entry->next;
  802. break;
  803. }
  804. }
  805. }
  806. if (traverse_from_tail)
  807. list_add(&idx_entry->next, pos);
  808. else
  809. list_add_tail(&idx_entry->next, pos);
  810. return 0;
  811. }
  812. int dvb_demux_push_idx_event(struct dvb_demux_feed *feed,
  813. struct dmx_index_event_info *idx_event, int should_lock)
  814. {
  815. int ret;
  816. if (!should_lock && !spin_is_locked(&feed->demux->lock))
  817. return -EINVAL;
  818. if (should_lock)
  819. spin_lock(&feed->demux->lock);
  820. ret = dvb_demux_save_idx_event(feed, idx_event, 1);
  821. if (should_lock)
  822. spin_unlock(&feed->demux->lock);
  823. return ret;
  824. }
  825. EXPORT_SYMBOL(dvb_demux_push_idx_event);
  826. static inline void dvb_dmx_notify_indexing(struct dvb_demux_feed *feed)
  827. {
  828. struct dmx_data_ready dmx_data_ready;
  829. struct dmx_index_entry *curr_entry;
  830. struct list_head *n, *pos;
  831. dmx_data_ready.status = DMX_OK_IDX;
  832. list_for_each_safe(pos, n, &feed->rec_info->idx_info.ready_list) {
  833. curr_entry = list_entry(pos, struct dmx_index_entry, next);
  834. if ((feed->rec_info->idx_info.min_pattern_tsp_num == (u64)-1) ||
  835. (curr_entry->event.match_tsp_num <=
  836. feed->rec_info->idx_info.min_pattern_tsp_num)) {
  837. dmx_data_ready.idx_event = curr_entry->event;
  838. feed->data_ready_cb.ts(&feed->feed.ts, &dmx_data_ready);
  839. list_del(&curr_entry->next);
  840. list_add_tail(&curr_entry->next,
  841. &feed->rec_info->idx_info.free_list);
  842. }
  843. }
  844. }
  845. void dvb_dmx_notify_idx_events(struct dvb_demux_feed *feed, int should_lock)
  846. {
  847. if (!should_lock && !spin_is_locked(&feed->demux->lock))
  848. return;
  849. if (should_lock)
  850. spin_lock(&feed->demux->lock);
  851. dvb_dmx_notify_indexing(feed);
  852. if (should_lock)
  853. spin_unlock(&feed->demux->lock);
  854. }
  855. EXPORT_SYMBOL(dvb_dmx_notify_idx_events);
  856. static void dvb_dmx_process_pattern_result(struct dvb_demux_feed *feed,
  857. struct dvb_dmx_video_patterns_results *patterns, int pattern,
  858. u64 curr_stc, u64 prev_stc,
  859. u64 curr_match_tsp, u64 prev_match_tsp,
  860. u64 curr_pusi_tsp, u64 prev_pusi_tsp)
  861. {
  862. int mpeg_frame_start;
  863. int h264_frame_start;
  864. int vc1_frame_start;
  865. int seq_start;
  866. u64 frame_end_in_seq;
  867. struct dmx_index_event_info idx_event;
  868. idx_event.pid = feed->pid;
  869. if (patterns->info[pattern].used_prefix_size) {
  870. idx_event.match_tsp_num = prev_match_tsp;
  871. idx_event.last_pusi_tsp_num = prev_pusi_tsp;
  872. idx_event.stc = prev_stc;
  873. } else {
  874. idx_event.match_tsp_num = curr_match_tsp;
  875. idx_event.last_pusi_tsp_num = curr_pusi_tsp;
  876. idx_event.stc = curr_stc;
  877. }
  878. /* notify on frame-end if needed */
  879. if (feed->prev_frame_valid) {
  880. if (feed->prev_frame_type & DMX_IDX_MPEG_I_FRAME_START) {
  881. idx_event.type = DMX_IDX_MPEG_I_FRAME_END;
  882. frame_end_in_seq = DMX_IDX_MPEG_FIRST_SEQ_FRAME_END;
  883. } else if (feed->prev_frame_type & DMX_IDX_MPEG_P_FRAME_START) {
  884. idx_event.type = DMX_IDX_MPEG_P_FRAME_END;
  885. frame_end_in_seq = DMX_IDX_MPEG_FIRST_SEQ_FRAME_END;
  886. } else if (feed->prev_frame_type & DMX_IDX_MPEG_B_FRAME_START) {
  887. idx_event.type = DMX_IDX_MPEG_B_FRAME_END;
  888. frame_end_in_seq = DMX_IDX_MPEG_FIRST_SEQ_FRAME_END;
  889. } else if (feed->prev_frame_type & DMX_IDX_H264_IDR_START) {
  890. idx_event.type = DMX_IDX_H264_IDR_END;
  891. frame_end_in_seq = DMX_IDX_H264_FIRST_SPS_FRAME_END;
  892. } else if (feed->prev_frame_type & DMX_IDX_H264_NON_IDR_START) {
  893. idx_event.type = DMX_IDX_H264_NON_IDR_END;
  894. frame_end_in_seq = DMX_IDX_H264_FIRST_SPS_FRAME_END;
  895. } else if (feed->prev_frame_type &
  896. DMX_IDX_H264_IDR_ISLICE_START) {
  897. idx_event.type = DMX_IDX_H264_IDR_END;
  898. frame_end_in_seq = DMX_IDX_H264_FIRST_SPS_FRAME_END;
  899. } else if (feed->prev_frame_type &
  900. DMX_IDX_H264_NON_IDR_PSLICE_START) {
  901. idx_event.type = DMX_IDX_H264_NON_IDR_END;
  902. frame_end_in_seq = DMX_IDX_H264_FIRST_SPS_FRAME_END;
  903. } else if (feed->prev_frame_type &
  904. DMX_IDX_H264_NON_IDR_BSLICE_START) {
  905. idx_event.type = DMX_IDX_H264_NON_IDR_END;
  906. frame_end_in_seq = DMX_IDX_H264_FIRST_SPS_FRAME_END;
  907. } else {
  908. idx_event.type = DMX_IDX_VC1_FRAME_END;
  909. frame_end_in_seq = DMX_IDX_VC1_FIRST_SEQ_FRAME_END;
  910. }
  911. if (feed->idx_params.types & idx_event.type)
  912. dvb_demux_save_idx_event(feed, &idx_event, 1);
  913. if (feed->first_frame_in_seq_notified &&
  914. feed->idx_params.types & frame_end_in_seq) {
  915. idx_event.type = frame_end_in_seq;
  916. dvb_demux_save_idx_event(feed, &idx_event, 1);
  917. feed->first_frame_in_seq_notified = 0;
  918. }
  919. }
  920. seq_start = patterns->info[pattern].type &
  921. (DMX_IDX_MPEG_SEQ_HEADER | DMX_IDX_H264_SPS |
  922. DMX_IDX_VC1_SEQ_HEADER);
  923. /* did we find start of sequence/SPS? */
  924. if (seq_start) {
  925. feed->first_frame_in_seq = 1;
  926. feed->first_frame_in_seq_notified = 0;
  927. feed->prev_frame_valid = 0;
  928. idx_event.type = patterns->info[pattern].type;
  929. if (feed->idx_params.types & idx_event.type)
  930. dvb_demux_save_idx_event(feed, &idx_event, 1);
  931. return;
  932. }
  933. mpeg_frame_start = patterns->info[pattern].type &
  934. (DMX_IDX_MPEG_I_FRAME_START |
  935. DMX_IDX_MPEG_P_FRAME_START |
  936. DMX_IDX_MPEG_B_FRAME_START);
  937. h264_frame_start = patterns->info[pattern].type &
  938. (DMX_IDX_H264_IDR_START | DMX_IDX_H264_NON_IDR_START);
  939. vc1_frame_start = patterns->info[pattern].type &
  940. DMX_IDX_VC1_FRAME_START;
  941. if (!mpeg_frame_start && !h264_frame_start && !vc1_frame_start) {
  942. /* neither sequence nor frame, notify on the entry if needed */
  943. idx_event.type = patterns->info[pattern].type;
  944. if (feed->idx_params.types & idx_event.type)
  945. dvb_demux_save_idx_event(feed, &idx_event, 1);
  946. feed->prev_frame_valid = 0;
  947. return;
  948. }
  949. /* notify on first frame in sequence/sps if needed */
  950. if (feed->first_frame_in_seq) {
  951. feed->first_frame_in_seq = 0;
  952. feed->first_frame_in_seq_notified = 1;
  953. if (mpeg_frame_start)
  954. idx_event.type = DMX_IDX_MPEG_FIRST_SEQ_FRAME_START;
  955. else if (h264_frame_start)
  956. idx_event.type = DMX_IDX_H264_FIRST_SPS_FRAME_START;
  957. else
  958. idx_event.type = DMX_IDX_VC1_FIRST_SEQ_FRAME_START;
  959. if (feed->idx_params.types & idx_event.type)
  960. dvb_demux_save_idx_event(feed, &idx_event, 1);
  961. }
  962. /* notify on frame start if needed */
  963. idx_event.type = patterns->info[pattern].type;
  964. if (feed->idx_params.types & idx_event.type)
  965. dvb_demux_save_idx_event(feed, &idx_event, 1);
  966. feed->prev_frame_valid = 1;
  967. feed->prev_frame_type = patterns->info[pattern].type;
  968. }
  969. void dvb_dmx_process_idx_pattern(struct dvb_demux_feed *feed,
  970. struct dvb_dmx_video_patterns_results *patterns, int pattern,
  971. u64 curr_stc, u64 prev_stc,
  972. u64 curr_match_tsp, u64 prev_match_tsp,
  973. u64 curr_pusi_tsp, u64 prev_pusi_tsp)
  974. {
  975. spin_lock(&feed->demux->lock);
  976. dvb_dmx_process_pattern_result(feed,
  977. patterns, pattern,
  978. curr_stc, prev_stc,
  979. curr_match_tsp, prev_match_tsp,
  980. curr_pusi_tsp, prev_pusi_tsp);
  981. spin_unlock(&feed->demux->lock);
  982. }
  983. EXPORT_SYMBOL(dvb_dmx_process_idx_pattern);
  984. static void dvb_dmx_index(struct dvb_demux_feed *feed,
  985. const u8 *buf,
  986. const u8 timestamp[TIMESTAMP_LEN])
  987. {
  988. int i;
  989. int p;
  990. u64 stc;
  991. int found_patterns;
  992. int count = payload(buf);
  993. u64 min_pattern_tsp_num;
  994. struct dvb_demux_feed *tmp_feed;
  995. struct dvb_demux *demux = feed->demux;
  996. struct dmx_index_event_info idx_event;
  997. struct dvb_dmx_video_patterns_results patterns;
  998. if (feed->demux->convert_ts)
  999. feed->demux->convert_ts(feed, timestamp, &stc);
  1000. else
  1001. stc = 0;
  1002. idx_event.pid = feed->pid;
  1003. idx_event.stc = stc;
  1004. idx_event.match_tsp_num = feed->rec_info->ts_output_count;
  1005. /* PUSI ? */
  1006. if (buf[1] & 0x40) {
  1007. feed->curr_pusi_tsp_num = feed->rec_info->ts_output_count;
  1008. if (feed->idx_params.types & DMX_IDX_PUSI) {
  1009. idx_event.type = DMX_IDX_PUSI;
  1010. idx_event.last_pusi_tsp_num =
  1011. feed->curr_pusi_tsp_num;
  1012. dvb_demux_save_idx_event(feed, &idx_event, 1);
  1013. }
  1014. }
  1015. /*
  1016. * if we still did not encounter a TS packet with PUSI indication,
  1017. * we cannot report index entries yet as we need to provide
  1018. * the TS packet number with PUSI indication preceding the TS
  1019. * packet pointed by the reported index entry.
  1020. */
  1021. if (feed->curr_pusi_tsp_num == (u64)-1) {
  1022. dvb_dmx_notify_indexing(feed);
  1023. return;
  1024. }
  1025. if ((feed->idx_params.types & DMX_IDX_RAI) && /* index RAI? */
  1026. (buf[3] & 0x20) && /* adaptation field exists? */
  1027. (buf[4] > 0) && /* adaptation field len > 0 ? */
  1028. (buf[5] & 0x40)) { /* RAI is set? */
  1029. idx_event.type = DMX_IDX_RAI;
  1030. idx_event.last_pusi_tsp_num =
  1031. feed->curr_pusi_tsp_num;
  1032. dvb_demux_save_idx_event(feed, &idx_event, 1);
  1033. }
  1034. /*
  1035. * if no pattern search is required, or the TS packet has no payload,
  1036. * pattern search is not executed.
  1037. */
  1038. if (!feed->pattern_num || !count) {
  1039. dvb_dmx_notify_indexing(feed);
  1040. return;
  1041. }
  1042. p = 188 - count; /* payload start */
  1043. found_patterns =
  1044. dvb_dmx_video_pattern_search(feed->patterns,
  1045. feed->pattern_num, &buf[p], count,
  1046. &feed->prefix_size, &patterns);
  1047. for (i = 0; i < found_patterns; i++)
  1048. dvb_dmx_process_pattern_result(feed, &patterns, i,
  1049. stc, feed->prev_stc,
  1050. feed->rec_info->ts_output_count, feed->prev_tsp_num,
  1051. feed->curr_pusi_tsp_num, feed->prev_pusi_tsp_num);
  1052. feed->prev_tsp_num = feed->rec_info->ts_output_count;
  1053. feed->prev_pusi_tsp_num = feed->curr_pusi_tsp_num;
  1054. feed->prev_stc = stc;
  1055. feed->last_pattern_tsp_num = feed->rec_info->ts_output_count;
  1056. /*
  1057. * it is possible to have a TS packet that has a prefix of
  1058. * a video pattern but the video pattern is not identified yet
  1059. * until we get the next TS packet of that PID. When we get
  1060. * the next TS packet of that PID, pattern-search would
  1061. * detect that we have a new index entry that starts in the
  1062. * previous TS packet.
  1063. * In order to notify the user on index entries with match_tsp_num
  1064. * in ascending order, index events with match_tsp_num up to
  1065. * the last_pattern_tsp_num are notified now to the user,
  1066. * the rest can't be notified now as we might hit the above
  1067. * scenario and cause the events not to be notified with
  1068. * ascending order of match_tsp_num.
  1069. */
  1070. if (feed->rec_info->idx_info.pattern_search_feeds_num == 1) {
  1071. /*
  1072. * optimization for case we have only one PID
  1073. * with video pattern search, in this case
  1074. * min_pattern_tsp_num is simply updated to the new
  1075. * TS packet number of the PID with pattern search.
  1076. */
  1077. feed->rec_info->idx_info.min_pattern_tsp_num =
  1078. feed->last_pattern_tsp_num;
  1079. dvb_dmx_notify_indexing(feed);
  1080. return;
  1081. }
  1082. /*
  1083. * if we have more than one PID with pattern search,
  1084. * min_pattern_tsp_num needs to be updated now based on
  1085. * last_pattern_tsp_num of all PIDs with pattern search.
  1086. */
  1087. min_pattern_tsp_num = (u64)-1;
  1088. i = feed->rec_info->idx_info.pattern_search_feeds_num;
  1089. list_for_each_entry(tmp_feed, &demux->feed_list, list_head) {
  1090. if ((tmp_feed->state != DMX_STATE_GO) ||
  1091. (tmp_feed->type != DMX_TYPE_TS) ||
  1092. (tmp_feed->feed.ts.buffer.ringbuff !=
  1093. feed->feed.ts.buffer.ringbuff))
  1094. continue;
  1095. if ((tmp_feed->last_pattern_tsp_num != (u64)-1) &&
  1096. ((min_pattern_tsp_num == (u64)-1) ||
  1097. (tmp_feed->last_pattern_tsp_num <
  1098. min_pattern_tsp_num)))
  1099. min_pattern_tsp_num = tmp_feed->last_pattern_tsp_num;
  1100. if (tmp_feed->pattern_num) {
  1101. i--;
  1102. if (i == 0)
  1103. break;
  1104. }
  1105. }
  1106. feed->rec_info->idx_info.min_pattern_tsp_num = min_pattern_tsp_num;
  1107. /* notify all index entries up to min_pattern_tsp_num */
  1108. dvb_dmx_notify_indexing(feed);
  1109. }
  1110. static inline void dvb_dmx_swfilter_output_packet(
  1111. struct dvb_demux_feed *feed,
  1112. const u8 *buf,
  1113. const u8 timestamp[TIMESTAMP_LEN])
  1114. {
  1115. /*
  1116. * if we output 192 packet with timestamp at head of packet,
  1117. * output the timestamp now before the 188 TS packet
  1118. */
  1119. if (feed->tsp_out_format == DMX_TSP_FORMAT_192_HEAD)
  1120. feed->cb.ts(timestamp, TIMESTAMP_LEN, NULL,
  1121. 0, &feed->feed.ts);
  1122. feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts);
  1123. /*
  1124. * if we output 192 packet with timestamp at tail of packet,
  1125. * output the timestamp now after the 188 TS packet
  1126. */
  1127. if (feed->tsp_out_format == DMX_TSP_FORMAT_192_TAIL)
  1128. feed->cb.ts(timestamp, TIMESTAMP_LEN, NULL,
  1129. 0, &feed->feed.ts);
  1130. if (feed->idx_params.enable)
  1131. dvb_dmx_index(feed, buf, timestamp);
  1132. feed->rec_info->ts_output_count++;
  1133. }
  1134. static inline void dvb_dmx_configure_decoder_fullness(
  1135. struct dvb_demux *demux,
  1136. int initialize)
  1137. {
  1138. struct dvb_demux_feed *feed;
  1139. int j;
  1140. for (j = 0; j < demux->feednum; j++) {
  1141. feed = &demux->feed[j];
  1142. if ((feed->state != DMX_STATE_GO) ||
  1143. (feed->type != DMX_TYPE_TS) ||
  1144. !(feed->ts_type & TS_DECODER))
  1145. continue;
  1146. if (initialize) {
  1147. if (demux->decoder_fullness_init)
  1148. demux->decoder_fullness_init(feed);
  1149. } else {
  1150. if (demux->decoder_fullness_abort)
  1151. demux->decoder_fullness_abort(feed);
  1152. }
  1153. }
  1154. }
  1155. static inline int dvb_dmx_swfilter_buffer_check(
  1156. struct dvb_demux *demux,
  1157. u16 pid)
  1158. {
  1159. int desired_space;
  1160. int ret;
  1161. struct dmx_ts_feed *ts;
  1162. struct dvb_demux_filter *f;
  1163. struct dvb_demux_feed *feed;
  1164. int was_locked;
  1165. int i, j;
  1166. if (likely(spin_is_locked(&demux->lock)))
  1167. was_locked = 1;
  1168. else
  1169. was_locked = 0;
  1170. /*
  1171. * Check that there's enough free space for data output.
  1172. * If there no space, wait for it (block).
  1173. * Since this function is called while spinlock
  1174. * is acquired, the lock should be released first.
  1175. * Once we get control back, lock is acquired back
  1176. * and checks that the filter is still valid.
  1177. */
  1178. for (j = 0; j < demux->feednum; j++) {
  1179. feed = &demux->feed[j];
  1180. if (demux->sw_filter_abort)
  1181. return -ENODEV;
  1182. if ((feed->state != DMX_STATE_GO) ||
  1183. ((feed->pid != pid) && (feed->pid != 0x2000)))
  1184. continue;
  1185. if (feed->secure_mode.is_secured &&
  1186. !dvb_dmx_is_rec_feed(feed))
  1187. return 0;
  1188. if (feed->type == DMX_TYPE_TS) {
  1189. desired_space = 192; /* upper bound */
  1190. ts = &feed->feed.ts;
  1191. if (feed->ts_type & TS_PACKET) {
  1192. if (likely(was_locked))
  1193. spin_unlock(&demux->lock);
  1194. ret = demux->buffer_ctrl.ts(ts,
  1195. desired_space, 1);
  1196. if (likely(was_locked))
  1197. spin_lock(&demux->lock);
  1198. if (ret < 0)
  1199. continue;
  1200. }
  1201. if (demux->sw_filter_abort)
  1202. return -ENODEV;
  1203. if (!ts->is_filtering)
  1204. continue;
  1205. if ((feed->ts_type & TS_DECODER) &&
  1206. (demux->decoder_fullness_wait)) {
  1207. if (likely(was_locked))
  1208. spin_unlock(&demux->lock);
  1209. ret = demux->decoder_fullness_wait(
  1210. feed,
  1211. desired_space);
  1212. if (likely(was_locked))
  1213. spin_lock(&demux->lock);
  1214. if (ret < 0)
  1215. continue;
  1216. }
  1217. continue;
  1218. }
  1219. /* else - section case */
  1220. desired_space = feed->feed.sec.tsfeedp + 188; /* upper bound */
  1221. for (i = 0; i < demux->filternum; i++) {
  1222. if (demux->sw_filter_abort)
  1223. return -EPERM;
  1224. if (!feed->feed.sec.is_filtering)
  1225. continue;
  1226. f = &demux->filter[i];
  1227. if (f->feed != feed)
  1228. continue;
  1229. if (likely(was_locked))
  1230. spin_unlock(&demux->lock);
  1231. ret = demux->buffer_ctrl.sec(&f->filter,
  1232. desired_space, 1);
  1233. if (likely(was_locked))
  1234. spin_lock(&demux->lock);
  1235. if (ret < 0)
  1236. break;
  1237. }
  1238. }
  1239. return 0;
  1240. }
  1241. static inline void dvb_dmx_swfilter_packet_type(struct dvb_demux_feed *feed,
  1242. const u8 *buf, const u8 timestamp[TIMESTAMP_LEN])
  1243. {
  1244. u16 pid = ts_pid(buf);
  1245. u8 scrambling_bits = ts_scrambling_ctrl(buf);
  1246. struct dmx_data_ready dmx_data_ready;
  1247. /*
  1248. * Notify on scrambling status change only when we move
  1249. * from clear (0) to non-clear and vise-versa
  1250. */
  1251. if ((scrambling_bits && !feed->scrambling_bits) ||
  1252. (!scrambling_bits && feed->scrambling_bits)) {
  1253. dmx_data_ready.status = DMX_OK_SCRAMBLING_STATUS;
  1254. dmx_data_ready.data_length = 0;
  1255. dmx_data_ready.scrambling_bits.pid = pid;
  1256. dmx_data_ready.scrambling_bits.old_value =
  1257. feed->scrambling_bits;
  1258. dmx_data_ready.scrambling_bits.new_value = scrambling_bits;
  1259. if (feed->type == DMX_TYPE_SEC)
  1260. dvb_dmx_notify_section_event(feed, &dmx_data_ready, 0);
  1261. else if (feed->feed.ts.is_filtering)
  1262. feed->data_ready_cb.ts(&feed->feed.ts, &dmx_data_ready);
  1263. }
  1264. feed->scrambling_bits = scrambling_bits;
  1265. switch (feed->type) {
  1266. case DMX_TYPE_TS:
  1267. if (!feed->feed.ts.is_filtering)
  1268. break;
  1269. if (feed->ts_type & TS_PACKET) {
  1270. if (feed->ts_type & TS_PAYLOAD_ONLY) {
  1271. if (!feed->secure_mode.is_secured)
  1272. dvb_dmx_swfilter_payload(feed, buf);
  1273. } else {
  1274. dvb_dmx_swfilter_output_packet(feed,
  1275. buf, timestamp);
  1276. }
  1277. }
  1278. if ((feed->ts_type & TS_DECODER) &&
  1279. !feed->secure_mode.is_secured)
  1280. if (feed->demux->write_to_decoder)
  1281. feed->demux->write_to_decoder(feed, buf, 188);
  1282. break;
  1283. case DMX_TYPE_SEC:
  1284. if (!feed->feed.sec.is_filtering ||
  1285. feed->secure_mode.is_secured)
  1286. break;
  1287. if (dvb_dmx_swfilter_section_one_packet(feed, buf) < 0)
  1288. feed->feed.sec.seclen = feed->feed.sec.secbufp = 0;
  1289. break;
  1290. default:
  1291. break;
  1292. }
  1293. }
  1294. #define DVR_FEED(f) \
  1295. (((f)->type == DMX_TYPE_TS) && \
  1296. ((f)->feed.ts.is_filtering) && \
  1297. (((f)->ts_type & (TS_PACKET | TS_DEMUX)) == TS_PACKET))
  1298. static void dvb_dmx_swfilter_one_packet(struct dvb_demux *demux, const u8 *buf,
  1299. const u8 timestamp[TIMESTAMP_LEN])
  1300. {
  1301. struct dvb_demux_feed *feed;
  1302. u16 pid = ts_pid(buf);
  1303. int dvr_done = 0;
  1304. if (dvb_demux_speedcheck) {
  1305. ktime_t cur_time;
  1306. u64 speed_bytes, speed_timedelta;
  1307. demux->speed_pkts_cnt++;
  1308. /* show speed every SPEED_PKTS_INTERVAL packets */
  1309. if (!(demux->speed_pkts_cnt % SPEED_PKTS_INTERVAL)) {
  1310. cur_time = ktime_get();
  1311. if (ktime_to_ns(demux->speed_last_time) != 0) {
  1312. speed_bytes = (u64)demux->speed_pkts_cnt
  1313. * 188 * 8;
  1314. /* convert to 1024 basis */
  1315. speed_bytes = 1000 * div64_u64(speed_bytes,
  1316. 1024);
  1317. speed_timedelta = ktime_ms_delta(cur_time,
  1318. demux->speed_last_time);
  1319. pr_info("TS speed %llu Kbits/sec\n",
  1320. div64_u64(speed_bytes, speed_timedelta));
  1321. }
  1322. demux->speed_last_time = cur_time;
  1323. demux->speed_pkts_cnt = 0;
  1324. }
  1325. }
  1326. if (buf[1] & 0x80) {
  1327. dprintk_tscheck("TEI detected. PID=0x%x data1=0x%x\n", pid,
  1328. buf[1]);
  1329. /*
  1330. * data in this packet can't be trusted - drop it unless
  1331. * module option dvb_demux_feed_err_pkts is set
  1332. */
  1333. if (!dvb_demux_feed_err_pkts)
  1334. return;
  1335. } else /* if TEI bit is set, pid may be wrong- skip pkt counter */
  1336. if (demux->cnt_storage && dvb_demux_tscheck) {
  1337. /* check pkt counter */
  1338. if (pid < MAX_PID) {
  1339. if (buf[3] & 0x10)
  1340. demux->cnt_storage[pid] =
  1341. (demux->cnt_storage[pid] + 1) &
  1342. 0xf;
  1343. if ((buf[3] & 0xf) != demux->cnt_storage[pid]) {
  1344. dprintk_tscheck(
  1345. "TS packet counter mismatch. PID=0x%x expected 0x%x got 0x%x\n",
  1346. pid, demux->cnt_storage[pid],
  1347. buf[3] & 0xf);
  1348. demux->cnt_storage[pid] = buf[3] & 0xf;
  1349. }
  1350. }
  1351. /* end check */
  1352. }
  1353. if (demux->playback_mode == DMX_PB_MODE_PULL)
  1354. if (dvb_dmx_swfilter_buffer_check(demux, pid) < 0)
  1355. return;
  1356. list_for_each_entry(feed, &demux->feed_list, list_head) {
  1357. if ((feed->pid != pid) && (feed->pid != 0x2000))
  1358. continue;
  1359. /*
  1360. * copy each packet only once to the dvr device, even
  1361. * if a PID is in multiple filters (e.g. video + PCR)
  1362. */
  1363. if ((DVR_FEED(feed)) && (dvr_done++))
  1364. continue;
  1365. if (feed->pid == pid)
  1366. dvb_dmx_swfilter_packet_type(feed, buf, timestamp);
  1367. else if ((feed->pid == 0x2000) &&
  1368. (feed->feed.ts.is_filtering))
  1369. dvb_dmx_swfilter_output_packet(feed, buf, timestamp);
  1370. }
  1371. }
  1372. void dvb_dmx_swfilter_packet(struct dvb_demux *demux, const u8 *buf,
  1373. const u8 timestamp[TIMESTAMP_LEN])
  1374. {
  1375. spin_lock(&demux->lock);
  1376. dvb_dmx_swfilter_one_packet(demux, buf, timestamp);
  1377. spin_unlock(&demux->lock);
  1378. }
  1379. EXPORT_SYMBOL(dvb_dmx_swfilter_packet);
  1380. void dvb_dmx_swfilter_packets(struct dvb_demux *demux, const u8 *buf,
  1381. size_t count)
  1382. {
  1383. ktime_t pre_time = ktime_set(0, 0);
  1384. u8 timestamp[TIMESTAMP_LEN] = {0};
  1385. if (dvb_demux_performancecheck)
  1386. pre_time = ktime_get();
  1387. spin_lock(&demux->lock);
  1388. demux->sw_filter_abort = 0;
  1389. dvb_dmx_configure_decoder_fullness(demux, 1);
  1390. while (count--) {
  1391. if (buf[0] == 0x47)
  1392. dvb_dmx_swfilter_one_packet(demux, buf, timestamp);
  1393. buf += 188;
  1394. }
  1395. spin_unlock(&demux->lock);
  1396. if (dvb_demux_performancecheck)
  1397. demux->total_process_time += dvb_dmx_calc_time_delta(pre_time);
  1398. }
  1399. EXPORT_SYMBOL(dvb_dmx_swfilter_packets);
  1400. static inline int find_next_packet(const u8 *buf, int pos, size_t count,
  1401. const int pktsize, const int leadingbytes)
  1402. {
  1403. int start = pos, lost;
  1404. while (pos < count) {
  1405. if ((buf[pos] == 0x47 && !leadingbytes) ||
  1406. (pktsize == 204 && buf[pos] == 0xB8) ||
  1407. (pktsize == 192 && leadingbytes &&
  1408. (pos+leadingbytes < count) &&
  1409. buf[pos+leadingbytes] == 0x47))
  1410. break;
  1411. pos++;
  1412. }
  1413. lost = pos - start;
  1414. if (lost) {
  1415. /* This garbage is part of a valid packet? */
  1416. int backtrack = pos - pktsize;
  1417. if (backtrack >= 0 && (buf[backtrack] == 0x47 ||
  1418. (pktsize == 204 && buf[backtrack] == 0xB8) ||
  1419. (pktsize == 192 &&
  1420. buf[backtrack+leadingbytes] == 0x47)))
  1421. return backtrack;
  1422. }
  1423. return pos;
  1424. }
  1425. /* Filter all pktsize= 188 or 204 sized packets and skip garbage. */
  1426. static inline void _dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf,
  1427. size_t count, const int pktsize, const int leadingbytes)
  1428. {
  1429. int p = 0, i, j;
  1430. const u8 *q;
  1431. ktime_t pre_time;
  1432. u8 timestamp[TIMESTAMP_LEN];
  1433. if (dvb_demux_performancecheck)
  1434. pre_time = ktime_get();
  1435. spin_lock(&demux->lock);
  1436. demux->sw_filter_abort = 0;
  1437. dvb_dmx_configure_decoder_fullness(demux, 1);
  1438. if (demux->tsbufp) { /* tsbuf[0] is now 0x47. */
  1439. i = demux->tsbufp;
  1440. j = pktsize - i;
  1441. if (count < j) {
  1442. memcpy(&demux->tsbuf[i], buf, count);
  1443. demux->tsbufp += count;
  1444. goto bailout;
  1445. }
  1446. memcpy(&demux->tsbuf[i], buf, j);
  1447. if (pktsize == 192) {
  1448. if (leadingbytes)
  1449. memcpy(timestamp, &demux->tsbuf[p],
  1450. TIMESTAMP_LEN);
  1451. else
  1452. memcpy(timestamp, &demux->tsbuf[188],
  1453. TIMESTAMP_LEN);
  1454. } else {
  1455. memset(timestamp, 0, TIMESTAMP_LEN);
  1456. }
  1457. if (pktsize == 192 &&
  1458. leadingbytes &&
  1459. demux->tsbuf[leadingbytes] == 0x47) /* double check */
  1460. dvb_dmx_swfilter_one_packet(demux,
  1461. demux->tsbuf + TIMESTAMP_LEN, timestamp);
  1462. else if (demux->tsbuf[0] == 0x47) /* double check */
  1463. dvb_dmx_swfilter_one_packet(demux,
  1464. demux->tsbuf, timestamp);
  1465. demux->tsbufp = 0;
  1466. p += j;
  1467. }
  1468. while (1) {
  1469. p = find_next_packet(buf, p, count, pktsize, leadingbytes);
  1470. if (demux->sw_filter_abort)
  1471. goto bailout;
  1472. if (p >= count)
  1473. break;
  1474. if (count - p < pktsize)
  1475. break;
  1476. q = &buf[p];
  1477. if (pktsize == 204 && (*q == 0xB8)) {
  1478. memcpy(demux->tsbuf, q, 188);
  1479. demux->tsbuf[0] = 0x47;
  1480. q = demux->tsbuf;
  1481. }
  1482. if (pktsize == 192) {
  1483. if (leadingbytes) {
  1484. q = &buf[p+leadingbytes];
  1485. memcpy(timestamp, &buf[p], TIMESTAMP_LEN);
  1486. } else {
  1487. memcpy(timestamp, &buf[p+188], TIMESTAMP_LEN);
  1488. }
  1489. } else {
  1490. memset(timestamp, 0, TIMESTAMP_LEN);
  1491. }
  1492. dvb_dmx_swfilter_one_packet(demux, q, timestamp);
  1493. p += pktsize;
  1494. }
  1495. i = count - p;
  1496. if (i) {
  1497. memcpy(demux->tsbuf, &buf[p], i);
  1498. demux->tsbufp = i;
  1499. if (pktsize == 204 && demux->tsbuf[0] == 0xB8)
  1500. demux->tsbuf[0] = 0x47;
  1501. }
  1502. bailout:
  1503. spin_unlock(&demux->lock);
  1504. if (dvb_demux_performancecheck)
  1505. demux->total_process_time += dvb_dmx_calc_time_delta(pre_time);
  1506. }
  1507. void dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf, size_t count)
  1508. {
  1509. _dvb_dmx_swfilter(demux, buf, count, 188, 0);
  1510. }
  1511. EXPORT_SYMBOL(dvb_dmx_swfilter);
  1512. void dvb_dmx_swfilter_204(struct dvb_demux *demux, const u8 *buf, size_t count)
  1513. {
  1514. _dvb_dmx_swfilter(demux, buf, count, 204, 0);
  1515. }
  1516. EXPORT_SYMBOL(dvb_dmx_swfilter_204);
  1517. void dvb_dmx_swfilter_raw(struct dvb_demux *demux, const u8 *buf, size_t count)
  1518. {
  1519. spin_lock(&demux->lock);
  1520. demux->feed->cb.ts(buf, count, NULL, 0, &demux->feed->feed.ts);
  1521. spin_unlock(&demux->lock);
  1522. }
  1523. EXPORT_SYMBOL(dvb_dmx_swfilter_raw);
  1524. void dvb_dmx_swfilter_format(
  1525. struct dvb_demux *demux,
  1526. const u8 *buf,
  1527. size_t count,
  1528. enum dmx_tsp_format_t tsp_format)
  1529. {
  1530. switch (tsp_format) {
  1531. case DMX_TSP_FORMAT_188:
  1532. _dvb_dmx_swfilter(demux, buf, count, 188, 0);
  1533. break;
  1534. case DMX_TSP_FORMAT_192_TAIL:
  1535. _dvb_dmx_swfilter(demux, buf, count, 192, 0);
  1536. break;
  1537. case DMX_TSP_FORMAT_192_HEAD:
  1538. _dvb_dmx_swfilter(demux, buf, count, 192, TIMESTAMP_LEN);
  1539. break;
  1540. case DMX_TSP_FORMAT_204:
  1541. _dvb_dmx_swfilter(demux, buf, count, 204, 0);
  1542. break;
  1543. default:
  1544. pr_err("%s: invalid TS packet format (format=%d)\n", __func__,
  1545. tsp_format);
  1546. break;
  1547. }
  1548. }
  1549. EXPORT_SYMBOL(dvb_dmx_swfilter_format);
  1550. static struct dvb_demux_filter *dvb_dmx_filter_alloc(struct dvb_demux *demux)
  1551. {
  1552. int i;
  1553. for (i = 0; i < demux->filternum; i++)
  1554. if (demux->filter[i].state == DMX_STATE_FREE)
  1555. break;
  1556. if (i == demux->filternum)
  1557. return NULL;
  1558. demux->filter[i].state = DMX_STATE_ALLOCATED;
  1559. return &demux->filter[i];
  1560. }
  1561. static struct dvb_demux_feed *dvb_dmx_feed_alloc(struct dvb_demux *demux)
  1562. {
  1563. int i;
  1564. for (i = 0; i < demux->feednum; i++)
  1565. if (demux->feed[i].state == DMX_STATE_FREE)
  1566. break;
  1567. if (i == demux->feednum)
  1568. return NULL;
  1569. demux->feed[i].state = DMX_STATE_ALLOCATED;
  1570. return &demux->feed[i];
  1571. }
  1572. const struct dvb_dmx_video_patterns *dvb_dmx_get_pattern(u64 dmx_idx_pattern)
  1573. {
  1574. switch (dmx_idx_pattern) {
  1575. case DMX_IDX_MPEG_SEQ_HEADER:
  1576. return &mpeg2_seq_hdr;
  1577. case DMX_IDX_MPEG_GOP:
  1578. return &mpeg2_gop;
  1579. case DMX_IDX_MPEG_I_FRAME_START:
  1580. return &mpeg2_iframe;
  1581. case DMX_IDX_MPEG_P_FRAME_START:
  1582. return &mpeg2_pframe;
  1583. case DMX_IDX_MPEG_B_FRAME_START:
  1584. return &mpeg2_bframe;
  1585. case DMX_IDX_H264_SPS:
  1586. return &h264_sps;
  1587. case DMX_IDX_H264_PPS:
  1588. return &h264_pps;
  1589. case DMX_IDX_H264_IDR_START:
  1590. return &h264_idr;
  1591. case DMX_IDX_H264_NON_IDR_START:
  1592. return &h264_non_idr;
  1593. case DMX_IDX_H264_IDR_ISLICE_START:
  1594. return &h264_idr_islice;
  1595. case DMX_IDX_H264_NON_IDR_PSLICE_START:
  1596. return &h264_non_idr_pslice;
  1597. case DMX_IDX_H264_NON_IDR_BSLICE_START:
  1598. return &h264_non_idr_bslice;
  1599. case DMX_IDX_H264_ACCESS_UNIT_DEL:
  1600. return &h264_non_access_unit_del;
  1601. case DMX_IDX_H264_SEI:
  1602. return &h264_non_sei;
  1603. case DMX_IDX_VC1_SEQ_HEADER:
  1604. return &vc1_seq_hdr;
  1605. case DMX_IDX_VC1_ENTRY_POINT:
  1606. return &vc1_entry_point;
  1607. case DMX_IDX_VC1_FRAME_START:
  1608. return &vc1_frame;
  1609. default:
  1610. return NULL;
  1611. }
  1612. }
  1613. EXPORT_SYMBOL(dvb_dmx_get_pattern);
  1614. static void dvb_dmx_init_idx_state(struct dvb_demux_feed *feed)
  1615. {
  1616. feed->prev_tsp_num = (u64)-1;
  1617. feed->curr_pusi_tsp_num = (u64)-1;
  1618. feed->prev_pusi_tsp_num = (u64)-1;
  1619. feed->prev_frame_valid = 0;
  1620. feed->first_frame_in_seq = 0;
  1621. feed->first_frame_in_seq_notified = 0;
  1622. feed->last_pattern_tsp_num = (u64)-1;
  1623. feed->pattern_num = 0;
  1624. memset(&feed->prefix_size, 0,
  1625. sizeof(struct dvb_dmx_video_prefix_size_masks));
  1626. if (feed->idx_params.types &
  1627. (DMX_IDX_MPEG_SEQ_HEADER |
  1628. DMX_IDX_MPEG_FIRST_SEQ_FRAME_START |
  1629. DMX_IDX_MPEG_FIRST_SEQ_FRAME_END)) {
  1630. feed->patterns[feed->pattern_num] =
  1631. dvb_dmx_get_pattern(DMX_IDX_MPEG_SEQ_HEADER);
  1632. feed->pattern_num++;
  1633. }
  1634. if ((feed->pattern_num < DVB_DMX_MAX_SEARCH_PATTERN_NUM) &&
  1635. (feed->idx_params.types & DMX_IDX_MPEG_GOP)) {
  1636. feed->patterns[feed->pattern_num] =
  1637. dvb_dmx_get_pattern(DMX_IDX_MPEG_GOP);
  1638. feed->pattern_num++;
  1639. }
  1640. /* MPEG2 I-frame */
  1641. if ((feed->pattern_num < DVB_DMX_MAX_SEARCH_PATTERN_NUM) &&
  1642. (feed->idx_params.types &
  1643. (DMX_IDX_MPEG_I_FRAME_START | DMX_IDX_MPEG_I_FRAME_END |
  1644. DMX_IDX_MPEG_P_FRAME_END | DMX_IDX_MPEG_B_FRAME_END |
  1645. DMX_IDX_MPEG_FIRST_SEQ_FRAME_START |
  1646. DMX_IDX_MPEG_FIRST_SEQ_FRAME_END))) {
  1647. feed->patterns[feed->pattern_num] =
  1648. dvb_dmx_get_pattern(DMX_IDX_MPEG_I_FRAME_START);
  1649. feed->pattern_num++;
  1650. }
  1651. /* MPEG2 P-frame */
  1652. if ((feed->pattern_num < DVB_DMX_MAX_SEARCH_PATTERN_NUM) &&
  1653. (feed->idx_params.types &
  1654. (DMX_IDX_MPEG_P_FRAME_START | DMX_IDX_MPEG_P_FRAME_END |
  1655. DMX_IDX_MPEG_I_FRAME_END | DMX_IDX_MPEG_B_FRAME_END |
  1656. DMX_IDX_MPEG_FIRST_SEQ_FRAME_START |
  1657. DMX_IDX_MPEG_FIRST_SEQ_FRAME_END))) {
  1658. feed->patterns[feed->pattern_num] =
  1659. dvb_dmx_get_pattern(DMX_IDX_MPEG_P_FRAME_START);
  1660. feed->pattern_num++;
  1661. }
  1662. /* MPEG2 B-frame */
  1663. if ((feed->pattern_num < DVB_DMX_MAX_SEARCH_PATTERN_NUM) &&
  1664. (feed->idx_params.types &
  1665. (DMX_IDX_MPEG_B_FRAME_START | DMX_IDX_MPEG_B_FRAME_END |
  1666. DMX_IDX_MPEG_I_FRAME_END | DMX_IDX_MPEG_P_FRAME_END |
  1667. DMX_IDX_MPEG_FIRST_SEQ_FRAME_START |
  1668. DMX_IDX_MPEG_FIRST_SEQ_FRAME_END))) {
  1669. feed->patterns[feed->pattern_num] =
  1670. dvb_dmx_get_pattern(DMX_IDX_MPEG_B_FRAME_START);
  1671. feed->pattern_num++;
  1672. }
  1673. if ((feed->pattern_num < DVB_DMX_MAX_SEARCH_PATTERN_NUM) &&
  1674. (feed->idx_params.types &
  1675. (DMX_IDX_H264_SPS |
  1676. DMX_IDX_H264_FIRST_SPS_FRAME_START |
  1677. DMX_IDX_H264_FIRST_SPS_FRAME_END))) {
  1678. feed->patterns[feed->pattern_num] =
  1679. dvb_dmx_get_pattern(DMX_IDX_H264_SPS);
  1680. feed->pattern_num++;
  1681. }
  1682. if ((feed->pattern_num < DVB_DMX_MAX_SEARCH_PATTERN_NUM) &&
  1683. (feed->idx_params.types & DMX_IDX_H264_PPS)) {
  1684. feed->patterns[feed->pattern_num] =
  1685. dvb_dmx_get_pattern(DMX_IDX_H264_PPS);
  1686. feed->pattern_num++;
  1687. }
  1688. /* H264 IDR */
  1689. if ((feed->pattern_num < DVB_DMX_MAX_SEARCH_PATTERN_NUM) &&
  1690. (feed->idx_params.types &
  1691. (DMX_IDX_H264_IDR_START | DMX_IDX_H264_IDR_END |
  1692. DMX_IDX_H264_NON_IDR_END |
  1693. DMX_IDX_H264_FIRST_SPS_FRAME_START |
  1694. DMX_IDX_H264_FIRST_SPS_FRAME_END))) {
  1695. feed->patterns[feed->pattern_num] =
  1696. dvb_dmx_get_pattern(DMX_IDX_H264_IDR_START);
  1697. feed->pattern_num++;
  1698. }
  1699. /* H264 non-IDR */
  1700. if ((feed->pattern_num < DVB_DMX_MAX_SEARCH_PATTERN_NUM) &&
  1701. (feed->idx_params.types &
  1702. (DMX_IDX_H264_NON_IDR_START | DMX_IDX_H264_NON_IDR_END |
  1703. DMX_IDX_H264_IDR_END |
  1704. DMX_IDX_H264_FIRST_SPS_FRAME_START |
  1705. DMX_IDX_H264_FIRST_SPS_FRAME_END))) {
  1706. feed->patterns[feed->pattern_num] =
  1707. dvb_dmx_get_pattern(DMX_IDX_H264_NON_IDR_START);
  1708. feed->pattern_num++;
  1709. }
  1710. /* H264 IDR ISlice */
  1711. if ((feed->pattern_num < DVB_DMX_MAX_SEARCH_PATTERN_NUM) &&
  1712. (feed->idx_params.types &
  1713. (DMX_IDX_H264_IDR_ISLICE_START | DMX_IDX_H264_IDR_END |
  1714. DMX_IDX_H264_NON_IDR_END |
  1715. DMX_IDX_H264_FIRST_SPS_FRAME_START |
  1716. DMX_IDX_H264_FIRST_SPS_FRAME_END))) {
  1717. feed->patterns[feed->pattern_num] =
  1718. dvb_dmx_get_pattern(DMX_IDX_H264_IDR_ISLICE_START);
  1719. feed->pattern_num++;
  1720. }
  1721. /* H264 non-IDR PSlice */
  1722. if ((feed->pattern_num < DVB_DMX_MAX_SEARCH_PATTERN_NUM) &&
  1723. (feed->idx_params.types &
  1724. (DMX_IDX_H264_NON_IDR_PSLICE_START | DMX_IDX_H264_NON_IDR_END |
  1725. DMX_IDX_H264_IDR_END |
  1726. DMX_IDX_H264_FIRST_SPS_FRAME_START |
  1727. DMX_IDX_H264_FIRST_SPS_FRAME_END))) {
  1728. feed->patterns[feed->pattern_num] =
  1729. dvb_dmx_get_pattern(DMX_IDX_H264_NON_IDR_PSLICE_START);
  1730. feed->pattern_num++;
  1731. }
  1732. /* H264 non-IDR BSlice */
  1733. if ((feed->pattern_num < DVB_DMX_MAX_SEARCH_PATTERN_NUM) &&
  1734. (feed->idx_params.types &
  1735. (DMX_IDX_H264_NON_IDR_BSLICE_START | DMX_IDX_H264_NON_IDR_END |
  1736. DMX_IDX_H264_IDR_END |
  1737. DMX_IDX_H264_FIRST_SPS_FRAME_START |
  1738. DMX_IDX_H264_FIRST_SPS_FRAME_END))) {
  1739. feed->patterns[feed->pattern_num] =
  1740. dvb_dmx_get_pattern(DMX_IDX_H264_NON_IDR_BSLICE_START);
  1741. feed->pattern_num++;
  1742. }
  1743. if ((feed->pattern_num < DVB_DMX_MAX_SEARCH_PATTERN_NUM) &&
  1744. (feed->idx_params.types & DMX_IDX_H264_ACCESS_UNIT_DEL)) {
  1745. feed->patterns[feed->pattern_num] =
  1746. dvb_dmx_get_pattern(DMX_IDX_H264_ACCESS_UNIT_DEL);
  1747. feed->pattern_num++;
  1748. }
  1749. if ((feed->pattern_num < DVB_DMX_MAX_SEARCH_PATTERN_NUM) &&
  1750. (feed->idx_params.types & DMX_IDX_H264_SEI)) {
  1751. feed->patterns[feed->pattern_num] =
  1752. dvb_dmx_get_pattern(DMX_IDX_H264_SEI);
  1753. feed->pattern_num++;
  1754. }
  1755. if ((feed->pattern_num < DVB_DMX_MAX_SEARCH_PATTERN_NUM) &&
  1756. (feed->idx_params.types &
  1757. (DMX_IDX_VC1_SEQ_HEADER |
  1758. DMX_IDX_VC1_FIRST_SEQ_FRAME_START |
  1759. DMX_IDX_VC1_FIRST_SEQ_FRAME_END))) {
  1760. feed->patterns[feed->pattern_num] =
  1761. dvb_dmx_get_pattern(DMX_IDX_VC1_SEQ_HEADER);
  1762. feed->pattern_num++;
  1763. }
  1764. if ((feed->pattern_num < DVB_DMX_MAX_SEARCH_PATTERN_NUM) &&
  1765. (feed->idx_params.types & DMX_IDX_VC1_ENTRY_POINT)) {
  1766. feed->patterns[feed->pattern_num] =
  1767. dvb_dmx_get_pattern(DMX_IDX_VC1_ENTRY_POINT);
  1768. feed->pattern_num++;
  1769. }
  1770. /* VC1 frame */
  1771. if ((feed->pattern_num < DVB_DMX_MAX_SEARCH_PATTERN_NUM) &&
  1772. (feed->idx_params.types &
  1773. (DMX_IDX_VC1_FRAME_START | DMX_IDX_VC1_FRAME_END |
  1774. DMX_IDX_VC1_FIRST_SEQ_FRAME_START |
  1775. DMX_IDX_VC1_FIRST_SEQ_FRAME_END))) {
  1776. feed->patterns[feed->pattern_num] =
  1777. dvb_dmx_get_pattern(DMX_IDX_VC1_FRAME_START);
  1778. feed->pattern_num++;
  1779. }
  1780. if (feed->pattern_num)
  1781. feed->rec_info->idx_info.pattern_search_feeds_num++;
  1782. }
  1783. static struct dvb_demux_rec_info *dvb_dmx_alloc_rec_info(
  1784. struct dmx_ts_feed *ts_feed)
  1785. {
  1786. int i;
  1787. struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
  1788. struct dvb_demux *demux = feed->demux;
  1789. struct dvb_demux_rec_info *rec_info;
  1790. struct dvb_demux_feed *tmp_feed;
  1791. /* check if this feed share recording buffer with other active feeds */
  1792. list_for_each_entry(tmp_feed, &demux->feed_list, list_head) {
  1793. if ((tmp_feed->state == DMX_STATE_GO) &&
  1794. (tmp_feed->type == DMX_TYPE_TS) &&
  1795. (tmp_feed != feed) &&
  1796. (tmp_feed->feed.ts.buffer.ringbuff ==
  1797. ts_feed->buffer.ringbuff)) {
  1798. /* indexing information is shared between the feeds */
  1799. tmp_feed->rec_info->ref_count++;
  1800. return tmp_feed->rec_info;
  1801. }
  1802. }
  1803. /* Need to allocate a new indexing info */
  1804. for (i = 0; i < demux->feednum; i++)
  1805. if (!demux->rec_info_pool[i].ref_count)
  1806. break;
  1807. if (i == demux->feednum)
  1808. return NULL;
  1809. rec_info = &demux->rec_info_pool[i];
  1810. rec_info->ref_count++;
  1811. INIT_LIST_HEAD(&rec_info->idx_info.free_list);
  1812. INIT_LIST_HEAD(&rec_info->idx_info.ready_list);
  1813. for (i = 0; i < DMX_IDX_EVENT_QUEUE_SIZE; i++)
  1814. list_add(&rec_info->idx_info.events[i].next,
  1815. &rec_info->idx_info.free_list);
  1816. rec_info->ts_output_count = 0;
  1817. rec_info->idx_info.min_pattern_tsp_num = (u64)-1;
  1818. rec_info->idx_info.pattern_search_feeds_num = 0;
  1819. rec_info->idx_info.indexing_feeds_num = 0;
  1820. return rec_info;
  1821. }
  1822. static void dvb_dmx_free_rec_info(struct dmx_ts_feed *ts_feed)
  1823. {
  1824. struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
  1825. if (!feed->rec_info || !feed->rec_info->ref_count) {
  1826. pr_err("%s: invalid idx info state\n", __func__);
  1827. return;
  1828. }
  1829. feed->rec_info->ref_count--;
  1830. }
  1831. static int dvb_demux_feed_find(struct dvb_demux_feed *feed)
  1832. {
  1833. struct dvb_demux_feed *entry;
  1834. list_for_each_entry(entry, &feed->demux->feed_list, list_head)
  1835. if (entry == feed)
  1836. return 1;
  1837. return 0;
  1838. }
  1839. static void dvb_demux_feed_add(struct dvb_demux_feed *feed)
  1840. {
  1841. spin_lock_irq(&feed->demux->lock);
  1842. if (dvb_demux_feed_find(feed)) {
  1843. pr_err("%s: feed already in list (type=%x state=%x pid=%x)\n",
  1844. __func__, feed->type, feed->state, feed->pid);
  1845. goto out;
  1846. }
  1847. list_add(&feed->list_head, &feed->demux->feed_list);
  1848. out:
  1849. spin_unlock_irq(&feed->demux->lock);
  1850. }
  1851. static void dvb_demux_feed_del(struct dvb_demux_feed *feed)
  1852. {
  1853. spin_lock_irq(&feed->demux->lock);
  1854. if (!(dvb_demux_feed_find(feed))) {
  1855. pr_err("%s: feed not in list (type=%x state=%x pid=%x)\n",
  1856. __func__, feed->type, feed->state, feed->pid);
  1857. goto out;
  1858. }
  1859. list_del(&feed->list_head);
  1860. out:
  1861. spin_unlock_irq(&feed->demux->lock);
  1862. }
  1863. static int dmx_ts_feed_set(struct dmx_ts_feed *ts_feed, u16 pid, int ts_type,
  1864. enum dmx_ts_pes pes_type,
  1865. size_t circular_buffer_size, ktime_t timeout)
  1866. {
  1867. struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
  1868. struct dvb_demux *demux = feed->demux;
  1869. if (pid > DMX_MAX_PID)
  1870. return -EINVAL;
  1871. if (mutex_lock_interruptible(&demux->mutex))
  1872. return -ERESTARTSYS;
  1873. if (ts_type & TS_DECODER) {
  1874. if (pes_type >= DMX_PES_OTHER) {
  1875. mutex_unlock(&demux->mutex);
  1876. return -EINVAL;
  1877. }
  1878. if (demux->pesfilter[pes_type] &&
  1879. demux->pesfilter[pes_type] != feed) {
  1880. mutex_unlock(&demux->mutex);
  1881. return -EINVAL;
  1882. }
  1883. demux->pesfilter[pes_type] = feed;
  1884. demux->pids[pes_type] = pid;
  1885. }
  1886. dvb_demux_feed_add(feed);
  1887. feed->pid = pid;
  1888. feed->buffer_size = circular_buffer_size;
  1889. feed->timeout = timeout;
  1890. feed->ts_type = ts_type;
  1891. feed->pes_type = pes_type;
  1892. if (feed->buffer_size) {
  1893. #ifdef NOBUFS
  1894. feed->buffer = NULL;
  1895. #else
  1896. feed->buffer = vmalloc(feed->buffer_size);
  1897. if (!feed->buffer) {
  1898. mutex_unlock(&demux->mutex);
  1899. return -ENOMEM;
  1900. }
  1901. #endif
  1902. }
  1903. feed->state = DMX_STATE_READY;
  1904. mutex_unlock(&demux->mutex);
  1905. return 0;
  1906. }
  1907. static int dmx_ts_feed_start_filtering(struct dmx_ts_feed *ts_feed)
  1908. {
  1909. struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
  1910. struct dvb_demux *demux = feed->demux;
  1911. int ret;
  1912. if (mutex_lock_interruptible(&demux->mutex))
  1913. return -ERESTARTSYS;
  1914. if (feed->state != DMX_STATE_READY || feed->type != DMX_TYPE_TS) {
  1915. mutex_unlock(&demux->mutex);
  1916. return -EINVAL;
  1917. }
  1918. if (!demux->start_feed) {
  1919. mutex_unlock(&demux->mutex);
  1920. return -ENODEV;
  1921. }
  1922. feed->first_cc = 1;
  1923. feed->scrambling_bits = 0;
  1924. if ((feed->ts_type & TS_PACKET) &&
  1925. !(feed->ts_type & TS_PAYLOAD_ONLY)) {
  1926. feed->rec_info = dvb_dmx_alloc_rec_info(ts_feed);
  1927. if (!feed->rec_info) {
  1928. mutex_unlock(&demux->mutex);
  1929. return -ENOMEM;
  1930. }
  1931. if (feed->idx_params.enable) {
  1932. dvb_dmx_init_idx_state(feed);
  1933. feed->rec_info->idx_info.indexing_feeds_num++;
  1934. if (demux->set_indexing)
  1935. demux->set_indexing(feed);
  1936. }
  1937. } else {
  1938. feed->pattern_num = 0;
  1939. feed->rec_info = NULL;
  1940. }
  1941. ret = demux->start_feed(feed);
  1942. if (ret < 0) {
  1943. if ((feed->ts_type & TS_PACKET) &&
  1944. !(feed->ts_type & TS_PAYLOAD_ONLY)) {
  1945. dvb_dmx_free_rec_info(ts_feed);
  1946. feed->rec_info = NULL;
  1947. }
  1948. mutex_unlock(&demux->mutex);
  1949. return ret;
  1950. }
  1951. spin_lock_irq(&demux->lock);
  1952. ts_feed->is_filtering = 1;
  1953. feed->state = DMX_STATE_GO;
  1954. spin_unlock_irq(&demux->lock);
  1955. mutex_unlock(&demux->mutex);
  1956. return 0;
  1957. }
  1958. static int dmx_ts_feed_stop_filtering(struct dmx_ts_feed *ts_feed)
  1959. {
  1960. struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
  1961. struct dvb_demux *demux = feed->demux;
  1962. int ret;
  1963. mutex_lock(&demux->mutex);
  1964. if (feed->state < DMX_STATE_GO) {
  1965. mutex_unlock(&demux->mutex);
  1966. return -EINVAL;
  1967. }
  1968. if (!demux->stop_feed) {
  1969. mutex_unlock(&demux->mutex);
  1970. return -ENODEV;
  1971. }
  1972. ret = demux->stop_feed(feed);
  1973. spin_lock_irq(&demux->lock);
  1974. ts_feed->is_filtering = 0;
  1975. feed->state = DMX_STATE_ALLOCATED;
  1976. spin_unlock_irq(&demux->lock);
  1977. if (feed->rec_info) {
  1978. if (feed->pattern_num)
  1979. feed->rec_info->idx_info.pattern_search_feeds_num--;
  1980. if (feed->idx_params.enable)
  1981. feed->rec_info->idx_info.indexing_feeds_num--;
  1982. dvb_dmx_free_rec_info(ts_feed);
  1983. feed->rec_info = NULL;
  1984. }
  1985. mutex_unlock(&demux->mutex);
  1986. return ret;
  1987. }
  1988. static int dmx_ts_feed_decoder_buff_status(struct dmx_ts_feed *ts_feed,
  1989. struct dmx_buffer_status *dmx_buffer_status)
  1990. {
  1991. struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
  1992. struct dvb_demux *demux = feed->demux;
  1993. int ret;
  1994. mutex_lock(&demux->mutex);
  1995. if (feed->state < DMX_STATE_GO) {
  1996. mutex_unlock(&demux->mutex);
  1997. return -EINVAL;
  1998. }
  1999. if (!demux->decoder_buffer_status) {
  2000. mutex_unlock(&demux->mutex);
  2001. return -ENODEV;
  2002. }
  2003. ret = demux->decoder_buffer_status(feed, dmx_buffer_status);
  2004. mutex_unlock(&demux->mutex);
  2005. return ret;
  2006. }
  2007. static int dmx_ts_feed_reuse_decoder_buffer(struct dmx_ts_feed *ts_feed,
  2008. int cookie)
  2009. {
  2010. struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
  2011. struct dvb_demux *demux = feed->demux;
  2012. int ret;
  2013. mutex_lock(&demux->mutex);
  2014. if (feed->state < DMX_STATE_GO) {
  2015. mutex_unlock(&demux->mutex);
  2016. return -EINVAL;
  2017. }
  2018. if (!demux->reuse_decoder_buffer) {
  2019. mutex_unlock(&demux->mutex);
  2020. return -ENODEV;
  2021. }
  2022. ret = demux->reuse_decoder_buffer(feed, cookie);
  2023. mutex_unlock(&demux->mutex);
  2024. return ret;
  2025. }
  2026. static int dmx_ts_feed_data_ready_cb(struct dmx_ts_feed *feed,
  2027. dmx_ts_data_ready_cb callback)
  2028. {
  2029. struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
  2030. struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
  2031. mutex_lock(&dvbdmx->mutex);
  2032. if (dvbdmxfeed->state == DMX_STATE_GO) {
  2033. mutex_unlock(&dvbdmx->mutex);
  2034. return -EINVAL;
  2035. }
  2036. dvbdmxfeed->data_ready_cb.ts = callback;
  2037. mutex_unlock(&dvbdmx->mutex);
  2038. return 0;
  2039. }
  2040. static int dmx_ts_set_secure_mode(struct dmx_ts_feed *feed,
  2041. struct dmx_secure_mode *secure_mode)
  2042. {
  2043. struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
  2044. struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
  2045. if (mutex_lock_interruptible(&dvbdmx->mutex))
  2046. return -ERESTARTSYS;
  2047. if (dvbdmxfeed->state == DMX_STATE_GO) {
  2048. mutex_unlock(&dvbdmx->mutex);
  2049. return -EBUSY;
  2050. }
  2051. dvbdmxfeed->secure_mode = *secure_mode;
  2052. mutex_unlock(&dvbdmx->mutex);
  2053. return 0;
  2054. }
  2055. static int dmx_ts_set_cipher_ops(struct dmx_ts_feed *feed,
  2056. struct dmx_cipher_operations *cipher_ops)
  2057. {
  2058. struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
  2059. struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
  2060. int ret = 0;
  2061. if (mutex_lock_interruptible(&dvbdmx->mutex))
  2062. return -ERESTARTSYS;
  2063. if ((dvbdmxfeed->state == DMX_STATE_GO) &&
  2064. dvbdmx->set_cipher_op)
  2065. ret = dvbdmx->set_cipher_op(dvbdmxfeed, cipher_ops);
  2066. if (!ret)
  2067. dvbdmxfeed->cipher_ops = *cipher_ops;
  2068. mutex_unlock(&dvbdmx->mutex);
  2069. return ret;
  2070. }
  2071. static int dmx_ts_set_video_codec(
  2072. struct dmx_ts_feed *ts_feed,
  2073. enum dmx_video_codec video_codec)
  2074. {
  2075. struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
  2076. feed->video_codec = video_codec;
  2077. return 0;
  2078. }
  2079. static int dmx_ts_set_idx_params(struct dmx_ts_feed *ts_feed,
  2080. struct dmx_indexing_params *idx_params)
  2081. {
  2082. struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
  2083. struct dvb_demux *dvbdmx = feed->demux;
  2084. int idx_enabled;
  2085. int ret = 0;
  2086. mutex_lock(&dvbdmx->mutex);
  2087. if ((feed->state == DMX_STATE_GO) &&
  2088. !feed->rec_info) {
  2089. mutex_unlock(&dvbdmx->mutex);
  2090. return -EINVAL;
  2091. }
  2092. idx_enabled = feed->idx_params.enable;
  2093. feed->idx_params = *idx_params;
  2094. if (feed->state == DMX_STATE_GO) {
  2095. spin_lock_irq(&dvbdmx->lock);
  2096. if (feed->pattern_num)
  2097. feed->rec_info->idx_info.pattern_search_feeds_num--;
  2098. if (idx_enabled && !idx_params->enable)
  2099. feed->rec_info->idx_info.indexing_feeds_num--;
  2100. if (!idx_enabled && idx_params->enable)
  2101. feed->rec_info->idx_info.indexing_feeds_num++;
  2102. dvb_dmx_init_idx_state(feed);
  2103. spin_unlock_irq(&dvbdmx->lock);
  2104. if (dvbdmx->set_indexing)
  2105. ret = dvbdmx->set_indexing(feed);
  2106. }
  2107. mutex_unlock(&dvbdmx->mutex);
  2108. return ret;
  2109. }
  2110. static int dvbdmx_ts_feed_oob_cmd(struct dmx_ts_feed *ts_feed,
  2111. struct dmx_oob_command *cmd)
  2112. {
  2113. struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
  2114. struct dmx_data_ready data;
  2115. struct dvb_demux *dvbdmx = feed->demux;
  2116. int ret = 0;
  2117. int secure_non_rec = feed->secure_mode.is_secured &&
  2118. !dvb_dmx_is_rec_feed(feed);
  2119. mutex_lock(&dvbdmx->mutex);
  2120. if (feed->state != DMX_STATE_GO) {
  2121. mutex_unlock(&dvbdmx->mutex);
  2122. return -EINVAL;
  2123. }
  2124. /* Decoder & non-recording secure feeds are handled by plug-in */
  2125. if ((feed->ts_type & TS_DECODER) || secure_non_rec) {
  2126. if (feed->demux->oob_command)
  2127. ret = feed->demux->oob_command(feed, cmd);
  2128. }
  2129. if (!(feed->ts_type & (TS_PAYLOAD_ONLY | TS_PACKET)) ||
  2130. secure_non_rec) {
  2131. mutex_unlock(&dvbdmx->mutex);
  2132. return ret;
  2133. }
  2134. data.data_length = 0;
  2135. switch (cmd->type) {
  2136. case DMX_OOB_CMD_EOS:
  2137. if (feed->ts_type & TS_PAYLOAD_ONLY)
  2138. dvb_dmx_check_pes_end(feed);
  2139. data.status = DMX_OK_EOS;
  2140. ret = feed->data_ready_cb.ts(&feed->feed.ts, &data);
  2141. break;
  2142. case DMX_OOB_CMD_MARKER:
  2143. data.status = DMX_OK_MARKER;
  2144. data.marker.id = cmd->params.marker.id;
  2145. ret = feed->data_ready_cb.ts(&feed->feed.ts, &data);
  2146. break;
  2147. default:
  2148. ret = -EINVAL;
  2149. break;
  2150. }
  2151. mutex_unlock(&dvbdmx->mutex);
  2152. return ret;
  2153. }
  2154. static int dvbdmx_ts_get_scrambling_bits(struct dmx_ts_feed *ts_feed,
  2155. u8 *value)
  2156. {
  2157. struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
  2158. struct dvb_demux *demux = feed->demux;
  2159. spin_lock(&demux->lock);
  2160. if (!ts_feed->is_filtering) {
  2161. spin_unlock(&demux->lock);
  2162. return -EINVAL;
  2163. }
  2164. *value = feed->scrambling_bits;
  2165. spin_unlock(&demux->lock);
  2166. return 0;
  2167. }
  2168. static int dvbdmx_ts_insertion_insert_buffer(struct dmx_ts_feed *ts_feed,
  2169. char *data, size_t size)
  2170. {
  2171. struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
  2172. struct dvb_demux *demux = feed->demux;
  2173. spin_lock(&demux->lock);
  2174. if (!ts_feed->is_filtering) {
  2175. spin_unlock(&demux->lock);
  2176. return 0;
  2177. }
  2178. feed->cb.ts(data, size, NULL, 0, ts_feed);
  2179. spin_unlock(&demux->lock);
  2180. return 0;
  2181. }
  2182. static int dmx_ts_set_tsp_out_format(
  2183. struct dmx_ts_feed *ts_feed,
  2184. enum dmx_tsp_format_t tsp_format)
  2185. {
  2186. struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
  2187. struct dvb_demux *dvbdmx = feed->demux;
  2188. mutex_lock(&dvbdmx->mutex);
  2189. if (feed->state == DMX_STATE_GO) {
  2190. mutex_unlock(&dvbdmx->mutex);
  2191. return -EINVAL;
  2192. }
  2193. feed->tsp_out_format = tsp_format;
  2194. mutex_unlock(&dvbdmx->mutex);
  2195. return 0;
  2196. }
  2197. /**
  2198. * dvbdmx_ts_reset_pes_state() - Reset the current PES length and PES counters
  2199. *
  2200. * @feed: dvb demux feed object
  2201. */
  2202. void dvbdmx_ts_reset_pes_state(struct dvb_demux_feed *feed)
  2203. {
  2204. unsigned long flags;
  2205. /*
  2206. * Reset PES state.
  2207. * PUSI seen indication is kept so we can get partial PES.
  2208. */
  2209. spin_lock_irqsave(&feed->demux->lock, flags);
  2210. feed->peslen = 0;
  2211. feed->pes_tei_counter = 0;
  2212. feed->pes_cont_err_counter = 0;
  2213. feed->pes_ts_packets_num = 0;
  2214. spin_unlock_irqrestore(&feed->demux->lock, flags);
  2215. }
  2216. EXPORT_SYMBOL(dvbdmx_ts_reset_pes_state);
  2217. static int dvbdmx_ts_flush_buffer(struct dmx_ts_feed *ts_feed, size_t length)
  2218. {
  2219. struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
  2220. struct dvb_demux *demux = feed->demux;
  2221. int ret = 0;
  2222. if (mutex_lock_interruptible(&demux->mutex))
  2223. return -ERESTARTSYS;
  2224. dvbdmx_ts_reset_pes_state(feed);
  2225. if ((feed->ts_type & TS_DECODER) && demux->flush_decoder_buffer)
  2226. /* Call decoder specific flushing if one exists */
  2227. ret = demux->flush_decoder_buffer(feed, length);
  2228. mutex_unlock(&demux->mutex);
  2229. return ret;
  2230. }
  2231. static int dvbdmx_allocate_ts_feed(struct dmx_demux *dmx,
  2232. struct dmx_ts_feed **ts_feed,
  2233. dmx_ts_cb callback)
  2234. {
  2235. struct dvb_demux *demux = (struct dvb_demux *)dmx;
  2236. struct dvb_demux_feed *feed;
  2237. if (mutex_lock_interruptible(&demux->mutex))
  2238. return -ERESTARTSYS;
  2239. if (!(feed = dvb_dmx_feed_alloc(demux))) {
  2240. mutex_unlock(&demux->mutex);
  2241. return -EBUSY;
  2242. }
  2243. feed->type = DMX_TYPE_TS;
  2244. feed->cb.ts = callback;
  2245. feed->demux = demux;
  2246. feed->pid = 0xffff;
  2247. feed->peslen = 0;
  2248. feed->pes_tei_counter = 0;
  2249. feed->pes_ts_packets_num = 0;
  2250. feed->pes_cont_err_counter = 0;
  2251. feed->secure_mode.is_secured = 0;
  2252. feed->buffer = NULL;
  2253. feed->tsp_out_format = DMX_TSP_FORMAT_188;
  2254. feed->idx_params.enable = 0;
  2255. /* default behaviour - pass first PES data even if it is
  2256. * partial PES data from previous PES that we didn't receive its header.
  2257. * Override this to 0 in your start_feed function in order to handle
  2258. * first PES differently.
  2259. */
  2260. feed->pusi_seen = 1;
  2261. (*ts_feed) = &feed->feed.ts;
  2262. (*ts_feed)->parent = dmx;
  2263. (*ts_feed)->priv = NULL;
  2264. (*ts_feed)->is_filtering = 0;
  2265. (*ts_feed)->start_filtering = dmx_ts_feed_start_filtering;
  2266. (*ts_feed)->stop_filtering = dmx_ts_feed_stop_filtering;
  2267. (*ts_feed)->set = dmx_ts_feed_set;
  2268. (*ts_feed)->set_video_codec = dmx_ts_set_video_codec;
  2269. (*ts_feed)->set_idx_params = dmx_ts_set_idx_params;
  2270. (*ts_feed)->set_tsp_out_format = dmx_ts_set_tsp_out_format;
  2271. (*ts_feed)->get_decoder_buff_status = dmx_ts_feed_decoder_buff_status;
  2272. (*ts_feed)->reuse_decoder_buffer = dmx_ts_feed_reuse_decoder_buffer;
  2273. (*ts_feed)->data_ready_cb = dmx_ts_feed_data_ready_cb;
  2274. (*ts_feed)->notify_data_read = NULL;
  2275. (*ts_feed)->set_secure_mode = dmx_ts_set_secure_mode;
  2276. (*ts_feed)->set_cipher_ops = dmx_ts_set_cipher_ops;
  2277. (*ts_feed)->oob_command = dvbdmx_ts_feed_oob_cmd;
  2278. (*ts_feed)->get_scrambling_bits = dvbdmx_ts_get_scrambling_bits;
  2279. (*ts_feed)->ts_insertion_init = NULL;
  2280. (*ts_feed)->ts_insertion_terminate = NULL;
  2281. (*ts_feed)->ts_insertion_insert_buffer =
  2282. dvbdmx_ts_insertion_insert_buffer;
  2283. (*ts_feed)->flush_buffer = dvbdmx_ts_flush_buffer;
  2284. if (!(feed->filter = dvb_dmx_filter_alloc(demux))) {
  2285. feed->state = DMX_STATE_FREE;
  2286. mutex_unlock(&demux->mutex);
  2287. return -EBUSY;
  2288. }
  2289. feed->filter->type = DMX_TYPE_TS;
  2290. feed->filter->feed = feed;
  2291. feed->filter->state = DMX_STATE_READY;
  2292. mutex_unlock(&demux->mutex);
  2293. return 0;
  2294. }
  2295. static int dvbdmx_release_ts_feed(struct dmx_demux *dmx,
  2296. struct dmx_ts_feed *ts_feed)
  2297. {
  2298. struct dvb_demux *demux = (struct dvb_demux *)dmx;
  2299. struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
  2300. mutex_lock(&demux->mutex);
  2301. if (feed->state == DMX_STATE_FREE) {
  2302. mutex_unlock(&demux->mutex);
  2303. return -EINVAL;
  2304. }
  2305. #ifndef NOBUFS
  2306. vfree(feed->buffer);
  2307. feed->buffer = NULL;
  2308. #endif
  2309. feed->state = DMX_STATE_FREE;
  2310. feed->filter->state = DMX_STATE_FREE;
  2311. ts_feed->priv = NULL;
  2312. dvb_demux_feed_del(feed);
  2313. feed->pid = 0xffff;
  2314. if (feed->ts_type & TS_DECODER && feed->pes_type < DMX_PES_OTHER)
  2315. demux->pesfilter[feed->pes_type] = NULL;
  2316. mutex_unlock(&demux->mutex);
  2317. return 0;
  2318. }
  2319. /******************************************************************************
  2320. * dmx_section_feed API calls
  2321. ******************************************************************************/
  2322. static int dmx_section_feed_allocate_filter(struct dmx_section_feed *feed,
  2323. struct dmx_section_filter **filter)
  2324. {
  2325. struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
  2326. struct dvb_demux *dvbdemux = dvbdmxfeed->demux;
  2327. struct dvb_demux_filter *dvbdmxfilter;
  2328. if (mutex_lock_interruptible(&dvbdemux->mutex))
  2329. return -ERESTARTSYS;
  2330. dvbdmxfilter = dvb_dmx_filter_alloc(dvbdemux);
  2331. if (!dvbdmxfilter) {
  2332. mutex_unlock(&dvbdemux->mutex);
  2333. return -EBUSY;
  2334. }
  2335. spin_lock_irq(&dvbdemux->lock);
  2336. *filter = &dvbdmxfilter->filter;
  2337. (*filter)->parent = feed;
  2338. (*filter)->priv = NULL;
  2339. dvbdmxfilter->feed = dvbdmxfeed;
  2340. dvbdmxfilter->type = DMX_TYPE_SEC;
  2341. dvbdmxfilter->state = DMX_STATE_READY;
  2342. dvbdmxfilter->next = dvbdmxfeed->filter;
  2343. dvbdmxfeed->filter = dvbdmxfilter;
  2344. spin_unlock_irq(&dvbdemux->lock);
  2345. mutex_unlock(&dvbdemux->mutex);
  2346. return 0;
  2347. }
  2348. static int dmx_section_feed_set(struct dmx_section_feed *feed,
  2349. u16 pid, size_t circular_buffer_size,
  2350. int check_crc)
  2351. {
  2352. struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
  2353. struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
  2354. if (pid > 0x1fff)
  2355. return -EINVAL;
  2356. if (mutex_lock_interruptible(&dvbdmx->mutex))
  2357. return -ERESTARTSYS;
  2358. dvb_demux_feed_add(dvbdmxfeed);
  2359. dvbdmxfeed->pid = pid;
  2360. dvbdmxfeed->buffer_size = circular_buffer_size;
  2361. dvbdmxfeed->feed.sec.check_crc = check_crc;
  2362. #ifdef NOBUFS
  2363. dvbdmxfeed->buffer = NULL;
  2364. #else
  2365. dvbdmxfeed->buffer = vmalloc(dvbdmxfeed->buffer_size);
  2366. if (!dvbdmxfeed->buffer) {
  2367. mutex_unlock(&dvbdmx->mutex);
  2368. return -ENOMEM;
  2369. }
  2370. #endif
  2371. dvbdmxfeed->state = DMX_STATE_READY;
  2372. mutex_unlock(&dvbdmx->mutex);
  2373. return 0;
  2374. }
  2375. static void prepare_secfilters(struct dvb_demux_feed *dvbdmxfeed)
  2376. {
  2377. int i;
  2378. struct dvb_demux_filter *f;
  2379. struct dmx_section_filter *sf;
  2380. u8 mask, mode, doneq;
  2381. if (!(f = dvbdmxfeed->filter))
  2382. return;
  2383. do {
  2384. sf = &f->filter;
  2385. doneq = 0;
  2386. for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
  2387. mode = sf->filter_mode[i];
  2388. mask = sf->filter_mask[i];
  2389. f->maskandmode[i] = mask & mode;
  2390. doneq |= f->maskandnotmode[i] = mask & ~mode;
  2391. }
  2392. f->doneq = doneq ? 1 : 0;
  2393. } while ((f = f->next));
  2394. }
  2395. static int dmx_section_feed_start_filtering(struct dmx_section_feed *feed)
  2396. {
  2397. struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
  2398. struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
  2399. int ret;
  2400. if (mutex_lock_interruptible(&dvbdmx->mutex))
  2401. return -ERESTARTSYS;
  2402. if (feed->is_filtering) {
  2403. mutex_unlock(&dvbdmx->mutex);
  2404. return -EBUSY;
  2405. }
  2406. if (!dvbdmxfeed->filter) {
  2407. mutex_unlock(&dvbdmx->mutex);
  2408. return -EINVAL;
  2409. }
  2410. dvbdmxfeed->feed.sec.tsfeedp = 0;
  2411. dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
  2412. dvbdmxfeed->feed.sec.secbufp = 0;
  2413. dvbdmxfeed->feed.sec.seclen = 0;
  2414. dvbdmxfeed->first_cc = 1;
  2415. dvbdmxfeed->scrambling_bits = 0;
  2416. if (!dvbdmx->start_feed) {
  2417. mutex_unlock(&dvbdmx->mutex);
  2418. return -ENODEV;
  2419. }
  2420. prepare_secfilters(dvbdmxfeed);
  2421. if ((ret = dvbdmx->start_feed(dvbdmxfeed)) < 0) {
  2422. mutex_unlock(&dvbdmx->mutex);
  2423. return ret;
  2424. }
  2425. spin_lock_irq(&dvbdmx->lock);
  2426. feed->is_filtering = 1;
  2427. dvbdmxfeed->state = DMX_STATE_GO;
  2428. spin_unlock_irq(&dvbdmx->lock);
  2429. mutex_unlock(&dvbdmx->mutex);
  2430. return 0;
  2431. }
  2432. static int dmx_section_feed_stop_filtering(struct dmx_section_feed *feed)
  2433. {
  2434. struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
  2435. struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
  2436. int ret;
  2437. mutex_lock(&dvbdmx->mutex);
  2438. if (dvbdmxfeed->state < DMX_STATE_GO) {
  2439. mutex_unlock(&dvbdmx->mutex);
  2440. return -EINVAL;
  2441. }
  2442. if (!dvbdmx->stop_feed) {
  2443. mutex_unlock(&dvbdmx->mutex);
  2444. return -ENODEV;
  2445. }
  2446. ret = dvbdmx->stop_feed(dvbdmxfeed);
  2447. spin_lock_irq(&dvbdmx->lock);
  2448. dvbdmxfeed->state = DMX_STATE_READY;
  2449. feed->is_filtering = 0;
  2450. spin_unlock_irq(&dvbdmx->lock);
  2451. mutex_unlock(&dvbdmx->mutex);
  2452. return ret;
  2453. }
  2454. static int dmx_section_feed_data_ready_cb(struct dmx_section_feed *feed,
  2455. dmx_section_data_ready_cb callback)
  2456. {
  2457. struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
  2458. struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
  2459. mutex_lock(&dvbdmx->mutex);
  2460. if (dvbdmxfeed->state == DMX_STATE_GO) {
  2461. mutex_unlock(&dvbdmx->mutex);
  2462. return -EINVAL;
  2463. }
  2464. dvbdmxfeed->data_ready_cb.sec = callback;
  2465. mutex_unlock(&dvbdmx->mutex);
  2466. return 0;
  2467. }
  2468. static int dmx_section_set_secure_mode(struct dmx_section_feed *feed,
  2469. struct dmx_secure_mode *secure_mode)
  2470. {
  2471. struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
  2472. struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
  2473. mutex_lock(&dvbdmx->mutex);
  2474. if (dvbdmxfeed->state == DMX_STATE_GO) {
  2475. mutex_unlock(&dvbdmx->mutex);
  2476. return -EBUSY;
  2477. }
  2478. dvbdmxfeed->secure_mode = *secure_mode;
  2479. mutex_unlock(&dvbdmx->mutex);
  2480. return 0;
  2481. }
  2482. static int dmx_section_set_cipher_ops(struct dmx_section_feed *feed,
  2483. struct dmx_cipher_operations *cipher_ops)
  2484. {
  2485. struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
  2486. struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
  2487. int ret = 0;
  2488. if (mutex_lock_interruptible(&dvbdmx->mutex))
  2489. return -ERESTARTSYS;
  2490. if ((dvbdmxfeed->state == DMX_STATE_GO) &&
  2491. dvbdmx->set_cipher_op) {
  2492. ret = dvbdmx->set_cipher_op(dvbdmxfeed, cipher_ops);
  2493. }
  2494. if (!ret)
  2495. dvbdmxfeed->cipher_ops = *cipher_ops;
  2496. mutex_unlock(&dvbdmx->mutex);
  2497. return ret;
  2498. }
  2499. static int dmx_section_feed_release_filter(struct dmx_section_feed *feed,
  2500. struct dmx_section_filter *filter)
  2501. {
  2502. struct dvb_demux_filter *dvbdmxfilter = (struct dvb_demux_filter *)filter, *f;
  2503. struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
  2504. struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
  2505. mutex_lock(&dvbdmx->mutex);
  2506. if (dvbdmxfilter->feed != dvbdmxfeed) {
  2507. mutex_unlock(&dvbdmx->mutex);
  2508. return -EINVAL;
  2509. }
  2510. if (feed->is_filtering) {
  2511. /* release dvbdmx->mutex as far as it is
  2512. acquired by stop_filtering() itself */
  2513. mutex_unlock(&dvbdmx->mutex);
  2514. feed->stop_filtering(feed);
  2515. mutex_lock(&dvbdmx->mutex);
  2516. }
  2517. spin_lock_irq(&dvbdmx->lock);
  2518. f = dvbdmxfeed->filter;
  2519. if (f == dvbdmxfilter) {
  2520. dvbdmxfeed->filter = dvbdmxfilter->next;
  2521. } else {
  2522. while (f->next != dvbdmxfilter)
  2523. f = f->next;
  2524. f->next = f->next->next;
  2525. }
  2526. filter->priv = NULL;
  2527. dvbdmxfilter->state = DMX_STATE_FREE;
  2528. spin_unlock_irq(&dvbdmx->lock);
  2529. mutex_unlock(&dvbdmx->mutex);
  2530. return 0;
  2531. }
  2532. static int dvbdmx_section_feed_oob_cmd(struct dmx_section_feed *section_feed,
  2533. struct dmx_oob_command *cmd)
  2534. {
  2535. struct dvb_demux_feed *feed = (struct dvb_demux_feed *)section_feed;
  2536. struct dvb_demux *dvbdmx = feed->demux;
  2537. struct dmx_data_ready data;
  2538. int ret = 0;
  2539. data.data_length = 0;
  2540. mutex_lock(&dvbdmx->mutex);
  2541. if (feed->state != DMX_STATE_GO) {
  2542. mutex_unlock(&dvbdmx->mutex);
  2543. return -EINVAL;
  2544. }
  2545. /* Secure section feeds are handled by the plug-in */
  2546. if (feed->secure_mode.is_secured) {
  2547. if (feed->demux->oob_command)
  2548. ret = feed->demux->oob_command(feed, cmd);
  2549. else
  2550. ret = 0;
  2551. mutex_unlock(&dvbdmx->mutex);
  2552. return ret;
  2553. }
  2554. switch (cmd->type) {
  2555. case DMX_OOB_CMD_EOS:
  2556. data.status = DMX_OK_EOS;
  2557. break;
  2558. case DMX_OOB_CMD_MARKER:
  2559. data.status = DMX_OK_MARKER;
  2560. data.marker.id = cmd->params.marker.id;
  2561. break;
  2562. default:
  2563. ret = -EINVAL;
  2564. break;
  2565. }
  2566. if (!ret)
  2567. ret = dvb_dmx_notify_section_event(feed, &data, 1);
  2568. mutex_unlock(&dvbdmx->mutex);
  2569. return ret;
  2570. }
  2571. static int dvbdmx_section_get_scrambling_bits(
  2572. struct dmx_section_feed *section_feed, u8 *value)
  2573. {
  2574. struct dvb_demux_feed *feed = (struct dvb_demux_feed *)section_feed;
  2575. struct dvb_demux *demux = feed->demux;
  2576. spin_lock(&demux->lock);
  2577. if (!section_feed->is_filtering) {
  2578. spin_unlock(&demux->lock);
  2579. return -EINVAL;
  2580. }
  2581. *value = feed->scrambling_bits;
  2582. spin_unlock(&demux->lock);
  2583. return 0;
  2584. }
  2585. static int dvbdmx_allocate_section_feed(struct dmx_demux *demux,
  2586. struct dmx_section_feed **feed,
  2587. dmx_section_cb callback)
  2588. {
  2589. struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
  2590. struct dvb_demux_feed *dvbdmxfeed;
  2591. if (mutex_lock_interruptible(&dvbdmx->mutex))
  2592. return -ERESTARTSYS;
  2593. if (!(dvbdmxfeed = dvb_dmx_feed_alloc(dvbdmx))) {
  2594. mutex_unlock(&dvbdmx->mutex);
  2595. return -EBUSY;
  2596. }
  2597. dvbdmxfeed->type = DMX_TYPE_SEC;
  2598. dvbdmxfeed->cb.sec = callback;
  2599. dvbdmxfeed->demux = dvbdmx;
  2600. dvbdmxfeed->pid = 0xffff;
  2601. dvbdmxfeed->secure_mode.is_secured = 0;
  2602. dvbdmxfeed->tsp_out_format = DMX_TSP_FORMAT_188;
  2603. dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
  2604. dvbdmxfeed->feed.sec.secbufp = dvbdmxfeed->feed.sec.seclen = 0;
  2605. dvbdmxfeed->feed.sec.tsfeedp = 0;
  2606. dvbdmxfeed->filter = NULL;
  2607. dvbdmxfeed->buffer = NULL;
  2608. dvbdmxfeed->idx_params.enable = 0;
  2609. (*feed) = &dvbdmxfeed->feed.sec;
  2610. (*feed)->is_filtering = 0;
  2611. (*feed)->parent = demux;
  2612. (*feed)->priv = NULL;
  2613. (*feed)->set = dmx_section_feed_set;
  2614. (*feed)->allocate_filter = dmx_section_feed_allocate_filter;
  2615. (*feed)->start_filtering = dmx_section_feed_start_filtering;
  2616. (*feed)->stop_filtering = dmx_section_feed_stop_filtering;
  2617. (*feed)->release_filter = dmx_section_feed_release_filter;
  2618. (*feed)->data_ready_cb = dmx_section_feed_data_ready_cb;
  2619. (*feed)->notify_data_read = NULL;
  2620. (*feed)->set_secure_mode = dmx_section_set_secure_mode;
  2621. (*feed)->set_cipher_ops = dmx_section_set_cipher_ops;
  2622. (*feed)->oob_command = dvbdmx_section_feed_oob_cmd;
  2623. (*feed)->get_scrambling_bits = dvbdmx_section_get_scrambling_bits;
  2624. (*feed)->flush_buffer = NULL;
  2625. mutex_unlock(&dvbdmx->mutex);
  2626. return 0;
  2627. }
  2628. static int dvbdmx_release_section_feed(struct dmx_demux *demux,
  2629. struct dmx_section_feed *feed)
  2630. {
  2631. struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
  2632. struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
  2633. mutex_lock(&dvbdmx->mutex);
  2634. if (dvbdmxfeed->state == DMX_STATE_FREE) {
  2635. mutex_unlock(&dvbdmx->mutex);
  2636. return -EINVAL;
  2637. }
  2638. #ifndef NOBUFS
  2639. vfree(dvbdmxfeed->buffer);
  2640. dvbdmxfeed->buffer = NULL;
  2641. #endif
  2642. dvbdmxfeed->state = DMX_STATE_FREE;
  2643. feed->priv = NULL;
  2644. dvb_demux_feed_del(dvbdmxfeed);
  2645. dvbdmxfeed->pid = 0xffff;
  2646. mutex_unlock(&dvbdmx->mutex);
  2647. return 0;
  2648. }
  2649. /******************************************************************************
  2650. * dvb_demux kernel data API calls
  2651. ******************************************************************************/
  2652. static int dvbdmx_open(struct dmx_demux *demux)
  2653. {
  2654. struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
  2655. if (dvbdemux->users >= MAX_DVB_DEMUX_USERS)
  2656. return -EUSERS;
  2657. dvbdemux->users++;
  2658. return 0;
  2659. }
  2660. static int dvbdmx_close(struct dmx_demux *demux)
  2661. {
  2662. struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
  2663. if (dvbdemux->users == 0)
  2664. return -ENODEV;
  2665. dvbdemux->users--;
  2666. //FIXME: release any unneeded resources if users==0
  2667. return 0;
  2668. }
  2669. static int dvbdmx_write(struct dmx_demux *demux, const char *buf, size_t count)
  2670. {
  2671. struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
  2672. if (!demux->frontend || !buf || demux->dvr_input_protected ||
  2673. (demux->frontend->source != DMX_MEMORY_FE))
  2674. return -EINVAL;
  2675. if (mutex_lock_interruptible(&dvbdemux->mutex))
  2676. return -ERESTARTSYS;
  2677. dvb_dmx_swfilter_format(dvbdemux, buf, count, dvbdemux->tsp_format);
  2678. mutex_unlock(&dvbdemux->mutex);
  2679. if (signal_pending(current))
  2680. return -EINTR;
  2681. return count;
  2682. }
  2683. static int dvbdmx_write_cancel(struct dmx_demux *demux)
  2684. {
  2685. struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
  2686. spin_lock_irq(&dvbdmx->lock);
  2687. /* cancel any pending wait for decoder's buffers */
  2688. dvbdmx->sw_filter_abort = 1;
  2689. dvbdmx->tsbufp = 0;
  2690. dvb_dmx_configure_decoder_fullness(dvbdmx, 0);
  2691. spin_unlock_irq(&dvbdmx->lock);
  2692. return 0;
  2693. }
  2694. static int dvbdmx_set_playback_mode(struct dmx_demux *demux,
  2695. enum dmx_playback_mode_t mode,
  2696. dmx_ts_fullness ts_fullness_callback,
  2697. dmx_section_fullness sec_fullness_callback)
  2698. {
  2699. struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
  2700. mutex_lock(&dvbdmx->mutex);
  2701. dvbdmx->playback_mode = mode;
  2702. dvbdmx->buffer_ctrl.ts = ts_fullness_callback;
  2703. dvbdmx->buffer_ctrl.sec = sec_fullness_callback;
  2704. mutex_unlock(&dvbdmx->mutex);
  2705. return 0;
  2706. }
  2707. static int dvbdmx_add_frontend(struct dmx_demux *demux,
  2708. struct dmx_frontend *frontend)
  2709. {
  2710. struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
  2711. struct list_head *head = &dvbdemux->frontend_list;
  2712. list_add(&(frontend->connectivity_list), head);
  2713. return 0;
  2714. }
  2715. static int dvbdmx_remove_frontend(struct dmx_demux *demux,
  2716. struct dmx_frontend *frontend)
  2717. {
  2718. struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
  2719. struct list_head *pos, *n, *head = &dvbdemux->frontend_list;
  2720. list_for_each_safe(pos, n, head) {
  2721. if (DMX_FE_ENTRY(pos) == frontend) {
  2722. list_del(pos);
  2723. return 0;
  2724. }
  2725. }
  2726. return -ENODEV;
  2727. }
  2728. static struct list_head *dvbdmx_get_frontends(struct dmx_demux *demux)
  2729. {
  2730. struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
  2731. if (list_empty(&dvbdemux->frontend_list))
  2732. return NULL;
  2733. return &dvbdemux->frontend_list;
  2734. }
  2735. static int dvbdmx_connect_frontend(struct dmx_demux *demux,
  2736. struct dmx_frontend *frontend)
  2737. {
  2738. struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
  2739. if (demux->frontend)
  2740. return -EINVAL;
  2741. mutex_lock(&dvbdemux->mutex);
  2742. demux->frontend = frontend;
  2743. mutex_unlock(&dvbdemux->mutex);
  2744. return 0;
  2745. }
  2746. static int dvbdmx_disconnect_frontend(struct dmx_demux *demux)
  2747. {
  2748. struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
  2749. mutex_lock(&dvbdemux->mutex);
  2750. dvbdemux->sw_filter_abort = 0;
  2751. demux->frontend = NULL;
  2752. mutex_unlock(&dvbdemux->mutex);
  2753. return 0;
  2754. }
  2755. static int dvbdmx_get_pes_pids(struct dmx_demux *demux, u16 * pids)
  2756. {
  2757. struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
  2758. /* 4 Demux Instances each with group of 5 pids */
  2759. memcpy(pids, dvbdemux->pids, DMX_PES_OTHER*sizeof(u16));
  2760. return 0;
  2761. }
  2762. static int dvbdmx_get_tsp_size(struct dmx_demux *demux)
  2763. {
  2764. int tsp_size;
  2765. struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
  2766. mutex_lock(&dvbdemux->mutex);
  2767. tsp_size = dvbdemux->ts_packet_size;
  2768. mutex_unlock(&dvbdemux->mutex);
  2769. return tsp_size;
  2770. }
  2771. static int dvbdmx_set_tsp_format(
  2772. struct dmx_demux *demux,
  2773. enum dmx_tsp_format_t tsp_format)
  2774. {
  2775. struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
  2776. if ((tsp_format > DMX_TSP_FORMAT_204) ||
  2777. (tsp_format < DMX_TSP_FORMAT_188))
  2778. return -EINVAL;
  2779. mutex_lock(&dvbdemux->mutex);
  2780. dvbdemux->tsp_format = tsp_format;
  2781. switch (tsp_format) {
  2782. case DMX_TSP_FORMAT_188:
  2783. dvbdemux->ts_packet_size = 188;
  2784. break;
  2785. case DMX_TSP_FORMAT_192_TAIL:
  2786. case DMX_TSP_FORMAT_192_HEAD:
  2787. dvbdemux->ts_packet_size = 192;
  2788. break;
  2789. case DMX_TSP_FORMAT_204:
  2790. dvbdemux->ts_packet_size = 204;
  2791. break;
  2792. }
  2793. mutex_unlock(&dvbdemux->mutex);
  2794. return 0;
  2795. }
  2796. int dvb_dmx_init(struct dvb_demux *dvbdemux)
  2797. {
  2798. int i;
  2799. struct dmx_demux *dmx = &dvbdemux->dmx;
  2800. dvbdemux->cnt_storage = NULL;
  2801. dvbdemux->users = 0;
  2802. dvbdemux->filter = vmalloc(dvbdemux->filternum * sizeof(struct dvb_demux_filter));
  2803. if (!dvbdemux->filter)
  2804. return -ENOMEM;
  2805. dvbdemux->feed = vmalloc(dvbdemux->feednum * sizeof(struct dvb_demux_feed));
  2806. if (!dvbdemux->feed) {
  2807. vfree(dvbdemux->filter);
  2808. dvbdemux->filter = NULL;
  2809. return -ENOMEM;
  2810. }
  2811. dvbdemux->rec_info_pool = vmalloc(dvbdemux->feednum *
  2812. sizeof(struct dvb_demux_rec_info));
  2813. if (!dvbdemux->rec_info_pool) {
  2814. vfree(dvbdemux->feed);
  2815. vfree(dvbdemux->filter);
  2816. dvbdemux->feed = NULL;
  2817. dvbdemux->filter = NULL;
  2818. return -ENOMEM;
  2819. }
  2820. dvbdemux->sw_filter_abort = 0;
  2821. dvbdemux->total_process_time = 0;
  2822. dvbdemux->total_crc_time = 0;
  2823. snprintf(dvbdemux->alias,
  2824. MAX_DVB_DEMUX_NAME_LEN,
  2825. "demux%d",
  2826. dvb_demux_index++);
  2827. dvbdemux->dmx.debugfs_demux_dir =
  2828. debugfs_create_dir(dvbdemux->alias, NULL);
  2829. if (dvbdemux->dmx.debugfs_demux_dir != NULL) {
  2830. debugfs_create_u32(
  2831. "total_processing_time", 0664,
  2832. dvbdemux->dmx.debugfs_demux_dir,
  2833. &dvbdemux->total_process_time);
  2834. debugfs_create_u32(
  2835. "total_crc_time", 0664,
  2836. dvbdemux->dmx.debugfs_demux_dir,
  2837. &dvbdemux->total_crc_time);
  2838. }
  2839. for (i = 0; i < dvbdemux->filternum; i++) {
  2840. dvbdemux->filter[i].state = DMX_STATE_FREE;
  2841. dvbdemux->filter[i].index = i;
  2842. }
  2843. for (i = 0; i < dvbdemux->feednum; i++) {
  2844. dvbdemux->feed[i].state = DMX_STATE_FREE;
  2845. dvbdemux->feed[i].index = i;
  2846. dvbdemux->rec_info_pool[i].ref_count = 0;
  2847. }
  2848. dvbdemux->cnt_storage = vmalloc(MAX_PID + 1);
  2849. if (!dvbdemux->cnt_storage)
  2850. printk(KERN_WARNING "Couldn't allocate memory for TS/TEI check. Disabling it\n");
  2851. INIT_LIST_HEAD(&dvbdemux->frontend_list);
  2852. for (i = 0; i < DMX_PES_OTHER; i++) {
  2853. dvbdemux->pesfilter[i] = NULL;
  2854. dvbdemux->pids[i] = 0xffff;
  2855. }
  2856. INIT_LIST_HEAD(&dvbdemux->feed_list);
  2857. dvbdemux->playing = 0;
  2858. dvbdemux->recording = 0;
  2859. dvbdemux->tsbufp = 0;
  2860. dvbdemux->tsp_format = DMX_TSP_FORMAT_188;
  2861. dvbdemux->ts_packet_size = 188;
  2862. if (!dvbdemux->check_crc32)
  2863. dvbdemux->check_crc32 = dvb_dmx_crc32;
  2864. if (!dvbdemux->memcopy)
  2865. dvbdemux->memcopy = dvb_dmx_memcopy;
  2866. dmx->frontend = NULL;
  2867. dmx->priv = dvbdemux;
  2868. dmx->open = dvbdmx_open;
  2869. dmx->close = dvbdmx_close;
  2870. dmx->write = dvbdmx_write;
  2871. dmx->write_cancel = dvbdmx_write_cancel;
  2872. dmx->set_playback_mode = dvbdmx_set_playback_mode;
  2873. dmx->allocate_ts_feed = dvbdmx_allocate_ts_feed;
  2874. dmx->release_ts_feed = dvbdmx_release_ts_feed;
  2875. dmx->allocate_section_feed = dvbdmx_allocate_section_feed;
  2876. dmx->release_section_feed = dvbdmx_release_section_feed;
  2877. dmx->map_buffer = NULL;
  2878. dmx->unmap_buffer = NULL;
  2879. dmx->add_frontend = dvbdmx_add_frontend;
  2880. dmx->remove_frontend = dvbdmx_remove_frontend;
  2881. dmx->get_frontends = dvbdmx_get_frontends;
  2882. dmx->connect_frontend = dvbdmx_connect_frontend;
  2883. dmx->disconnect_frontend = dvbdmx_disconnect_frontend;
  2884. dmx->get_pes_pids = dvbdmx_get_pes_pids;
  2885. dmx->set_tsp_format = dvbdmx_set_tsp_format;
  2886. dmx->get_tsp_size = dvbdmx_get_tsp_size;
  2887. mutex_init(&dvbdemux->mutex);
  2888. spin_lock_init(&dvbdemux->lock);
  2889. return 0;
  2890. }
  2891. EXPORT_SYMBOL(dvb_dmx_init);
  2892. void dvb_dmx_release(struct dvb_demux *dvbdemux)
  2893. {
  2894. if (dvbdemux->dmx.debugfs_demux_dir != NULL)
  2895. debugfs_remove_recursive(dvbdemux->dmx.debugfs_demux_dir);
  2896. dvb_demux_index--;
  2897. vfree(dvbdemux->cnt_storage);
  2898. vfree(dvbdemux->filter);
  2899. vfree(dvbdemux->feed);
  2900. vfree(dvbdemux->rec_info_pool);
  2901. }
  2902. EXPORT_SYMBOL(dvb_dmx_release);