Advertisement
Guest User

Untitled

a guest
Nov 13th, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.58 KB | None | 0 0
  1. diff --git a/cmdutils.c b/cmdutils.c
  2. index 3d428f3eea..b7d55997bf 100644
  3. --- a/cmdutils.c
  4. +++ b/cmdutils.c
  5. @@ -1112,9 +1112,14 @@ static void print_program_info(int flags, int level)
  6. const char *indent = flags & INDENT? " " : "";
  7.  
  8. av_log(NULL, level, "%s version " FFMPEG_VERSION, program_name);
  9. - if (flags & SHOW_COPYRIGHT)
  10. + if (flags & SHOW_COPYRIGHT) {
  11. av_log(NULL, level, " Copyright (c) %d-%d the FFmpeg developers",
  12. program_birth_year, CONFIG_THIS_YEAR);
  13. +#if CONFIG_SAMYGO_SRF
  14. + av_log(NULL, level, "\n srf decryption v0.1.3 (c) 2016-%d bugficks@samygo",
  15. + CONFIG_THIS_YEAR);
  16. +#endif
  17. + }
  18. av_log(NULL, level, "\n");
  19. av_log(NULL, level, "%sbuilt with %s\n", indent, CC_IDENT);
  20.  
  21. diff --git a/configure b/configure
  22. index 9b5789ab83..e8118004bc 100755
  23. --- a/configure
  24. +++ b/configure
  25. @@ -281,11 +281,14 @@ External library support:
  26. --enable-decklink enable Blackmagic DeckLink I/O support [no]
  27. --enable-mediacodec enable Android MediaCodec support [no]
  28. --enable-netcdf enable NetCDF, needed for sofalizer filter [no]
  29. + --disable-nettle disable Nettle low-level cryptographic library [autodetect]
  30. --enable-openal enable OpenAL 1.1 capture support [no]
  31. --enable-opencl enable OpenCL code
  32. --enable-opengl enable OpenGL rendering [no]
  33. --enable-openssl enable openssl, needed for https support
  34. if gnutls is not used [no]
  35. + --disable-samygo-srf disable SamyGO PVR decryption
  36. + --disable-samygo-tsaudio disable SamyGO invalid TS audio stream workaround
  37. --disable-schannel disable SChannel SSP, needed for TLS support on
  38. Windows if openssl and gnutls are not used [autodetect]
  39. --disable-sdl2 disable sdl2 [autodetect]
  40. @@ -1586,6 +1589,7 @@ EXTERNAL_LIBRARY_LIST="
  41. libzvbi
  42. mediacodec
  43. netcdf
  44. + nettle
  45. openal
  46. opencl
  47. opengl
  48. @@ -1631,6 +1635,8 @@ FEATURE_LIST="
  49. hardcoded_tables
  50. omx_rpi
  51. runtime_cpudetect
  52. + samygo_srf
  53. + samygo_tsaudio
  54. safe_bitstream_reader
  55. shared
  56. small
  57. @@ -2940,15 +2946,17 @@ matroska_demuxer_suggest="bzlib lzo zlib"
  58. matroska_muxer_select="iso_media riffenc"
  59. mmf_muxer_select="riffenc"
  60. mov_demuxer_select="iso_media riffdec"
  61. -mov_demuxer_suggest="zlib"
  62. +mov_demuxer_suggest="zlib nettle"
  63. mov_muxer_select="iso_media riffenc rtpenc_chain"
  64. mp3_demuxer_select="mpegaudio_parser"
  65. mp4_muxer_select="mov_muxer"
  66. mpegts_demuxer_select="iso_media"
  67. +mpegts_demuxer_suggest="nettle"
  68. mpegts_muxer_select="adts_muxer latm_muxer"
  69. mpegtsraw_demuxer_select="mpegts_demuxer"
  70. mxf_d10_muxer_select="mxf_muxer"
  71. mxf_opatom_muxer_select="mxf_muxer"
  72. +mxf_muxer_suggest="nettle"
  73. nut_muxer_select="riffenc"
  74. nuv_demuxer_select="riffdec"
  75. oga_muxer_select="ogg_muxer"
  76. @@ -3284,6 +3292,8 @@ enable doc
  77. enable faan faandct faanidct
  78. enable optimizations
  79. enable runtime_cpudetect
  80. +enable samygo_srf
  81. +enable samygo_tsaudio
  82. enable safe_bitstream_reader
  83. enable static
  84. enable swscale_alpha
  85. @@ -5734,6 +5744,11 @@ enabled pthreads &&
  86. disabled zlib || check_lib zlib.h zlibVersion -lz || disable zlib
  87. disabled bzlib || check_lib bzlib.h BZ2_bzlibVersion -lbz2 || disable bzlib
  88. disabled lzma || check_lib lzma.h lzma_version_number -llzma || disable lzma
  89. +disabled nettle \
  90. + || require_pkg_config nettle nettle/aes.h nettle_aes_encrypt \
  91. + || check_lib2 nettle/aes.h nettle_aes_encrypt -lnettle \
  92. + || disable nettle
  93. +
  94.  
  95. check_lib math.h sin -lm && LIBM="-lm"
  96. disabled crystalhd || check_lib "stdint.h libcrystalhd/libcrystalhd_if.h" DtsCrystalHDVersion -lcrystalhd || disable crystalhd
  97. diff --git a/ffmpeg_opt.c b/ffmpeg_opt.c
  98. index d1fe8742ff..8352d766ef 100644
  99. --- a/ffmpeg_opt.c
  100. +++ b/ffmpeg_opt.c
  101. @@ -644,6 +644,36 @@ static AVCodec *choose_decoder(OptionsContext *o, AVFormatContext *s, AVStream *
  102. return avcodec_find_decoder(st->codecpar->codec_id);
  103. }
  104.  
  105. +#if CONFIG_SAMYGO_TSAUDIO
  106. +static void sgo_fix_decoder(OptionsContext *o, AVFormatContext *s, AVStream *st)
  107. +{
  108. + // bugficks@samygo
  109. + // Some PVRs have audio streams w/ 0 channels which will make ffmpeg abort.
  110. + // The workaround is to set those audio streams type to unknown and run
  111. + // ffmpeg w/ '-ignore_unknown' option.
  112. + if(st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && !st->codecpar->channel_layout) {
  113. + int i,j;
  114. + const AVCodecDescriptor *desc = avcodec_descriptor_get(st->codecpar->codec_id);
  115. +
  116. + for(i = 0; i < s->nb_programs; i++)
  117. + for(j = 0; j < s->programs[i]->nb_stream_indexes; j++)
  118. + if(s->programs[i]->stream_index[j] == st->index)
  119. + break;
  120. +
  121. + av_log(s, AV_LOG_ERROR, "[SamyGO] Invalid audio stream detected: stream #%d:%d (codec=%s, channels=0)\n", j, st->index, desc->name);
  122. + av_log(s, AV_LOG_WARNING, "[SamyGO] Setting audio stream codec_type=AVMEDIA_TYPE_UNKNOWN\n");
  123. +
  124. + st->codecpar->codec_type = AVMEDIA_TYPE_UNKNOWN;
  125. + //st->codecpar->codec_id = AV_CODEC_ID_NONE;
  126. +
  127. + if(!ignore_unknown_streams) {
  128. + ignore_unknown_streams = 1;
  129. + av_log(s, AV_LOG_WARNING, "[SamyGO] Setting option: -ignore_unknown\n");
  130. + }
  131. + }
  132. +}
  133. +#endif //#if CONFIG_SAMYGO_TSAUDIO
  134. +
  135. /* Add all the streams from the given input file to the global
  136. * list of input streams. */
  137. static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
  138. @@ -691,6 +721,11 @@ static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
  139. }
  140.  
  141. ist->dec = choose_decoder(o, ic, st);
  142. +
  143. +#if CONFIG_SAMYGO_TSAUDIO
  144. + sgo_fix_decoder(o, ic, st);
  145. +#endif
  146. +
  147. ist->decoder_opts = filter_codec_opts(o->g->codec_opts, ist->st->codecpar->codec_id, ic, st, ist->dec);
  148.  
  149. ist->reinit_filters = -1;
  150. diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
  151. index 3eff1522bd..2af40229b1 100644
  152. --- a/libavformat/mpegts.c
  153. +++ b/libavformat/mpegts.c
  154. @@ -28,6 +28,7 @@
  155. #include "libavutil/mathematics.h"
  156. #include "libavutil/opt.h"
  157. #include "libavutil/avassert.h"
  158. +#include "libavutil/aes.h"
  159. #include "libavcodec/bytestream.h"
  160. #include "libavcodec/get_bits.h"
  161. #include "libavcodec/opus.h"
  162. @@ -159,6 +160,11 @@ struct MpegTSContext {
  163. /** filters for various streams specified by PMT + for the PAT and PMT */
  164. MpegTSFilter *pids[NB_PID_MAX];
  165. int current_pid;
  166. +
  167. +#if CONFIG_SAMYGO_SRF
  168. + struct AVAES *aes_decrypt;
  169. + uint8_t srf_key[16];
  170. +#endif
  171. };
  172.  
  173. #define MPEGTS_OPTIONS \
  174. @@ -245,6 +251,119 @@ typedef struct PESContext {
  175.  
  176. extern AVInputFormat ff_mpegts_demuxer;
  177.  
  178. +//////////////////////////////////////////////////////////////////////////
  179. +
  180. +#if CONFIG_SAMYGO_SRF
  181. +
  182. +static int sgo_srf_decrypt_init(
  183. + AVFormatContext *s, MpegTSContext *ts)
  184. +{
  185. + int ret = 0;
  186. + char pathKey[sizeof(s->filename)] = { 0, };
  187. + AVIOContext *ioKey = 0;
  188. +
  189. + ts->aes_decrypt = 0;
  190. +
  191. + // if there is a key from -cryptokey option use that one otherwise try filename.[srf=key]
  192. + if(s->key)
  193. + {
  194. + if(s->keylen != sizeof(ts->srf_key))
  195. + return AVERROR_INVALIDDATA;
  196. +
  197. + memcpy(ts->srf_key, s->key, sizeof(ts->srf_key));
  198. + }
  199. + else
  200. + {
  201. + char *p = 0;
  202. + av_strlcpy(pathKey, s->filename, sizeof(pathKey));
  203. + p = av_strnstr(pathKey, ".srf", sizeof(pathKey));
  204. + if(!p)
  205. + return 0;
  206. +
  207. + p[1] = 'k';
  208. + p[2] = 'e';
  209. + p[3] = 'y';
  210. +
  211. + ret = avio_open(&ioKey, pathKey, AVIO_FLAG_READ);
  212. + if(ret)
  213. + return ret;
  214. +
  215. + ret = avio_read(ioKey, ts->srf_key, sizeof(ts->srf_key));
  216. + avio_closep(&ioKey);
  217. +
  218. + if(ret != sizeof(ts->srf_key))
  219. + return AVERROR_INVALIDDATA;
  220. + }
  221. +
  222. + av_log(ts->stream, AV_LOG_TRACE, "srf_key: ");
  223. + av_hex_dump_log(ts->stream, AV_LOG_TRACE, ts->srf_key, sizeof(ts->srf_key));
  224. +
  225. + ts->aes_decrypt = av_aes_alloc();
  226. + if(!ts->aes_decrypt)
  227. + ret = AVERROR(ENOMEM);
  228. + else
  229. + ret = av_aes_init(ts->aes_decrypt, ts->srf_key, sizeof(ts->srf_key) * 8, 1);
  230. +
  231. + return ret;
  232. +}
  233. +
  234. +static void sgo_srf_decrypt_deinit(
  235. + MpegTSContext *ts)
  236. +{
  237. + if(ts->aes_decrypt)
  238. + av_freep(&ts->aes_decrypt);
  239. +}
  240. +
  241. +static int sgo_srf_decrypt_packet(
  242. + MpegTSContext *ts, const uint8_t *packet)
  243. +{
  244. + const int _AES_BLOCK_SIZE = 16;
  245. +
  246. + uint8_t has_af = (packet[3] >> 5) & 1;
  247. + uint8_t scr_ctrl = (packet[3] >> 6) & 3;
  248. +
  249. + if(!ts->aes_decrypt)
  250. + {
  251. + if(scr_ctrl)
  252. + {
  253. + av_log(ts->stream, AV_LOG_ERROR, "sgo_srf_decrypt_packet: missing or invalid key for '%s'.\n", ts->stream->filename);
  254. + return AVERROR_INVALIDDATA;
  255. + }
  256. + }
  257. + else if(scr_ctrl)
  258. + {
  259. + uint8_t *data = (uint8_t *)packet;
  260. + int offset = 4;
  261. + int count = 0;
  262. +
  263. + if(scr_ctrl == 1)
  264. + {
  265. + // Samsung encrypted adaption field
  266. + has_af = 0;
  267. + }
  268. +
  269. + if(has_af)
  270. + {
  271. + // skip adaption field
  272. + offset += packet[4] + 1;
  273. + }
  274. +
  275. + // remove scrambling bits
  276. + data[3] &= (~(3 << 6)) & 0xFF;
  277. +
  278. + data += offset;
  279. + count = (TS_PACKET_SIZE - offset) / _AES_BLOCK_SIZE;
  280. +
  281. + av_aes_crypt(ts->aes_decrypt, data, data, count, 0, 1);
  282. + }
  283. +
  284. + return 0;
  285. +}
  286. +
  287. +#endif // #if CONFIG_SAMYGO_SRF
  288. +
  289. +//////////////////////////////////////////////////////////////////////////
  290. +
  291. static struct Program * get_program(MpegTSContext *ts, unsigned int programid)
  292. {
  293. int i;
  294. @@ -2296,6 +2415,11 @@ static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
  295. }
  296. }
  297.  
  298. +#if CONFIG_SAMYGO_SRF
  299. + if(sgo_srf_decrypt_packet(ts, packet))
  300. + return AVERROR_INVALIDDATA;
  301. +#endif
  302. +
  303. p = packet + 4;
  304. if (has_adaptation) {
  305. int64_t pcr_h;
  306. @@ -2630,6 +2754,10 @@ static int mpegts_read_header(AVFormatContext *s)
  307. ts->stream = s;
  308. ts->auto_guess = 0;
  309.  
  310. +#if CONFIG_SAMYGO_SRF
  311. + sgo_srf_decrypt_init(s, ts);
  312. +#endif
  313. +
  314. if (s->iformat == &ff_mpegts_demuxer) {
  315. /* normal demux */
  316.  
  317. @@ -2797,6 +2925,10 @@ static void mpegts_free(MpegTSContext *ts)
  318. {
  319. int i;
  320.  
  321. +#if CONFIG_SAMYGO_SRF
  322. + sgo_srf_decrypt_deinit(ts);
  323. +#endif
  324. +
  325. clear_programs(ts);
  326.  
  327. for (i = 0; i < NB_PID_MAX; i++)
  328. @@ -2896,6 +3028,10 @@ MpegTSContext *avpriv_mpegts_parse_open(AVFormatContext *s)
  329. mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);
  330. mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
  331.  
  332. +#if CONFIG_SAMYGO_SRF
  333. + sgo_srf_decrypt_init(s, ts);
  334. +#endif
  335. +
  336. return ts;
  337. }
  338.  
  339. diff --git a/libavutil/aes.c b/libavutil/aes.c
  340. index 397ea77389..d051265bd2 100644
  341. --- a/libavutil/aes.c
  342. +++ b/libavutil/aes.c
  343. @@ -33,6 +33,54 @@ struct AVAES *av_aes_alloc(void)
  344. return av_mallocz(sizeof(struct AVAES));
  345. }
  346.  
  347. +#if CONFIG_NETTLE
  348. +
  349. +int av_aes_init(
  350. + struct AVAES *a, const uint8_t *key, int key_bits, int decrypt)
  351. +{
  352. + int kl = key_bits >> 3;
  353. + switch(kl)
  354. + {
  355. + case 16:
  356. + case 24:
  357. + case 32:
  358. + break;
  359. + default:
  360. + return AVERROR(EINVAL);
  361. + }
  362. + if(decrypt)
  363. + aes_set_decrypt_key(&a->aes_ctx.ctx, kl, key);
  364. + else
  365. + aes_set_encrypt_key(&a->aes_ctx.ctx, kl, key);
  366. +
  367. + return 0;
  368. +}
  369. +
  370. +void av_aes_crypt(
  371. + AVAES *a, uint8_t *dst, const uint8_t *src, int count,
  372. + uint8_t *iv, int decrypt)
  373. +{
  374. + size_t len = count * AES_BLOCK_SIZE;
  375. +
  376. + if(iv)
  377. + {
  378. + CBC_SET_IV(&a->aes_ctx, iv);
  379. + if(decrypt)
  380. + CBC_DECRYPT(&a->aes_ctx, aes_decrypt, len, dst, src);
  381. + else
  382. + CBC_ENCRYPT(&a->aes_ctx, aes_encrypt, len, dst, src);
  383. + }
  384. + else
  385. + {
  386. + if(decrypt)
  387. + aes_decrypt(&a->aes_ctx.ctx, len, dst, src);
  388. + else
  389. + aes_encrypt(&a->aes_ctx.ctx, len, dst, src);
  390. + }
  391. +}
  392. +
  393. +#else /* !CONFIG_NETTLE */
  394. +
  395. static const uint8_t rcon[10] = {
  396. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36
  397. };
  398. @@ -266,3 +314,4 @@ int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
  399. return 0;
  400. }
  401.  
  402. +#endif /* #if CONFIG_NETTLE */
  403. diff --git a/libavutil/aes_internal.h b/libavutil/aes_internal.h
  404. index 494425878d..9eb1ddb3e6 100644
  405. --- a/libavutil/aes_internal.h
  406. +++ b/libavutil/aes_internal.h
  407. @@ -24,6 +24,20 @@
  408. #include "mem.h"
  409. #include <stdint.h>
  410.  
  411. +#if CONFIG_NETTLE
  412. +
  413. +#include <nettle/aes.h>
  414. +#include <nettle/cbc.h>
  415. +
  416. +typedef struct AVAES {
  417. + DECLARE_ALIGNED(16, struct CBC_CTX(struct aes_ctx, AES_BLOCK_SIZE), aes_ctx);
  418. +
  419. + void (*crypt)(struct AVAES *ctx, uint8_t *iv_or_ctr, size_t length, uint8_t *dst, const uint8_t *src);
  420. +
  421. +} AVAES;
  422. +
  423. +#else /* !CONFIG_NETTLE */
  424. +
  425. typedef union {
  426. uint64_t u64[2];
  427. uint32_t u32[4];
  428. @@ -40,4 +54,6 @@ typedef struct AVAES {
  429. void (*crypt)(struct AVAES *a, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int rounds);
  430. } AVAES;
  431.  
  432. +#endif /* #if CONFIG_NETTLE */
  433. +
  434. #endif /* AVUTIL_AES_INTERNAL_H */
  435. diff --git a/version.sh b/version.sh
  436. index edc4dd33c5..94d822f98b 100755
  437. --- a/version.sh
  438. +++ b/version.sh
  439. @@ -45,7 +45,7 @@ if [ -z "$2" ]; then
  440. exit
  441. fi
  442.  
  443. -NEW_REVISION="#define FFMPEG_VERSION \"$version\""
  444. +NEW_REVISION="#define FFMPEG_VERSION \"$version [SamyGO]\""
  445. OLD_REVISION=$(cat "$2" 2> /dev/null | head -4 | tail -1)
  446.  
  447. # String used for preprocessor guard
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement