Recherche avancée

Médias (0)

Mot : - Tags -/xmlrpc

Aucun média correspondant à vos critères n’est disponible sur le site.

Autres articles (30)

  • MediaSPIP Init et Diogène : types de publications de MediaSPIP

    11 novembre 2010, par

    À l’installation d’un site MediaSPIP, le plugin MediaSPIP Init réalise certaines opérations dont la principale consiste à créer quatre rubriques principales dans le site et de créer cinq templates de formulaire pour Diogène.
    Ces quatre rubriques principales (aussi appelées secteurs) sont : Medias ; Sites ; Editos ; Actualités ;
    Pour chacune de ces rubriques est créé un template de formulaire spécifique éponyme. Pour la rubrique "Medias" un second template "catégorie" est créé permettant d’ajouter (...)

  • Use, discuss, criticize

    13 avril 2011, par

    Talk to people directly involved in MediaSPIP’s development, or to people around you who could use MediaSPIP to share, enhance or develop their creative projects.
    The bigger the community, the more MediaSPIP’s potential will be explored and the faster the software will evolve.
    A discussion list is available for all exchanges between users.

  • Encoding and processing into web-friendly formats

    13 avril 2011, par

    MediaSPIP automatically converts uploaded files to internet-compatible formats.
    Video files are encoded in MP4, Ogv and WebM (supported by HTML5) and MP4 (supported by Flash).
    Audio files are encoded in MP3 and Ogg (supported by HTML5) and MP3 (supported by Flash).
    Where possible, text is analyzed in order to retrieve the data needed for search engine detection, and then exported as a series of image files.
    All uploaded files are stored online in their original format, so you can (...)

Sur d’autres sites (5962)

  • How to extract elementary video from mp4 using ffmpeg programmatically ?

    24 octobre 2019, par epipav

    I have started learning ffmpeg few weaks ago. At the moment I am able to transcode any video to mp4 using h264/AVC codec. The main scheme is something like that :

    -open input
    -demux
    -decode
    -encode
    -mux

    The actual code is below :

    #include <iostream>

    #include

    extern "C" {

     #
     ifndef __STDC_CONSTANT_MACROS# undef main /* Prevents SDL from overriding main() */ # define __STDC_CONSTANT_MACROS# endif

     # pragma comment(lib, "avcodec.lib")# pragma comment(lib, "avformat.lib")# pragma comment(lib, "swscale.lib")# pragma comment(lib, "avutil.lib")

     #include
     #include
     #include
     #include
     #include <libavutil></libavutil>opt.h>
     #include
     #include
     #include
     #include
     #include
    }

    using namespace std;

    void open_video(AVFormatContext * oc, AVCodec * codec, AVStream * st) {
     int ret;
     AVCodecContext * c;
     c = st - > codec;

     /*open codec */

     cout &lt;&lt; "probably starts here" &lt;&lt; endl;
     ret = avcodec_open2(c, codec, NULL);
     cout &lt;&lt; "and ends here" &lt;&lt; endl;

     if (ret &lt; 0) {
       cout &lt;&lt; ("Could not open video codec") &lt;&lt; endl;
     }

    }

    /*This function will add a new stream to our file.
    @param
    oc -> Format context that the new stream will be added.
    codec -> codec of the stream, this will be passed.
    codec_id ->
    chWidth->
    chHeight->
    */

    AVStream * addStream(AVFormatContext * oc, AVCodec ** codec, enum AVCodecID codec_id, int chWidth, int chHeight, int fps) {
     AVCodecContext * c;
     AVStream * st;

     //find encoder of the stream, it passes this information to @codec, later on
     //it will be used in encoding the video @ avcodec_encode_video2 in loop.
     * codec = avcodec_find_encoder(AV_CODEC_ID_H264);

     if (( * codec) == NULL)
       cout &lt;&lt; "ERROR CAN NOT FIND ENCODER! ERROR! ERROR! AVCODEC_FIND_ENCODER FAILED !!!1 "
     "" &lt;&lt; endl;

     if (!( * codec))
       printf("Could not find encoder for ' %s ' ", avcodec_get_name(codec_id));

     //create a new stream with the found codec inside oc(AVFormatContext).
     st = avformat_new_stream(oc, * codec);

     if (!st)
       cout &lt;&lt; " Cannot allocate stream " &lt;&lt; endl;

     //Setting the stream id.
     //Since, there can be other streams in this AVFormatContext,
     //we should find the first non used index. And this is oc->nb_streams(number of streams) - 1
     st - > id = oc - > nb_streams - 1;

     c = st - > codec;

     //setting the stream's codec's properties.
     c - > codec_id = codec_id;
     c - > bit_rate = 4000000;
     c - > width = chWidth;
     c - > height = chHeight;
     c - > time_base.den = fps;
     //fps;
     c - > time_base.num = 1;
     c - > gop_size = 12;
     c - > pix_fmt = AV_PIX_FMT_YUV420P;

     if (c - > codec_id == AV_CODEC_ID_MPEG2VIDEO) {
       /* just for testing, we also add B frames */
       c - > max_b_frames = 2;
     }

     if (c - > codec_id == AV_CODEC_ID_MPEG1VIDEO) {
       /* Needed to avoid using macroblocks in which some coeffs overflow.
        * This does not happen with normal video, it just happens here as
        * the motion of the chroma plane does not match the luma plane. */
       c - > mb_decision = 2;
     }

     /* Some formats want stream headers to be separate. */
     if (oc - > oformat - > flags &amp; AVFMT_GLOBALHEADER)
       c - > flags |= CODEC_FLAG_GLOBAL_HEADER;

     //returning our lovely new brand stream.
     return st;

    }

    int changeResolution(string source, int format) {
     //Data members
     struct SwsContext * sws_ctx = NULL;
     AVFrame * pFrame = NULL;
     AVFrame * outFrame = NULL;
     AVPacket packet;
     uint8_t * buffer = NULL;
     uint8_t endcode[] = {
       0,
       0,
       1,
       0xb7
     };
     AVDictionary * optionsDict = NULL;
     AVFormatContext * pFormatCtx = NULL;
     AVFormatContext * outputContext = NULL;
     AVCodecContext * pCodecCtx;
     AVCodec * pCodec;
     AVCodec * codec;
     AVCodec * videoCodec;
     AVOutputFormat * fmt;
     AVStream * video_stream;
     int changeWidth;
     int changeHeight;
     int frameFinished;
     int numBytes;
     int fps;

     int lock = 0;

     //Register all codecs &amp; other important stuff. Vital!..
     av_register_all();

     //Selects the desired resolution.
     if (format == 0) {
       changeWidth = 320;
       changeHeight = 180;
     } else if (format == 1) {
       changeWidth = 640;
       changeHeight = 480;

     } else if (format == 2) {
       changeWidth = 960;
       changeHeight = 540;

     } else if (format == 3) {
       changeWidth = 1024;
       changeHeight = 768;

     } else {
       changeWidth = 1280;
       changeHeight = 720;
     }

     // Open video file
     int aaa;
     aaa = avformat_open_input( &amp; pFormatCtx, source.c_str(), NULL, NULL);
     if (aaa != 0) {
       cout &lt;&lt; " cannot open input file \n" &lt;&lt; endl;
       cout &lt;&lt; "aaa = " &lt;&lt; aaa &lt;&lt; endl;
       return -1; // Couldn't open file    
     }

     // Retrieve stream information
     if (av_find_stream_info(pFormatCtx) &lt; 0)
       return -1; // Couldn't find stream information

     //just checking duration casually for no reason
     /*int64_t duration = pFormatCtx->duration;

     cout &lt;&lt; "the duration is " &lt;&lt; duration &lt;&lt; " " &lt;&lt; endl;*/

     //this writes the info about the file
     av_dump_format(pFormatCtx, 0, 0, 0);
     cin >> lock;

     // Find the first video stream
     int videoStream = -1;
     int i;

     for (i = 0; i &lt; 3; i++)
       if (pFormatCtx - > streams[i] - > codec - > codec_type == AVMEDIA_TYPE_VIDEO) {
         videoStream = i;
         cout &lt;&lt; " lel \n ";
         break;

       }

     if (videoStream == -1)
       return -1; // Didn't find a video stream

     // Get a pointer to the codec context for the video stream
     pCodecCtx = pFormatCtx - > streams[videoStream] - > codec;
     fps = pCodecCtx - > time_base.den;

     //Find the decoder of the input file, for the video stream
     pCodec = avcodec_find_decoder(pCodecCtx - > codec_id);

     if (pCodec == NULL) {
       fprintf(stderr, "Unsupported codec!\n");
       return -1; // Codec not found
     }

     // Open codec, you must open it first, in order to use it.
     if (avcodec_open2(pCodecCtx, pCodec, &amp; optionsDict) &lt; 0)
       return -1; // Could not open codec

     // Allocate video frame ( pFrame for taking the packets into, outFrame for processed frames to packet.)
     pFrame = avcodec_alloc_frame();
     outFrame = avcodec_alloc_frame();

     i = 0;

     int ret;
     int video_frame_count = 0;

     //Initiate the outFrame set the buffer &amp; fill the properties
     numBytes = avpicture_get_size(PIX_FMT_YUV420P, changeWidth, changeHeight);
     buffer = (uint8_t * ) av_malloc(numBytes * sizeof(uint8_t));
     avpicture_fill((AVPicture * ) outFrame, buffer, PIX_FMT_YUV420P, changeWidth, changeHeight);

     int pp;
     int frameNo = 0;

     //allocate the outputContext, it will be the AVFormatContext of our output file.
     //It will try to find the format by giving the file name.
     avformat_alloc_output_context2( &amp; outputContext, NULL, NULL, "myoutput.mp4");

     //Cant find the file extension, using MPEG as default.
     if (!outputContext) {
       printf("Could not deduce output format from file extension: using MPEG.\n");
       avformat_alloc_output_context2( &amp; outputContext, NULL, "mpeg", "myoutput.mp4");
     }

     //Still cant set file extension, exit.
     if (!outputContext) {
       return 1;
     }

     //set AVOutputFormat fmt to our outputContext's format.
     fmt = outputContext - > oformat;
     video_stream = NULL;

     //If fmt has a valid codec_id, create a new video stream.
     //This function will set the streams codec &amp; codecs desired properties.
     //Stream's codec will be passed to videoCodec for later usage.
     if (fmt - > video_codec != AV_CODEC_ID_NONE)
       video_stream = addStream(outputContext, &amp; videoCodec, fmt - > video_codec, changeWidth, changeHeight, fps);

     //open the video using videoCodec. by avcodec_open2() i.e open the codec.
     if (video_stream)
       open_video(outputContext, videoCodec, video_stream);

     //Creating our new output file.
     if (!(fmt - > flags &amp; AVFMT_NOFILE)) {
       ret = avio_open( &amp; outputContext - > pb, "toBeStreamed.264", AVIO_FLAG_WRITE);
       if (ret &lt; 0) {
         cout &lt;&lt; " cant open file " &lt;&lt; endl;
         return 1;
       }
     }

     //Writing the header of format context.
     //ret = avformat_write_header(outputContext, NULL);

     if (ret >= 0) {
       cout &lt;&lt; "writing header success !!!" &lt;&lt; endl;
     }

     //Start reading packages from input file.
     while (av_read_frame(pFormatCtx, &amp; packet) >= 0) {

       // Is this a packet from the video stream?  
       if (packet.stream_index == videoStream) {

         // Decode video package into frames
         ret = avcodec_decode_video2(pCodecCtx, pFrame, &amp; frameFinished, &amp; packet);

         if (ret &lt; 0) {
           printf(" Error decoding frame !!..");
           return ret;
         }

         if (frameFinished) {
           printf("video_frame n:%d    coded_n:%d\n", video_frame_count++, pFrame - > coded_picture_number);
         }

         av_free_packet( &amp; packet);

         //do stuff with frame, in this case we are changing the resolution.
         static struct SwsContext * img_convert_ctx_in = NULL;
         if (img_convert_ctx_in == NULL) {
           img_convert_ctx_in = sws_getContext(pCodecCtx - > width,
             pCodecCtx - > height,
             pCodecCtx - > pix_fmt,
             changeWidth,
             changeHeight,
             PIX_FMT_YUV420P,
             SWS_BICUBIC,
             NULL,
             NULL,
             NULL);

         }
         //scale the frames
         sws_scale(img_convert_ctx_in,
           pFrame - > data,
           pFrame - > linesize,
           0,
           pCodecCtx - > height,
           outFrame - > data,
           outFrame - > linesize);

         //initiate the pts value
         if (frameNo == 0)
           outFrame - > pts = 0;

         //calculate the pts value &amp; set it.
         outFrame - > pts += av_rescale_q(1, video_stream - > codec - > time_base, video_stream - > time_base);

         //encode frames into packages. Package passed in @packet.
         if (avcodec_encode_video2(outputContext - > streams[0] - > codec, &amp; packet, outFrame, &amp; pp) &lt; 0)
           cout &lt;&lt; "Encoding frames into packages, failed. " &lt;&lt; endl;

         frameNo++;

         //write the packages into file, resulting in creating a video file.
         av_interleaved_write_frame(outputContext, &amp; packet);

       }

     }

     av_free_packet( &amp; packet);
     //av_write_trailer(outputContext);

     avio_close(outputContext - > pb);

     // Free the RGB image
     av_free(buffer);
     av_free(outFrame);

     // Free the YUV frame
     av_free(pFrame);

     // Close the codec
     avcodec_close(video_stream - > codec);
     avcodec_close(pCodecCtx);

     // Close the video file
     avformat_close_input( &amp; pFormatCtx);

     return 0;
    }
    </iostream>

    at the end of the process I get my desired file with desired codec & container & resolution.

    My problem is, in a part of our project I need to get elementary video streams IN file. Such as example.264. However I can not add a stream without creating an AVFormatContext. I can not create an AVFormatContext because 264 files does not have a container,they are just raw video ?, as far as I know.

    I have tried the way in decoding_encoding.c which uses fwrite. However that example was for mpeg-2 codec and when I try to adapt that code to H264/AVC codec, I got "floating point division by zero" error from mediainfo and moreover, some of the properties of the video was not showing (such as FPS & playtime & quality factor). I think it has to do with the "endcode" the example adds at the end of the code. It is for mpeg-2. ( uint8_t endcode[] = 0, 0, 1, 0xb7  ; )

    Anyway, I would love to get a startpoint for this task. I have managed to come this far by using internet resources ( quite few & outdated for ffmpeg) but now I’m stuck a little.

  • Stream RTP packets to FFMPEG [duplicate]

    21 mars 2017, par Johnathan Kanarek

    This question already has an answer here :

    I get RTP stream from WebRTC server (I used mediasoup) using node.js and I get the decrypted RTP packets raw data from the stream. I want to forward this RTP data to ffmpeg. I create SDP file that describes both the audio and video streams and send the packets through UDP.
    The SDP :

    v=0
    o=mediasoup 7199daf55e496b370e36cd1d25b1ef5b9dff6858 0 IN IP4 192.168.193.182
    s=7199daf55e496b370e36cd1d25b1ef5b9dff6858
    c=IN IP4 192.168.193.182
    t=0 0
    m=audio 33400 RTP/AVP 111
    a=rtpmap:111 /opus/48000
    a=fmtp:111 minptime=10;useinbandfec=1
    a=rtcp-fb:111 transport-cc
    a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level
    a=mid:audio
    a=recvonly
    m=video 33402 RTP/AVP 100
    a=rtpmap:100 /VP8/90000
    a=rtcp-fb:100 ccm fir
    a=rtcp-fb:100 nack
    a=rtcp-fb:100 nack pli
    a=rtcp-fb:100 goog-remb
    a=rtcp-fb:100 transport-cc
    a=extmap:2 urn:ietf:params:rtp-hdrext:toffset
    a=extmap:3 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time
    a=extmap:4 urn:3gpp:video-orientation
    a=extmap:5 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01
    a=extmap:6 http://www.webrtc.org/experiments/rtp-hdrext/playout-delay
    a=mid:video
    a=recvonly
    a=rtcp-mux

    The command :
    ffmpeg -loglevel debug -analyzeduration 2147483647 -probesize 2147483647 -protocol_whitelist file,crypto,udp,rtp -re -vcodec vp8 -acodec opus -i test.sdp -vcodec h264 -acodec aac -y output.mp4

    The log :

    ffmpeg version 3.2
    Copyright (c) 2000-2016 the FFmpeg developers


     built with gcc 4.4.7 (GCC) 20120313 (Red Hat 4.4.7-11)

     configuration: --prefix=/opt/kaltura/ffmpeg-3.2 --libdir=/opt/kaltura/ffmpeg-3.2/lib --shlibdir=/opt/kaltura/ffmpeg-3.2/lib --extra-cflags='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC' --extra-cflags='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC -I/opt/kaltura/include' --extra-ldflags=-L/opt/kaltura/lib --disable-devices --enable-bzlib --enable-libgsm --enable-libmp3lame --enable-libschroedinger --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libx265 --enable-avisynth --enable-libxvid --enable-filter=movie --enable-avfilter --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libvpx --enable-libspeex --enable-libass --enable-postproc --enable-pthreads --enable-static --enable-shared --enable-gpl --disable-debug --disable-optimizations --enable-gpl --enable-pthreads --enable-swscale --enable-vdpau --enable-bzlib --disable-devices --enable-filter=movie --enable-version3 --enable-indev=lavfi --enable-x11grab

     libavutil      55. 34.100 / 55. 34.100

     libavcodec     57. 64.100 / 57. 64.100

     libavformat    57. 56.100 / 57. 56.100

     libavdevice    57.  1.100 / 57.  1.100

     libavfilter     6. 65.100 /  6. 65.100

     libswscale      4.  2.100 /  4.  2.100

     libswresample   2.  3.100 /  2.  3.100

     libpostproc    54.  1.100 / 54.  1.100

    Splitting the commandline.

    Reading option '-loglevel' ...
    matched as option 'loglevel' (set logging level) with argument 'debug'.

    Reading option '-analyzeduration' ...
    matched as AVOption 'analyzeduration' with argument '2147483647'.

    Reading option '-probesize' ...
    matched as AVOption 'probesize' with argument '2147483647'.

    Reading option '-protocol_whitelist' ...
    matched as AVOption 'protocol_whitelist' with argument 'file,crypto,udp,rtp'.

    Reading option '-re' ...
    matched as option 're' (read input at native frame rate) with argument '1'.

    Reading option '-vcodec' ...
    matched as option 'vcodec' (force video codec ('copy' to copy stream)) with argument 'vp8'.

    Reading option '-acodec' ...
    matched as option 'acodec' (force audio codec ('copy' to copy stream)) with argument 'opus'.
    Reading option '-i' ... matched as input file with argument 'test.sdp'.
    Reading option '-vcodec' ... matched as option 'vcodec' (force video codec ('copy' to copy stream)) with argument 'h264'.
    Reading option '-acodec' ... matched as option 'acodec' (force audio codec ('copy' to copy stream)) with argument 'aac'.
    Reading option '-y' ... matched as option 'y' (overwrite output files) with argument '1'.
    Reading option 'output.mp4' ... matched as output file.
    Finished splitting the commandline.
    Parsing a group of options: global .
    Applying option loglevel (set logging level) with argument debug.
    Applying option y (overwrite output files) with argument 1.
    Successfully parsed a group of options.
    Parsing a group of options: input file test.sdp.
    Applying option re (read input at native frame rate) with argument 1.
    Applying option vcodec (force video codec ('copy' to copy stream)) with argument vp8.
    Applying option acodec (force audio codec ('copy' to copy stream)) with argument opus.
    Successfully parsed a group of options.
    Opening an input file: test.sdp.
    [sdp @ 0xb1ef00] Format sdp probed with size=2048 and score=50
    [sdp @ 0xb1ef00] audio codec set to: (null)
    [sdp @ 0xb1ef00] audio samplerate set to: 44100
    [sdp @ 0xb1ef00] audio channels set to: 1
    [sdp @ 0xb1ef00] video codec set to: (null)
    [udp @ 0xb21940] end receive buffer size reported is 131072
    [udp @ 0xb21660] end receive buffer size reported is 131072
    [sdp @ 0xb1ef00] setting jitter buffer size to 500
    [udp @ 0xb21da0] end receive buffer size reported is 131072
    [udp @ 0xb22060] end receive buffer size reported is 131072
    [sdp @ 0xb1ef00] setting jitter buffer size to 500

    [sdp @ 0xb1ef00] Before avformat_find_stream_info() pos: 889 bytes read:889 seeks:0 nb_streams:2

    [vp8 @ 0xb27600] Header size larger than data provided

       Last message repeated 2 times
    [sdp @ 0xb1ef00] Non-increasing DTS in stream 1: packet 2 with DTS 0, packet 3 with DTS 0
    [vp8 @ 0xb27600] Header size larger than data provided

    ... repeats many times until I kill the socket ...

       Last message repeated 1 times
    [sdp @ 0xb1ef00] Non-increasing DTS in stream 1: packet 273 with DTS 553050, packet 274 with DTS 553050
    [vp8 @ 0xb27600] Header size larger than data provided

    received id=7199daf55e496b370e36cd1d25b1ef5b9dff6858 type=bye
    PeerConnection close. id=7199daf55e496b370e36cd1d25b1ef5b9dff6858
    -- PeerConnection.closed,  err: undefined
    -- peers in the room = 0
    [sdp @ 0xb1ef00] decoding for stream 1 failed
    [sdp @ 0xb1ef00] Could not find codec parameters for stream 1 (Video: vp8, 1 reference frame, yuv420p): unspecified size
    Consider increasing the value for the 'analyzeduration' and 'probesize' options
    [sdp @ 0xb1ef00] After avformat_find_stream_info() pos: 889 bytes read:889 seeks:0 frames:584
    Input #0, sdp, from 'test.sdp':
     Metadata:
       title           : 7199daf55e496b370e36cd1d25b1ef5b9dff6858
     Duration: N/A, start: 0.000000, bitrate: N/A
       Stream #0:0, 309, 1/90000: Audio: opus, 48000 Hz, mono, fltp
       Stream #0:1, 275, 1/90000: Video: vp8, 1 reference frame, yuv420p, 90k tbr, 90k tbn, 90k tbc
    Successfully opened the file.
    Parsing a group of options: output file output.mp4.
    Applying option vcodec (force video codec ('copy' to copy stream)) with argument h264.
    Applying option acodec (force audio codec ('copy' to copy stream)) with argument aac.
    Successfully parsed a group of options.
    Opening an output file: output.mp4.
    Matched encoder 'libx264' for codec 'h264'.

    [file @ 0xbc56e0]
    Setting default whitelist 'file,crypto'

    Successfully opened the file.

    detected 1 logical cores

    [graph 0 input from stream 0:1 @ 0xb1eca0]
    Setting 'video_size' to value '0x0'

    [buffer @ 0xbc54e0]
    Unable to parse option value "0x0" as image size

    [graph 0 input from stream 0:1 @ 0xb1eca0]
    Setting 'pix_fmt' to value '0'

    [graph 0 input from stream 0:1 @ 0xb1eca0]
    Setting 'time_base' to value '1/90000'

    [graph 0 input from stream 0:1 @ 0xb1eca0] Setting 'pixel_aspect' to value '0/1'
    [graph 0 input from stream 0:1 @ 0xb1eca0] Setting 'sws_param' to value 'flags=2'
    [graph 0 input from stream 0:1 @ 0xb1eca0] Setting 'frame_rate' to value '90000/1'
    [buffer @ 0xbc54e0] Unable to parse option value "0x0" as image size
    [buffer @ 0xbc54e0] Error setting option video_size to value 0x0.
    [graph 0 input from stream 0:1 @ 0xb1eca0] Error applying options to the filter.
    Error opening filters!
    [AVIOContext @ 0xbc57c0] Statistics: 0 seeks, 0 writeouts

    [AVIOContext @ 0xb1f8c0]
    Statistics: 889 bytes read, 0 seeks

    As you can see, at the beginning of the log the SDP parsed without recognizing the codecs :

    Opening an input file: test.sdp.
    [sdp @ 0xb1ef00] Format sdp probed with size=2048 and score=50
    [sdp @ 0xb1ef00] audio codec set to: (null)
    [sdp @ 0xb1ef00] audio samplerate set to: 44100
    [sdp @ 0xb1ef00] audio channels set to: 1
    [sdp @ 0xb1ef00] video codec set to: (null)

    Then it’s trying to read the packets from the sockets.
    Only when I close the socket, ffmpeg continues to parse the SDP, this time finding the correct codec :

    Opening an input file: test.sdp.
    [sdp @ 0xb1ef00] Format sdp probed with size=2048 and score=50
    [sdp @ 0xb1ef00] audio codec set to: (null)
    [sdp @ 0xb1ef00] audio samplerate set to: 44100
    [sdp @ 0xb1ef00] audio channels set to: 1
    [sdp @ 0xb1ef00] video codec set to: (null)

    I suspect that the "Non-increasing DTS" and "Header size larger than data provided" errors are caused by wrong parsing of the packets due to usage with the wrong codec.

    I checked the SDP order and it seems the same as in other examples I have.

    Can someone suggest an explanation ?

    BTW, audio only works fine, but I guess it’s because of the simplicity of OPUS.

    Thanks.

  • Xbox Sphinx Protocol

    21 octobre 2013, par Multimedia Mike — DRM, xbox

    I’ve gone down the rabbit hole of trying to read the Xbox DVD drive from Linux. Honestly, I’m trying to remember why I even care at this point. Perhaps it’s just my metagame of trying to understand how games and related technologies operate. In my last post of the matter, I determined that it is possible to hook an Xbox drive up to a PC using a standard 40-pin IDE interface and read data sectors. However, I learned that just because the Xbox optical drive is reading an Xbox disc, that doesn’t mean it’s just going to read the sectors in response to a host request.

    Oh goodness, no. The drive is going to make the host work for those sectors.

    To help understand the concept of locked/unlocked sectors on an Xbox disc, I offer this simplistic diagram :


    Xbox locked disc diagram

    Any DVD drive (including the Xbox drive) is free to read those first 6992 sectors (about 14 MB of data) which just contain a short DVD video asking the user to insert the disc into a proper Xbox console. Reading the remaining sectors involves performing a sequence of SCSI commands that I have taken to calling the “Sphinx Protocol” for reasons I will explain later in this post.

    References
    Doing a little Googling after my last post on the matter produced this site hosting deep, technical Xbox information. It even has a page about exactly what I am trying to achieve : Use an Xbox DVD Drive in Your PC. The page provides a tool named dvdunlocker written by “The Specialist” to perform the necessary unlocking. The archive includes a compiled Windows binary as well as its source code. The source code is written in Delphi Pascal and leverages Windows SCSI APIs. Still, it is well commented and provides a roadmap, which I will try to describe in this post.

    Sphinx Protocol
    Here is a rough flowchart of the steps that are (probably) involved in the unlocking of those remaining sectors. I reverse engineered this based on the Pascal tool described in the previous section. Disclaimer : at the time of this writing, I haven’t tested all of the steps due to some Linux kernel problems, described later.


    Xbox SCSI Unlock Protocol

    Concerning the challenge/response table that the drive sends back, it’s large (0×664 / 1636 bytes), and not all of the bytes’ meanings are known. However, these are the bytes that seem to be necessary (all multi-byte numbers are big endian) :

     bytes 0-1        Size of mode page payload data (should be 0x0662)
     bytes 2-771      Unknown
     byte  772        Should be 1
     byte  773        Number of entries in challenge/response table
     bytes 774-1026   Encrypted challenge/response table
     bytes 1027-1186  Unknown
     bytes 1187-1230  Key basis (44 bytes)
     bytes 1231-1635  Unknown
    

    The challenge/response table is the interesting part, but it’s encrypted with RC4 a.k.a. ARCFOUR. The key is derived from the 44 bytes I have labeled “key basis”– cryptographic literature probably has a better term for it ; chime in if you know what that might be. An SHA-1 hash is computed over the 44 bytes.

    The resulting SHA-1 hash — the first part of it, to be exact — is fed as the key into the RC4 decryption. The output of SHA-1 contains 160 bits of information. 160 / 8 = 20 bytes of information. To express this as a printable hex digest requires 40 characters. The SHA-1 hash is converted to a hex digest and then the first 7 of the characters are fed into the RC4 initialization function as the key. Then, the RC4 decrypter does its work on the 253 bytes of the challenge/response table.

    So that’s why I took to calling this the “Sphinx Protocol” — I felt like I was being challenged with a bizarre riddle. Perhaps that describes a lot of cryptosystems, though You have to admit it sounds kind of cool.

    The challenge/response table contains 23 11-byte records. The format of this table is (again, multi-byte numbers are big-endian) :

     byte  0     This is 1 if this challenge/response pair is valid
     byte  1     Challenge ID
     bytes 2-5   Challenge
     byte  6     Response ID
     bytes 7-10  Response
    

    Example
    It’s useful to note that the challenge/response table and associated key is different for every disc (at least all the ones I have looked at). So this might be data that comes from the disc, since the values will always be the same for a given disc.

    Let’s examine Official Xbox Magazine disc #16 (Indiana Jones and The Emperor’s Tomb) :


    Xbox Magazine #16 featuring Indiana Jones

    Before I decrypt the challenge/response table, it looks like this :

       0 : 180, 172 : 0xEB100059 ;  66 : 0xD56AFB56
       1 :  34,  71 : 0x8F9BF03A ; 192 : 0xC32CBDF8
       2 : 226, 216 : 0xA29B77F2 ;  12 : 0x4474A6F1
       3 :  72, 122 : 0x9F5ABF33 ; 255 : 0xC5E3C304
       4 :   1, 103 : 0x76142ADA ; 233 : 0xDE145D42 ****
       5 :  49, 193 : 0xA1CD6192 ; 189 : 0x2169DBA5
       6 : 182, 250 : 0x9977894F ;  96 : 0x5A929E2B
       7 : 148,  71 : 0x6DD10A54 ; 115 : 0xF0BDAC4F
       8 :  12,  45 : 0x5D5EB6FD ; 148 : 0x84E60A00
       9 :  99, 121 : 0xFEAED372 ; 201 : 0xDA9986F9
      10 : 172, 230 : 0xE6C0D0B4 ; 214 : 0x9050C250
      11 :  84,  65 : 0x95CB8775 ; 104 : 0x550886C6
      12 : 210,  65 : 0x1ED23619 ; 171 : 0x6DF4A35B
      13 :   2, 155 : 0xD0AAE1E0 ; 130 : 0x00D1FFCF
      14 :  40,   2 : 0x172EFEB8 ; 159 : 0x37E03E50
      15 :  49,  15 : 0x43E5E378 ; 223 : 0x267F9C9A
      16 : 240, 173 : 0x357D5D1C ; 250 : 0x24965D67
      17 :  80, 184 : 0x5E7AF1A3 ;  81 : 0x3A8F69A7
      18 : 154, 186 : 0x6626BEAC ; 245 : 0xE639540A
      19 : 231, 249 : 0xFABAAFB7 ; 227 : 0x4C686A07
      20 : 150, 186 : 0x9A6D7AA3 ; 133 : 0x25971CF0
      21 : 236, 192 : 0x5CD97DD4 ; 247 : 0x26655EFB
      22 :  68, 173 : 0xE2D372E4 ; 207 : 0x103FBF94
    there are 1 valid pairs in the list : 4
    

    My best clue that it’s not right is that there is only 1 valid entry (denoted by my tool using ****). The source I reverse engineered for this data indicates that there needs to be at least 2 valid pairs. After running the RC4 decryption on the table, it looks like this and I get far more valid pairs :

       0 :   1, 174 : 0xBD628255 ;   0 : 0x9F0A31AF ****
       1 :   2, 176 : 0x3151B341 ;   2 : 0x9C87C180
       2 :   3, 105 : 0x018879E5 ;   1 : 0xFF068B5C
       3 :   2,   7 : 0x1F316AAF ;   3 : 0xF420D3ED
       4 :   3,  73 : 0xC2EBFBE9 ;   0 : 0x17062B5B
       5 : 252, 163 : 0xFF14B5CB ; 236 : 0xAF813FBC
       6 :   2, 233 : 0x5EE95C49 ;   1 : 0x37AA5511
       7 :   1, 126 : 0xBD628255 ;   0 : 0x5BA3FBD4 ****
       8 :   3,   4 : 0xB68BFEE6 ;   3 : 0xA8F3B918
       9 :   3,  32 : 0xEA614943 ;   2 : 0xA678D715
      10 :   2, 248 : 0x1BDD374E ;   0 : 0x8D2AC2C7
      11 :   3,  17 : 0x0EABCE81 ;   2 : 0xC90A7242
      12 :   1, 186 : 0xBD628255 ;   0 : 0xC4820242 ****
      13 :   3, 145 : 0xB178F942 ;   3 : 0x4D78AD62
      14 :   3,  37 : 0x4A6CE5E2 ;   2 : 0xBF94E1C6
      15 :   1, 102 : 0xBD628255 ;   0 : 0xFFB83D8D ****
      16 :   3, 122 : 0xF97B0905 ;   1 : 0x38533125
      17 :   3, 197 : 0x57A6865D ;   2 : 0xA61D31EF
      18 :   3,  27 : 0xC7227D7C ;   2 : 0xA3F9BA1E
      19 :   1,  16 : 0xBD628255 ;   0 : 0x8557CCC8 ****
      20 :   2,  53 : 0x1DA9D156 ;   3 : 0xC9051754
      21 :   2,  90 : 0x3CD66BEE ;   3 : 0xFD851D3E
      22 :   1, 252 : 0xBD628255 ;   0 : 0xB3F22701 ****
    there are 6 valid pairs in the list : 0 7 12 15 19 22
    

    So, hopefully, I have the decryption correct.

    Also of note is that you only get one chance to get this unlocking correct– fail, and the drive won’t return a valid DVD structure block again. You will either need to reboot the Xbox or eject & close the tray before you get to try again.

    Problems Making It Work In Linux
    There are a couple of ways to play with SCSI protocols under Linux. In more recent kernels, block devices are named /dev/sda, /dev/sdb, etc. Each of these block devices has a corresponding character device named /dev/sg0, /dev/sg1, etc. ‘sg’ stands for SCSI generic. This character devices can be opened as readable and/or writable and SCSI commands can be freely written with write() and data retrieved with read(). Pretty powerful.

    Except that the one machine I still possess which supports 40-pin IDE/ATAPI devices is running Linux kernel 2.6.24 which dates back to early 2008 and it still enumerates the IDE block devices as /dev/hda, /dev/hdb, etc. There are no corresponding /dev/sgX character devices. What to do ? It seems that a program can still issue SCSI commands using an ioctl() facility named SG_IO.

    I was able to make the SG_IO ioctl() work for the most part (except for the discovery that the Xbox drive doesn’t respond to a basic SCSI Inquiry command). However, I ran into a serious limitation– a program can only open a /dev/hdX block device in read-only mode if the device corresponds to a read-only drive like, for example, a DVD-ROM drive. This means that a program can’t issue SCSI mode select commands to the drive, which counts as writing. This means that my tool can’t unlock the drive.

    Current Status
    So this is where my experiment is blocked right now. I have been trying to compile various Linux kernels to remedy the situation. But I always seem to find myself stuck in one of 2 situations, depending on the configuration options I choose : Either the drives are enumerated with the /dev/hdX convention and I am stuck in read-only mode (with no mode select) ; or the drives are enumerated with /dev/sdX along with corresponding /dev/sgN character devices, in which case the kernel does not recognize the Xbox DVD-ROM drive.

    This makes me wonder if there’s a discrepancy between the legacy ATA/ATAPI drivers (which sees the drive) and the newer SATA/PATA subsystem (which doesn’t see the drive). I also wonder about hacking the kernel logic to allow SCSI mode select logic to proceed to the device for a read-only file handle.