2

嗨,我正在使用androidffmpeg一起流式传输到 Wowza 服务器。在这android部分,我只有这个:

public synchronized void onPreviewFrame(byte[] data, Camera camera) {
/* get video data */
naStream(data); // native method
}

在本机端使用ffmpeg(frame_video 是来自的字节数组android):

    av_init_packet(&pkt);
    avpicture_fill((AVPicture *)picture,frame_video,PIX_FMT_YUV420P,640,480);

        if(ret == 0)
            LOGI(7,"avpicture_fill -> error %d", ret);

        /* encode the image */
        pkt.data = NULL;
        pkt.size = 0;

        ret = avcodec_encode_video2(video_c, &pkt, picture, &got_packet);

        if(ret != 0)
            LOGI(7,"avcodec_encode_video2 -> error %d", ret);

        /* if zero size, it means the image was buffered */
        if (got_packet) {
            if (pkt.pts != AV_NOPTS_VALUE) {
                pkt.pts = av_rescale_q(picture->pts, video_c->time_base, AV_TIME_BASE_Q);
            }
            if (pkt.dts != AV_NOPTS_VALUE) {
                pkt.dts = av_rescale_q(pkt.dts, video_c->time_base, AV_TIME_BASE_Q);
            }

            pkt.duration = av_rescale_q(video_st->duration, video_c->time_base, video_st->time_base);

            if (video_c->coded_frame->key_frame)
                pkt.flags |= AV_PKT_FLAG_KEY;

            /* write the compressed frame in the media file */
            ret = av_interleaved_write_frame(oc, &pkt);

这里的问题是流以灰度形式到达...... 我尝试使用以下方法从 NV21 转换为 YUV420P:

img_convert_ctx = sws_getContext(video_c->width, video_c->height,PIX_FMT_NV21, video_c->width, video_c->height,PIX_FMT_YUV420P,SWS_FAST_BILINEAR | SWS_PRINT_INFO, NULL, NULL, NULL);

sws_scale(img_convert_ctx, (const uint8_t * const*)picture->data, picture->linesize, 0,video_c->height, tmp_picture->data, tmp_picture->linesize);

sws_freeContext(img_convert_ctx);

但后来我不能 write_interleaved 数据包......

我正在做这样的“图片”和“ tmp_picture ”分配:

picture = avcodec_alloc_frame();
tmp_picture = avcodec_alloc_frame();

numBytes=avpicture_get_size(PIX_FMT_YUV420P, video_c->width, video_c->height);
buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));

avpicture_fill((AVPicture *)tmp_picture, buffer, PIX_FMT_YUV420P,video_c->width, video_c->height);

在我删除以下行之后:

if (pkt.pts != AV_NOPTS_VALUE) {
                pkt.pts = av_rescale_q(picture->pts, video_c->time_base, AV_TIME_BASE_Q);
            }
            if (pkt.dts != AV_NOPTS_VALUE) {
                pkt.dts = av_rescale_q(pkt.dts, video_c->time_base, AV_TIME_BASE_Q);
            }

并补充说:

if (video_c->coded_frame->pts != AV_NOPTS_VALUE)
  pkt.pts = av_rescale_q(video_c->coded_frame->pts,video_c->time_base, video_st->time_base);

它开始编写交错的数据包,但在 wowza 我看不到任何东西......有人可以帮我吗?

4

0 回答 0