1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
CMFCApplication1Dlg *pDlg = (CMFCApplication1Dlg *)carg;
int64_t timeBase;
char src_filename[180];
int refcount = 0;
int video_stream_idx = -1;
int audio_stream_idx = -1;
AVCodecContext *video_dec_ctx = NULL;
AVStream *video_stream = NULL;
AVFormatContext *fmt_ctx = NULL;
int ret;
strcpy_s(src_filename, 180, "rtsp://service:CyresCenco1*@172.26.48.202/rtsp_tunnel");
//strcpy_s(src_filename, 180, "c:\\temp\\Test.avi");
/* open input file, and allocate format context */
//int ret = avformat_open_input(&fmt_ctx, "c:\\temp\\Test.avi", NULL, NULL);
ret = avformat_open_input(&fmt_ctx, src_filename, NULL, NULL);
if (ret < 0)
{
return -1;
}
ret = avformat_find_stream_info(fmt_ctx, NULL);
if (ret < 0)
{
return -1;
}
int stream_index;
AVStream *st;
AVCodec *dec = NULL;
AVDictionary *opts = NULL;
ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
if (ret < 0)
{
return -1;
}
else
{
stream_index = ret;
st = fmt_ctx->streams[stream_index];
/* find decoder for the stream */
dec = avcodec_find_decoder(st->codecpar->codec_id);
if (!dec)
{
return -1;
}
/* Allocate a codec context for the decoder */
video_dec_ctx = avcodec_alloc_context3(dec);
if (!video_dec_ctx)
{
return -1;
}
/* Copy codec parameters from input stream to output codec context */
if ((ret = avcodec_parameters_to_context(video_dec_ctx, st->codecpar)) < 0)
{
fprintf(stderr, "Failed to copy %s codec parameters to decoder context\n",
av_get_media_type_string(AVMEDIA_TYPE_VIDEO));
return -1;
}
/* Init the decoders, with or without reference counting */
av_dict_set(&opts, "refcounted_frames", refcount ? "1" : "0", 0);
if ((ret = avcodec_open2(video_dec_ctx, dec, &opts)) < 0)
{
return -1;
}
video_stream_idx = stream_index;
}
video_stream = fmt_ctx->streams[video_stream_idx];
/*FILE *video_dst_file = fopen("d:\\temp\\ocack.avi", "wb");
if (!video_dst_file)
{
return -1;
}*/
/* allocate image where the decoded image will be put */
int width_src, height_src;
enum AVPixelFormat pix_fmt_src, pix_fmt_dst;
uint8_t *video_tmp_data[4] = { NULL };
int video_tmp_linesize[4];
uint8_t *video_dst_data[4] = { NULL };
int video_dst_linesize[4];
width_src = video_dec_ctx->width;
height_src = video_dec_ctx->height;
pix_fmt_src = video_dec_ctx->pix_fmt;
pix_fmt_dst = AV_PIX_FMT_RGB24;
//AfxMessageBox(av_get_pix_fmt_name(pix_fmt_dst));
ret = av_image_alloc(video_tmp_data, video_tmp_linesize,
width_src, height_src, pix_fmt_src, 1);
ret = av_image_alloc(video_dst_data, video_dst_linesize,
width_src, height_src, pix_fmt_dst /* pix_fmt_src */, 1);
if (ret < 0)
{
return -1;
}
int video_dst_bufsize = ret;
av_dump_format(fmt_ctx, 0, src_filename, 0);
if (!video_stream)
{
return -1;
}
AVFrame *frame;
frame = av_frame_alloc();
if (!frame)
{
return -1;
}
/* initialize packet, set data to NULL, let the demuxer fill it */
AVPacket pkt;
av_init_packet(&pkt);
pkt.data = NULL;
pkt.size = 0;
int got_frame;
int width_dst = width_src / 3;
int height_dst = height_src / 3;
SwsContext *img_convert_ctx = sws_getContext(width_src, height_src, pix_fmt_src, width_dst, height_dst, AV_PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);
CDC *pDC = pDlg->GetDC();
HDC hDC = *pDC;
HDC hDCMem = CreateCompatibleDC(hDC);
BYTE* pbmpData = NULL;
BITMAPINFO bmi = { 0 };
bmi.bmiHeader.biBitCount = 24;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biHeight = -height_src;
bmi.bmiHeader.biWidth = width_src;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biSizeImage = height_src * width_src * 3;
hbmp = CreateDIBSection(hDCMem, &bmi, DIB_RGB_COLORS, (LPVOID *)&pbmpData, NULL, 0);
timeBase = (int64_t(video_dec_ctx->time_base.num) * AV_TIME_BASE) / int64_t(video_dec_ctx->time_base.den);
int cmpFrame = 1;
int64_t seekTarget = int64_t(cmpFrame) * timeBase;
/* read frames from the file */
while (av_read_frame(fmt_ctx, &pkt) >= 0)
{
cmpFrame = cmpFrame + 5;
seekTarget = int64_t(cmpFrame) * timeBase;
AVPacket orig_pkt = pkt;
do
{
got_frame = 0;
ret = avcodec_decode_video2(video_dec_ctx, frame, &got_frame, &pkt);
if (ret < 0)
{
return -1;
}
if (got_frame)
{
if (frame->width != width_src || frame->height != height_src ||
frame->format != pix_fmt_src)
{
return -1;
}
}
av_image_copy(video_tmp_data, video_tmp_linesize,
(const uint8_t **)(frame->data), frame->linesize,
pix_fmt_src, width_src, height_src);
sws_scale(img_convert_ctx, video_tmp_data, video_tmp_linesize, 0, height_src, video_dst_data, video_dst_linesize);
EnterCriticalSection(&csBitMap);
video_dst_data[0] = (uint8_t*)pbmpData;
video_dst_linesize[0] = width_src * 3;
SelectObject(hDCMem, hbmp);
BitBlt(hDC, 0, 0, width_src, height_src, hDCMem, 0, 0, SRCCOPY);
//pDlg->m_BMP.SetBitmap(hbmp);
LeaveCriticalSection(&csBitMap);
//fwrite(video_dst_data[0], 1, video_dst_bufsize, video_dst_file);
if (ret < 0)
break;
pkt.data += ret;
pkt.size -= ret;
} while (pkt.size > 0);
av_packet_unref(&orig_pkt);
}
char Output[200];
sprintf(Output, "Play the output video file with the command:\n"
"ffplay -f rawvideo -pix_fmt %s -video_size %dx%d\n",
av_get_pix_fmt_name(pix_fmt_src), width_src, height_src); |
Partager