andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | // Placed first to get WEBRTC_VIDEO_ENGINE_FILE_API. |
pbos@webrtc.org | 281cff8 | 2013-05-17 13:44:48 +0000 | [diff] [blame] | 12 | #include "webrtc/engine_configurations.h" |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 13 | |
| 14 | #ifdef WEBRTC_VIDEO_ENGINE_FILE_API |
| 15 | |
mikhal@webrtc.org | 026e6b6 | 2013-01-08 19:19:59 +0000 | [diff] [blame] | 16 | #include "webrtc/video_engine/vie_file_image.h" |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 17 | |
| 18 | #include <stdio.h> // NOLINT |
| 19 | |
mikhal@webrtc.org | 026e6b6 | 2013-01-08 19:19:59 +0000 | [diff] [blame] | 20 | #include "webrtc/common_video/interface/video_image.h" |
| 21 | #include "webrtc/common_video/jpeg/include/jpeg.h" |
| 22 | #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
| 23 | #include "webrtc/system_wrappers/interface/trace.h" |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | |
| 27 | int ViEFileImage::ConvertJPEGToVideoFrame(int engine_id, |
| 28 | const char* file_nameUTF8, |
mikhal@webrtc.org | 3bbed74 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 29 | I420VideoFrame* video_frame) { |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 30 | // Read jpeg file into temporary buffer. |
| 31 | EncodedImage image_buffer; |
| 32 | |
| 33 | FILE* image_file = fopen(file_nameUTF8, "rb"); |
| 34 | if (!image_file) { |
| 35 | WEBRTC_TRACE(kTraceError, kTraceVideo, engine_id, |
| 36 | "%s could not open file %s", __FUNCTION__, file_nameUTF8); |
| 37 | return -1; |
| 38 | } |
| 39 | if (fseek(image_file, 0, SEEK_END) != 0) { |
| 40 | fclose(image_file); |
| 41 | WEBRTC_TRACE(kTraceError, kTraceVideo, engine_id, |
| 42 | "ConvertJPEGToVideoFrame fseek SEEK_END error for file %s", |
| 43 | file_nameUTF8); |
| 44 | return -1; |
| 45 | } |
| 46 | int buffer_size = ftell(image_file); |
| 47 | if (buffer_size == -1) { |
| 48 | fclose(image_file); |
| 49 | WEBRTC_TRACE(kTraceError, kTraceVideo, engine_id, |
| 50 | "ConvertJPEGToVideoFrame could tell file size for file %s", |
| 51 | file_nameUTF8); |
| 52 | return -1; |
| 53 | } |
| 54 | image_buffer._size = buffer_size; |
| 55 | if (fseek(image_file, 0, SEEK_SET) != 0) { |
| 56 | fclose(image_file); |
| 57 | WEBRTC_TRACE(kTraceError, kTraceVideo, engine_id, |
| 58 | "ConvertJPEGToVideoFrame fseek SEEK_SET error for file %s", |
| 59 | file_nameUTF8); |
| 60 | return -1; |
| 61 | } |
pbos@webrtc.org | 67879bc | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 62 | image_buffer._buffer = new uint8_t[ image_buffer._size + 1]; |
| 63 | if (image_buffer._size != fread(image_buffer._buffer, sizeof(uint8_t), |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 64 | image_buffer._size, image_file)) { |
| 65 | WEBRTC_TRACE(kTraceError, kTraceVideo, engine_id, |
| 66 | "%s could not read file %s", __FUNCTION__, file_nameUTF8); |
| 67 | fclose(image_file); |
| 68 | delete [] image_buffer._buffer; |
| 69 | return -1; |
| 70 | } |
| 71 | fclose(image_file); |
| 72 | |
| 73 | int ret = ConvertJpegToI420(image_buffer, video_frame); |
| 74 | |
| 75 | delete [] image_buffer._buffer; |
| 76 | image_buffer._buffer = NULL; |
| 77 | |
| 78 | if (ret == -1) { |
| 79 | WEBRTC_TRACE(kTraceError, kTraceVideo, engine_id, |
| 80 | "%s could decode file %s from jpeg format", __FUNCTION__, |
| 81 | file_nameUTF8); |
| 82 | return -1; |
| 83 | } else if (ret == -3) { |
| 84 | WEBRTC_TRACE(kTraceError, kTraceVideo, engine_id, |
| 85 | "%s could not convert jpeg's data to i420 format", |
| 86 | __FUNCTION__, file_nameUTF8); |
| 87 | } |
| 88 | return 0; |
| 89 | } |
| 90 | |
mikhal@webrtc.org | 3bbed74 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 91 | int ViEFileImage::ConvertPictureToI420VideoFrame(int engine_id, |
| 92 | const ViEPicture& picture, |
| 93 | I420VideoFrame* video_frame) { |
| 94 | int half_width = (picture.width + 1) / 2; |
mikhal@webrtc.org | 026e6b6 | 2013-01-08 19:19:59 +0000 | [diff] [blame] | 95 | video_frame->CreateEmptyFrame(picture.width, picture.height, |
| 96 | picture.width, half_width, half_width); |
| 97 | return ConvertToI420(kI420, picture.data, 0, 0, |
| 98 | picture.width, picture.height, |
| 99 | 0, kRotateNone, video_frame); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | } // namespace webrtc |
| 103 | |
| 104 | #endif // WEBRTC_VIDEO_ENGINE_FILE_API |