<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>It is .mkv.<br><br>Sent from my iPhone</div><div><br>On 28.07.2013, at 23:08, Alex Cohn <<a href="mailto:alexcohn@netvision.net.il">alexcohn@netvision.net.il</a>> wrote:<br><br></div><blockquote type="cite"><div><p dir="ltr">On Jul 23, 2013 11:54 AM, "Marika Marszalkowski" <<a href="mailto:marikaner@gmail.com">marikaner@gmail.com</a>> wrote:<br>
><br>
> I should have added that oformat->video_codec is AV_CODEC_ID_H264, of course.<br>
><br>
> Best,<br>
> Marika<br>
><br>
><br>
> On Mon, Jul 22, 2013 at 8:00 PM, Marika Marszalkowski <<a href="mailto:marikaner@gmail.com">marikaner@gmail.com</a>> wrote:<br>
>><br>
>> Hello everyone,<br>
>><br>
>> I have been trying to create an encoded h264 Video from multiple images (QImage), but I can't really achieve this.<br>
>><br>
>> This list is my last resort and I totally hope there is someone out there who can help me with this not as complicated problem. I am not sure what the exact problem is - the video is created, but it is kind of jumpy, so that an image is shown and it stays there for two second and then there is a very short sequence (like half a second) and it's not moving again. Also the first 40 encoded frames, return 0 (SUCCESS) but got_packet_ptr is 0.<br>

>><br>
>> So this is how I create the video with its header:<br>
>><br>
>>         AVCodec *codec = avcodec_find_encoder(oformat->video_codec);<br>
>><br>
>><br>
>>         m_codecContext = avcodec_alloc_context3(codec);<br>
>><br>
>>         m_codecContext->gop_size = 30;<br>
>><br>
>>         m_codecContext->sample_fmt = AV_SAMPLE_FMT_NONE;<br>
>><br>
>>         m_codecContext->bit_rate = width * height;<br>
>><br>
>>         m_codecContext->width = width;<br>
>><br>
>>         m_codecContext->height = height;<br>
>><br>
>>         m_codecContext->time_base = (AVRational){1,frameRate};<br>
>><br>
>>         m_codecContext->pix_fmt = AV_PIX_FMT_YUV420P;<br>
>><br>
>><br>
>>         formatCtx = avformat_alloc_context();<br>
>><br>
>>         if(!formatCtx)<br>
>><br>
>>         {<br>
>><br>
>>            printf("Error allocating format context\n");<br>
>><br>
>>         }<br>
>><br>
>>         formatCtx->oformat = oformat;<br>
>><br>
>>         formatCtx->video_codec_id = oformat->video_codec;<br>
>><br>
>>         snprintf(formatCtx->filename, sizeof(formatCtx->filename), "%s", outputFileName.toStdString().c_str());<br>
>><br>
>><br>
>>         AVStream *videoStream = av_new_stream(formatCtx, 0);<br>
>><br>
>>         if(!videoStream)<br>
>><br>
>>         {<br>
>><br>
>>            printf("Could not allocate stream\n");<br>
>><br>
>>         }<br>
>><br>
>>         videoStream->codec = m_codecContext;<br>
>><br>
>><br>
>>         if(formatCtx->oformat->flags & AVFMT_GLOBALHEADER)<br>
>><br>
>>         {<br>
>><br>
>>            m_codecContext->flags |= CODEC_FLAG_GLOBAL_HEADER;<br>
>><br>
>>         }<br>
>><br>
>><br>
>>         avcodec_open2(m_codecContext, codec, NULL);<br>
>><br>
>>         avio_open(&formatCtx->pb, outputFileName.toStdString().c_str(), AVIO_FLAG_WRITE);<br>
>><br>
>>         avformat_write_header(formatCtx, NULL);<br>
>><br>
>><br>
>><br>
>> and this is how I add the frames:<br>
>><br>
>><br>
>><br>
>><br>
>>         AVFrame *frame = avcodec_alloc_frame();<br>
>><br>
>>         int size = m_codecContext->width * m_codecContext->height;<br>
>><br>
>>         int numBytes = avpicture_get_size(m_codecContext->pix_fmt, m_codecContext->width, m_codecContext->height);<br>
>><br>
>><br>
>>         QSharedPointer<BlendImage> img = imageData;<br>
>><br>
>><br>
>>         uint8_t *outbuf = (uint8_t *)malloc(numBytes);<br>
>><br>
>>         uint8_t *picture_buf = (uint8_t *)av_malloc(numBytes);<br>
>><br>
>><br>
>>         if (true)<br>
>><br>
>>         {<br>
>><br>
>>             int ret = av_image_fill_arrays(frame->data, frame->linesize, picture_buf, m_codecContext->pix_fmt, m_codecContext->width, m_codecContext->height, 1);<br>
>><br>
>><br>
>>             frame->data[0] = picture_buf;<br>
>><br>
>>             frame->data[1] = frame->data[0] + size;<br>
>><br>
>>             frame->data[2] = frame->data[1] + size/4;<br>
>><br>
>>             frame->linesize[0] = m_codecContext->width;<br>
>><br>
>>             frame->linesize[1] = m_codecContext->width/2;<br>
>><br>
>>             frame->linesize[2] = m_codecContext->width/2;<br>
>><br>
>><br>
>>             fflush(stdout);<br>
>><br>
>>             for (int y = 0; y < m_codecContext->height; y++)<br>
>><br>
>>             {<br>
>><br>
>>                 for (int x = 0; x < m_codecContext->width; x++)<br>
>><br>
>>                 {<br>
>><br>
>>                     unsigned char b = img->bits()[(y * m_codecContext->width + x) * 4 + 0];<br>
>><br>
>>                     unsigned char g = img->bits()[(y * m_codecContext->width + x) * 4 + 1];<br>
>><br>
>>                     unsigned char r = img->bits()[(y * m_codecContext->width + x) * 4 + 2];<br>
>><br>
>><br>
>>                     unsigned char Y = (0.257 * r) + (0.504 * g) + (0.098 * b) + 16;<br>
>><br>
>><br>
>>                     frame->data[0][y * frame->linesize[0] + x] = Y;<br>
>><br>
>><br>
>>                     if (y % 2 == 0 && x % 2 == 0)<br>
>><br>
>>                     {<br>
>><br>
>>                         unsigned char V = (0.439 * r) - (0.368 * g) - (0.071 * b) + 128;<br>
>><br>
>>                         unsigned char U = -(0.148 * r) - (0.291 * g) + (0.439 * b) + 128;<br>
>><br>
>><br>
>>                         frame->data[1][y/2 * frame->linesize[1] + x/2] = U;<br>
>><br>
>>                         frame->data[2][y/2 * frame->linesize[2] + x/2] = V;<br>
>><br>
>>                     }<br>
>><br>
>>                }<br>
>><br>
>>             }<br>
>><br>
>>             int pts = (1.0 / 30.0) * 9000.0 * frameIndex;<br>
>><br>
>>             frame->pts = pts;<br>
>><br>
>><br>
>>             int got_packet_ptr;<br>
>><br>
>><br>
>>             AVPacket packet;<br>
>><br>
>>             av_init_packet(&packet);<br>
>><br>
>>             packet.data = outbuf;<br>
>><br>
>>             packet.size = numBytes;<br>
>><br>
>>             packet.stream_index = formatCtx->streams[0]->index;<br>
>><br>
>>             packet.flags |= AV_PKT_FLAG_KEY;<br>
>><br>
>>             packet.pts = packet.dts = pts;<br>
>><br>
>><br>
>>             m_codecContext->coded_frame->pts = pts; <br>
>><br>
>><br>
>>             ret = avcodec_encode_video2(m_codecContext, &packet, frame, &got_packet_ptr);<br>
>><br>
>><br>
>><br>
>>             if (got_packet_ptr != 0)<br>
>><br>
>>             {<br>
>><br>
>>                 m_codecContext->coded_frame->pts = pts;<br>
>><br>
>><br>
>>                 if (m_codecContext->coded_frame->pts != (0x8000000000000000LL))<br>
>><br>
>>                     pts = av_rescale_q(m_codecContext->coded_frame->pts, m_codecContext->time_base, formatCtx->streams[0]->time_base);<br>
>><br>
>><br>
>>                    packet.pts = pts;<br>
>><br>
>><br>
>>                 if(m_codecContext->coded_frame->key_frame)<br>
>><br>
>>                 {<br>
>><br>
>>                    packet.flags |= AV_PKT_FLAG_KEY;<br>
>><br>
>>                 }<br>
>><br>
>><br>
>><br>
>>                 std::cout << "pts: " << packet.pts << ", dts: "  << packet.dts << std::endl;<br>
>><br>
>><br>
>>                 av_interleaved_write_frame(formatCtx, &packet);<br>
>><br>
>>                 av_free_packet(&packet);<br>
>><br>
>>             }<br>
>><br>
>>         }<br>
>><br>
>><br>
>>         free(picture_buf);<br>
>><br>
>>         free(outbuf);<br>
>><br>
>>         av_free(frame);<br>
>><br>
>><br>
>><br>
>> and of course in the end:<br>
>><br>
>><br>
>>         av_write_trailer(formatCtx);<br>
>><br>
>>         fclose(m_file);<br>
>><br>
>>         avcodec_close(m_codecContext);<br>
>><br>
>>         av_free(m_codecContext);<br>
>><br>
>><br>
>><br>
>> I think this has something to do with the PTS/DTS values. PTS values start at 120 (as the first 40 frames are omitted) and are increased by 3 ((1.0 / 30.0) * 9000.0 * frameIndex) and the DTS values start at -6 also being increased by 3 per frame. But it might also be the settings or anything else. I feel like I have tried everything I was able to find on the internet and it's still wrong. I am desperate, help would be soooo appreciated!<br>

>><br>
>><br>
>> Cheers,<br>
>><br>
>> Marika</p>
<p dir="ltr">What is your output format?<br>
Alex<br>
</p>
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>Libav-user mailing list</span><br><span><a href="mailto:Libav-user@ffmpeg.org">Libav-user@ffmpeg.org</a></span><br><span><a href="http://ffmpeg.org/mailman/listinfo/libav-user">http://ffmpeg.org/mailman/listinfo/libav-user</a></span><br></div></blockquote></body></html>