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" |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | |
| 26 | int ViEFileImage::ConvertJPEGToVideoFrame(int engine_id, |
| 27 | const char* file_nameUTF8, |
mikhal@webrtc.org | 3bbed74 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 28 | I420VideoFrame* video_frame) { |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 29 | // Read jpeg file into temporary buffer. |
| 30 | EncodedImage image_buffer; |
| 31 | |
| 32 | FILE* image_file = fopen(file_nameUTF8, "rb"); |
| 33 | if (!image_file) { |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 34 | return -1; |
| 35 | } |
| 36 | if (fseek(image_file, 0, SEEK_END) != 0) { |
| 37 | fclose(image_file); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 38 | return -1; |
| 39 | } |
| 40 | int buffer_size = ftell(image_file); |
| 41 | if (buffer_size == -1) { |
| 42 | fclose(image_file); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 43 | return -1; |
| 44 | } |
| 45 | image_buffer._size = buffer_size; |
| 46 | if (fseek(image_file, 0, SEEK_SET) != 0) { |
| 47 | fclose(image_file); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 48 | return -1; |
| 49 | } |
pbos@webrtc.org | 67879bc | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 50 | image_buffer._buffer = new uint8_t[ image_buffer._size + 1]; |
| 51 | if (image_buffer._size != fread(image_buffer._buffer, sizeof(uint8_t), |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 52 | image_buffer._size, image_file)) { |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 53 | fclose(image_file); |
| 54 | delete [] image_buffer._buffer; |
| 55 | return -1; |
| 56 | } |
| 57 | fclose(image_file); |
| 58 | |
| 59 | int ret = ConvertJpegToI420(image_buffer, video_frame); |
| 60 | |
| 61 | delete [] image_buffer._buffer; |
| 62 | image_buffer._buffer = NULL; |
| 63 | |
| 64 | if (ret == -1) { |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 65 | return -1; |
| 66 | } else if (ret == -3) { |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 67 | } |
| 68 | return 0; |
| 69 | } |
| 70 | |
mikhal@webrtc.org | 3bbed74 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 71 | int ViEFileImage::ConvertPictureToI420VideoFrame(int engine_id, |
| 72 | const ViEPicture& picture, |
| 73 | I420VideoFrame* video_frame) { |
| 74 | int half_width = (picture.width + 1) / 2; |
mikhal@webrtc.org | 026e6b6 | 2013-01-08 19:19:59 +0000 | [diff] [blame] | 75 | video_frame->CreateEmptyFrame(picture.width, picture.height, |
| 76 | picture.width, half_width, half_width); |
| 77 | return ConvertToI420(kI420, picture.data, 0, 0, |
| 78 | picture.width, picture.height, |
| 79 | 0, kRotateNone, video_frame); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | } // namespace webrtc |
| 83 | |
| 84 | #endif // WEBRTC_VIDEO_ENGINE_FILE_API |