Recherche avancée

Médias (1)

Mot : - Tags -/iphone

Autres articles (104)

  • Soumettre améliorations et plugins supplémentaires

    10 avril 2011

    Si vous avez développé une nouvelle extension permettant d’ajouter une ou plusieurs fonctionnalités utiles à MediaSPIP, faites le nous savoir et son intégration dans la distribution officielle sera envisagée.
    Vous pouvez utiliser la liste de discussion de développement afin de le faire savoir ou demander de l’aide quant à la réalisation de ce plugin. MediaSPIP étant basé sur SPIP, il est également possible d’utiliser le liste de discussion SPIP-zone de SPIP pour (...)

  • Emballe médias : à quoi cela sert ?

    4 février 2011, par

    Ce plugin vise à gérer des sites de mise en ligne de documents de tous types.
    Il crée des "médias", à savoir : un "média" est un article au sens SPIP créé automatiquement lors du téléversement d’un document qu’il soit audio, vidéo, image ou textuel ; un seul document ne peut être lié à un article dit "média" ;

  • ANNEXE : Les plugins utilisés spécifiquement pour la ferme

    5 mars 2010, par

    Le site central/maître de la ferme a besoin d’utiliser plusieurs plugins supplémentaires vis à vis des canaux pour son bon fonctionnement. le plugin Gestion de la mutualisation ; le plugin inscription3 pour gérer les inscriptions et les demandes de création d’instance de mutualisation dès l’inscription des utilisateurs ; le plugin verifier qui fournit une API de vérification des champs (utilisé par inscription3) ; le plugin champs extras v2 nécessité par inscription3 (...)

Sur d’autres sites (10187)

  • libav + AV_PIX_FMT_YUV420P + nvjpeg gives green images

    18 mai 2023, par george_d

    I need to grab frames from remote source and save them as JPEG, and I want to utilize GPU for that purpose.

    


    To achieve that, I made my own grabber based on this libav example, which decodes frames using hardware. After that I pass them to nvjpeg.

    


    I also set software frame to be in format of planar yuv 4:2:0 (and not nv12, which is not planar), which is mandatory for nvjpeg's nvjpegEncodeYUV function.

    


    But as I pass the frame to nvjpegEncodeYUV(), the resulting frame comes in green (example).

    


    So, here is the libav code :

    


    static void
encode_to_jpeg(const uint8_t* raw_data,
               const int size,
               const int width,
               const int height,
               char* output_filename)
{
  JpegCoder jpegCoder = JpegCoder();
  JpegCoderImage* jpegImage =
    new JpegCoderImage(width, height, 3, JPEGCODER_CSS_420);
  jpegImage->fill(raw_data);
  JpegCoderBytes* dataContainer = jpegCoder.encode(jpegImage, 70);

  maybe_create_dir_for_output_filename(output_filename);
  write_bin_data_to_file(
    output_filename, (char*)dataContainer->data, dataContainer->size);

  delete dataContainer;
  delete jpegImage;
}

static int
process_packet(AVCodecContext* avctx,
               AVPacket* packet)
{
  AVFrame* frame = NULL;
  AVFrame* sw_frame = NULL;
  AVFrame* tmp_frame = NULL;
  uint8_t* buffer = NULL;
  char* frame_filename = NULL;
  int size;
  int ret = 0;

  ret = avcodec_send_packet(avctx, packet);
  if (ret < 0) {
    av_log(NULL, AV_LOG_ERROR, "Error during decoding\n");
    return ret;
  }

  while (true) {
    if (!(frame = av_frame_alloc()) || !(sw_frame = av_frame_alloc())) {
      av_log(NULL, AV_LOG_ERROR, "Can not alloc frame\n");
      ret = AVERROR(ENOMEM);
      goto fail;
    }

    sw_frame->format = AV_PIX_FMT_YUV420P; // here i force the frames to be in yuv 4:2:0 planar format

    ret = avcodec_receive_frame(avctx, frame);
    if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
      av_frame_free(&frame);
      av_frame_free(&sw_frame);
      av_freep(&buffer);
      return 0;
    } else if (ret < 0) {
      av_log(NULL, AV_LOG_ERROR, "Error while decoding\n");
      goto fail;
    }

    if (frame->format == hw_pix_fmt) {
      // pass the data from GPU to CPU
      if ((ret = av_hwframe_transfer_data(sw_frame, frame, 0)) < 0) {
        av_log(
          NULL, AV_LOG_ERROR, "Error transferring the data to system memory\n");
        goto fail;
      }
      tmp_frame = sw_frame;
    } else {
      tmp_frame = frame;
    }

    size = av_image_get_buffer_size(
      (AVPixelFormat)tmp_frame->format, tmp_frame->width, tmp_frame->height, 1);

    buffer = (uint8_t*)av_malloc(size);
    if (!buffer) {
      av_log(NULL, AV_LOG_ERROR, "Can not alloc buffer\n");
      ret = AVERROR(ENOMEM);
      goto fail;
    }
    ret = av_image_copy_to_buffer(buffer,
                                  size,
                                  (const uint8_t* const*)tmp_frame->data,
                                  (const int*)tmp_frame->linesize,
                                  (AVPixelFormat)tmp_frame->format,
                                  tmp_frame->width,
                                  tmp_frame->height,
                                  1);
    if (ret < 0) {
      av_log(NULL, AV_LOG_ERROR, "Can not copy image to buffer\n");
      goto fail;
    }
    frame_filename = get_frame_filename((uintmax_t)avctx->frame_number);
    encode_to_jpeg(
      buffer, size, tmp_frame->width, tmp_frame->height, frame_filename);
    free(frame_filename);

  fail:
    // av_frame_free(&filtered_frame);
    av_frame_free(&frame);
    av_frame_free(&sw_frame);
    av_freep(&buffer);
    if (ret < 0) {
      return ret;
    }
  }
}



    


    Basically, I adopted nvjpeg part from the nvjpeg Python wrapper, so here i will post only the differing parts.

    


    #define JPEGCODER_GLOBAL_CONTEXT                                               \
  ((NvJpegGlobalContext*)(JpegCoder::_global_context))
#define ChromaSubsampling_Covert_JpegCoderToNvJpeg(subsampling)                \
  ((nvjpegChromaSubsampling_t)(subsampling))
#define ChromaSubsampling_Covert_NvJpegToJpegCoder(subsampling)                \
  ((JpegCoderChromaSubsampling)(subsampling))

size_t
getBufferSize(size_t width, size_t height)
{
  return (size_t)(width * height);
}

JpegCoderImage::JpegCoderImage(size_t width,
                               size_t height,
                               short nChannel,
                               JpegCoderChromaSubsampling subsampling)
{
  unsigned char* pBuffer = nullptr;
  cudaError_t eCopy =
    cudaMalloc((void**)&pBuffer, width * height * NVJPEG_MAX_COMPONENT);
  if (cudaSuccess != eCopy) {
    throw JpegCoderError(eCopy, cudaGetErrorString(eCopy));
  }

  this->height = height;
  this->width = width;
  this->nChannel = nChannel;
  this->subsampling = subsampling;

  nvjpegImage_t* img = (nvjpegImage_t*)malloc(sizeof(nvjpegImage_t));
  // More verbose, but readable
  img->channel[0] = pBuffer;
  img->channel[1] = pBuffer + (width * height);
  img->channel[2] = pBuffer + (width * height) + ((width / 2) * height);
  img->channel[3] = NULL;

  img->pitch[0] = (unsigned int)width;
  img->pitch[1] = (unsigned int)width / 2;
  img->pitch[2] = (unsigned int)width / 2;
  img->pitch[3] = 0;

  this->img = img;
}

void
JpegCoderImage::fill(const unsigned char* data)
{
  cudaError_t eCopy = cudaMemcpy(((nvjpegImage_t*)(this->img))->channel[0],
                                 data,
                                 getBufferSize(width, height),
                                 cudaMemcpyHostToDevice);
  if (cudaSuccess != eCopy) {
    throw JpegCoderError(eCopy, cudaGetErrorString(eCopy));
  }
  this->subsampling = JPEGCODER_CSS_420;
}


JpegCoderBytes*
JpegCoder::encode(JpegCoderImage* img, int quality)
{
  nvjpegHandle_t nv_handle = JPEGCODER_GLOBAL_CONTEXT->nv_handle;
  nvjpegEncoderState_t nv_enc_state = JPEGCODER_GLOBAL_CONTEXT->nv_enc_state;
  nvjpegEncoderParams_t nv_enc_params;

  nvjpegEncoderParamsCreate(nv_handle, &nv_enc_params, NULL);

  nvjpegEncoderParamsSetQuality(nv_enc_params, quality, NULL);
  nvjpegEncoderParamsSetOptimizedHuffman(nv_enc_params, 1, NULL);
  nvjpegEncoderParamsSetSamplingFactors(
    nv_enc_params,
    ChromaSubsampling_Covert_JpegCoderToNvJpeg(img->subsampling),
    NULL);
  int nReturnCode = nvjpegEncodeYUV(nv_handle,
                                    nv_enc_state,
                                    nv_enc_params,
                                    (nvjpegImage_t*)(img->img),
                                    ChromaSubsampling_Covert_JpegCoderToNvJpeg(img->subsampling),
                                    (int)img->width,
                                    (int)img->height,
                                    NULL);
  if (NVJPEG_STATUS_SUCCESS != nReturnCode) {
    throw JpegCoderError(nReturnCode, "NvJpeg Encoder Error");
  }

  size_t length;
  nvjpegEncodeRetrieveBitstream(nv_handle, nv_enc_state, NULL, &length, NULL);

  JpegCoderBytes* jpegData = new JpegCoderBytes(length);
  nvjpegEncodeRetrieveBitstream(
    nv_handle, nv_enc_state, jpegData->data, &(jpegData->size), NULL);

  nvjpegEncoderParamsDestroy(nv_enc_params);
  return jpegData;
}


    


    I tried removing implicit pixel format conversion and implementing nv12 to yuv420p conversion by myself, but it gave the same result.

    


    I also tried using AV_PIX_FMT_BGR24 and nvJpegEncode, which did not work either - pictures become completely messed up.

    


    The only thing worked for me before was using swscale + AV_PIX_FMT_BGR24 + nvjpegEncodeImage - but swscale gives large CPU overhead, which is not something I want to have.

    


    How can I make this thing work properly ?

    


  • Why is my DSharpPlus Slash Command not playing my desired sound using FFMPEG in C# ?

    19 mai 2023, par IngeniousThoughts

    I'm having a problem with my ffmpeg setup the commands work fine but my play command doesn't play my desired sound.

    


    //The command.
        [SlashCommand("play", "plays a sound in a voice channel.")]
        public async Task HowlCommand(InteractionContext ctx, [Choice("ChoiceName", "C:\\My\\Program\\Directory\\Name\\MySound.mp3")]
                                                             [Option("Sound", "Please select a Sound")] string filepath)
        {
            //Creates a slash command used response.
            //Also removes the error message.
            await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder()
                                                                                                                    .WithContent("Playing sound in voice channel. Please wait just a moment!"));
 
            //Checks if the user is not a bot to send the message.
            if (ctx.Member.IsBot)
            {
                return;
            }
            else
            {
                if(filepath != "C:\\My\\Program\\Directory\\Name\\MySound.mp3")
                {
                    var embedmessage = new DiscordMessageBuilder()
                        .AddEmbed(new DiscordEmbedBuilder()
 
                        .WithAuthor("BotName", null, ctx.Client.CurrentApplication.Icon)
                        .WithTitle("Please select the following sound to play:")
                        .WithImageUrl(ctx.Client.CurrentApplication.Icon)
                        .WithFooter("VoiceChannel Error.", "ImageURL.png")
                        .WithTimestamp(DateTime.Now)
                        .Build()
 
                        );
 
                    //Makes the command wait 5 seconds before sending the rest of the command data.
                    await Task.Delay(TimeSpan.FromSeconds(5));
 
                    //Sends the embed in a message.
                    await ctx.Channel.SendMessageAsync(embedmessage);
                }
                else
                {
                    //Makes the command wait 5 seconds before sending the rest of the command data.
                    await Task.Delay(TimeSpan.FromSeconds(5));
 
 
                    var vnext = ctx.Client.GetVoiceNext();
                    var vnc = vnext.GetConnection(ctx.Guild);
                    
                    //if null throws exception.
                    if (vnc == null)
                        throw new System.InvalidOperationException("Not connected in this guild.");
 
 
                    //Gets the mp3 file to use.
                    var ffmpeg = Process.Start(new ProcessStartInfo
                    {
                        FileName = "ffmpeg",
                        Arguments = $@"-i ""{filepath}"" -ac 2 -f s16le -ar 48000 pipe:1",
                        RedirectStandardOutput = true,
                        UseShellExecute = false
                    });
                    Stream pcm = ffmpeg.StandardOutput.BaseStream;
 
                    VoiceTransmitSink transmit = vnc.GetTransmitSink();
                    await pcm.CopyToAsync(transmit);
                    vnc.GetTransmitSink().VolumeModifier = 5;
 
                    //Makes the command wait 10 seconds before sending the rest of the command data.
                    await Task.Delay(TimeSpan.FromSeconds(10));
 
                    //Disconnects the bot from the voice channel.
                    vnc.Disconnect();
                }
            }
        }


    


    //The command.
        [SlashCommand("join", "Joins a voice channel.")]
        public async Task JoinChannel(InteractionContext ctx, [Choice("MyVoiceChannel", "VoiceChannelName")]
                                                             [Option("VoiceChannel", "Please choose a Voice Channel.")] DiscordChannel channel)
        {
            //Creates a slash command used response.
            //Also removes the error message.
            await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder()
                                                                                                                    .WithContent("Joining voice channel. Please wait just a moment!"));
 
            //Checks if the user is not a bot to send the message.
            if (ctx.Member.IsBot)
            {
                return;
            }
            else
            {
                if (channel.Name != "MyVoiceChannelName")
                {
                    var embedmessage = new DiscordMessageBuilder()
                        .AddEmbed(new DiscordEmbedBuilder()
 
                        .WithAuthor("BotName", null, ctx.Client.CurrentApplication.Icon)
                        .WithTitle("Please Create The Following Voice Channel:")
                        .WithImageUrl(ctx.Client.CurrentApplication.Icon)
                        .AddField("VoiceChannel:", "**BotName**" + Environment.NewLine + "Is Case Sensitive: **Yes**")
                        .WithFooter("VoiceChannel Error.", "ImageURL.png")
                        .WithTimestamp(DateTime.Now)
                        .Build()
 
                        );
 
                    //Makes the command wait 5 seconds before sending the rest of the command data.
                    await Task.Delay(TimeSpan.FromSeconds(5));
 
                    //Sends the embed in a message.
                    await ctx.Channel.SendMessageAsync(embedmessage);
                }
                else
                {
                    //Makes the command wait 5 seconds before sending the rest of the command data.
                    await Task.Delay(TimeSpan.FromSeconds(5));
 
 
                    channel = ctx.Member.VoiceState?.Channel;
                    await channel.ConnectAsync();
 
                }
            }
        }
 
    }
}


    


    public sealed class Program&#xA;    {&#xA;        public static DiscordClient Client { get; private set; }&#xA;        public static InteractivityExtension Interactivity { get; private set; }&#xA;        public static CommandsNextExtension Commands { get; private set; }&#xA;        public static VoiceNextExtension VoiceNext { get; private set; }&#xA; &#xA; &#xA;        static async Task Main(string[] args)&#xA;        {&#xA; &#xA;            //Main Window configs specifying the title name and color.&#xA;            Console.BackgroundColor = ConsoleColor.Black;&#xA;            Console.ForegroundColor = ConsoleColor.Magenta;&#xA;            Console.Title = "BotName";&#xA; &#xA;            //1. Get the details of your config.json file by deserialising it&#xA;            var configJsonFile = new JSONReader();&#xA;            await configJsonFile.ReadJSON();&#xA; &#xA;            //2. Setting up the Bot Configuration&#xA;            var discordConfig = new DiscordConfiguration()&#xA;            {&#xA;                Intents = DiscordIntents.All,&#xA;                Token = configJsonFile.token,&#xA;                TokenType = TokenType.Bot,&#xA;                AutoReconnect = true&#xA;            };&#xA; &#xA;            //3. Apply this config to our DiscordClient&#xA;            Client = new DiscordClient(discordConfig);&#xA; &#xA;            //4. Set the default timeout for Commands that use interactivity&#xA;            Client.UseInteractivity(new InteractivityConfiguration()&#xA;            {&#xA;                Timeout = TimeSpan.FromMinutes(2)&#xA;            });&#xA; &#xA;            //5. Set up the Task Handler Ready event&#xA;            Client.Ready &#x2B;= OnClientReady;&#xA; &#xA;            //6. Set up the Commands Configuration&#xA;            var commandsConfig = new CommandsNextConfiguration()&#xA;            {&#xA;                StringPrefixes = new string[] { configJsonFile.prefix },&#xA;                EnableMentionPrefix = true,&#xA;                EnableDms = true,&#xA;                EnableDefaultHelp = false,&#xA;            };&#xA; &#xA;            Commands = Client.UseCommandsNext(commandsConfig);&#xA; &#xA;            //7. Register your commands&#xA;            var slashCommandsConfig = Client.UseSlashCommands();&#xA;            slashCommandsConfig.RegisterCommands<mysoundscommand>(MyGuildID);&#xA; &#xA;            //8. Allows usage of voice channels.&#xA;            var VoiceNext = Client.UseVoiceNext();&#xA; &#xA;            //9. Connect to get the Bot online&#xA;            await Client.ConnectAsync();&#xA;            await Task.Delay(-1);&#xA;        }&#xA; &#xA;        private static Task OnClientReady(DiscordClient sender, ReadyEventArgs e)&#xA;        {&#xA;            return Task.CompletedTask;&#xA;        }&#xA;    }&#xA;</mysoundscommand>

    &#xA;

    SourceCode Link :

    &#xA;

    text

    &#xA;

    playing the playsound slash command but wasn't expecting it to not play the mp3 file.

    &#xA;

    everything else worked fine except when it transmits the sound it doesn't play it.

    &#xA;

  • Memory Leak in c++/cli application

    10 décembre 2013, par Ankush

    I am passing bitmap from my c# app to c++/cli dll that add it to video.
    The problem is program slowly leaking memory. I tried _CrtDumpMemoryLeaks() shows me leak of bitmap & another 40 byte leak but i am disposing bitmap.
    Can anyone see memory leak, Here is code..

    Flow :

    1) Capture screenshot by takescreenshot()

    2) pass it to c++/cli function

    3) dispose bitmap

    lines from my c# app

    Bitmap snap = takescreeshot();
    vencoder.AddBitmap(snap);
    snap.Dispose();
    vencoder.printleak();

    private static Bitmap takescreeshot()
       {
           System.Drawing.Bitmap bitmap = null;
           System.Drawing.Graphics graphics = null;

           bitmap = new Bitmap
           (
               System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width,
               System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height,
               System.Drawing.Imaging.PixelFormat.Format24bppRgb
           );

           graphics = System.Drawing.Graphics.FromImage(bitmap);

           graphics.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size);

           //Write TimeSpamp
           Rectangle rect = new Rectangle(1166, 738, 200, 20);
           String datetime= System.String.Format("{0:dd:MM:yy  hh:mm:ss}",DateTime.Now);
           System.Drawing.Font sysfont = new System.Drawing.Font("Times New Roman", 14, FontStyle.Bold);
           graphics.DrawString(datetime, sysfont, Brushes.Red,rect);
           //

           Grayscale filter = new Grayscale(0.2125, 0.7154, 0.0721);
           Bitmap grayImage = filter.Apply(bitmap);

           //Dispose
           bitmap.Dispose();
           graphics.Dispose();

           return grayImage;
       }

    now in c++/cli dll

    bool VideoEncoder::AddBitmap(Bitmap^ bitmap)
       {
           BitmapData^ bitmapData = bitmap->LockBits( System::Drawing::Rectangle( 0, 0,bitmap->Width, bitmap->Height ),ImageLockMode::ReadOnly,PixelFormat::Format8bppIndexed);
           uint8_t* ptr = reinterpret_cast( static_cast( bitmapData->Scan0 ) );
           uint8_t* srcData[4] = { ptr, NULL, NULL, NULL };
           int srcLinesize[4] = { bitmapData->Stride, 0, 0, 0 };

           pCurrentPicture = CreateFFmpegPicture(pVideoStream->codec->pix_fmt, pVideoStream->codec->width, pVideoStream->codec->height);

           sws_scale(pImgConvertCtx, srcData, srcLinesize, 0, bitmap->Height, pCurrentPicture->data, pCurrentPicture->linesize );

           bitmap->UnlockBits( bitmapData );

           write_video_frame();

           bitmapData=nullptr;
           ptr=NULL;

           return true;
       }

    AVFrame * VideoEncoder::CreateFFmpegPicture(int pix_fmt, int nWidth, int nHeight)
       {

         AVFrame *picture     = NULL;
         uint8_t *picture_buf = NULL;
         int size;

         picture = avcodec_alloc_frame();
         if ( !picture)
         {
           printf("Cannot create frame\n");
           return NULL;
         }

         size = avpicture_get_size((AVPixelFormat)pix_fmt, nWidth, nHeight);

         picture_buf = (uint8_t *) av_malloc(size);

         if (!picture_buf)
         {
           av_free(picture);
           printf("Cannot allocate buffer\n");
           return NULL;
         }

         avpicture_fill((AVPicture *)picture, picture_buf,
           (AVPixelFormat)pix_fmt, nWidth, nHeight);

         return picture;
       }

       void VideoEncoder::write_video_frame()
       {
           AVCodecContext* codecContext = pVideoStream->codec;

           int out_size, ret = 0;

           if ( pFormatContext->oformat->flags &amp; AVFMT_RAWPICTURE )
           {
               printf( "raw picture must be written" );
           }
           else
           {
               out_size = avcodec_encode_video( codecContext, pVideoEncodeBuffer,nSizeVideoEncodeBuffer, pCurrentPicture );

               if ( out_size > 0 )
               {
                   AVPacket packet;
                   av_init_packet( &amp;packet );

                   if ( codecContext->coded_frame->pts != AV_NOPTS_VALUE )
                   {
                       packet.pts = av_rescale_q( packet.pts, codecContext->time_base, pVideoStream->time_base );
                   }

                   if ( codecContext->coded_frame->pkt_dts != AV_NOPTS_VALUE )
                   {
                       packet.dts = av_rescale_q( packet.dts, codecContext->time_base, pVideoStream->time_base );
                   }

                   if ( codecContext->coded_frame->key_frame )
                   {
                       packet.flags |= AV_PKT_FLAG_KEY;
                   }

                   packet.stream_index = pVideoStream->index;
                   packet.data = pVideoEncodeBuffer;
                   packet.size = out_size;

                   ret = av_interleaved_write_frame( pFormatContext, &amp;packet );

                   av_free_packet(&amp;packet);
                   av_freep(pCurrentPicture);
               }
               else
               {
                   // image was buffered
               }
           }

           if ( ret != 0 )
           {
               throw gcnew Exception( "Error while writing video frame." );
           }
       }


       void VideoEncoder::printleak()
       {
           printf("No of leaks: %d",_CrtDumpMemoryLeaks());
           printf("\n");
       }